t_tsan_vptr_race.sh revision 1.4
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 vptr_race
47vptr_race_head() {
48	atf_set "descr" "Test thread sanitizer for vptr race condition"
49	atf_set "require.progs" "c++ paxctl"
50}
51
52atf_test_case vptr_race_profile
53vptr_race_profile_head() {
54	atf_set "descr" "Test thread sanitizer for vptr race with profiling option"
55	atf_set "require.progs" "c++ paxctl"
56}
57atf_test_case vptr_race_pic
58vptr_race_pic_head() {
59	atf_set "descr" "Test thread sanitizer for vptr race with position independent code (PIC) flag"
60	atf_set "require.progs" "c++ paxctl"
61}
62atf_test_case vptr_race_pie
63vptr_race_pie_head() {
64	atf_set "descr" "Test thread sanitizer for vptr race with position independent execution (PIE) flag"
65	atf_set "require.progs" "c++ paxctl"
66}
67
68vptr_race_body(){
69	cat > test.cc << EOF
70#include <pthread.h>
71pthread_barrier_t barrier;
72struct A {
73  volatile bool done;
74  A(): done(false) { }
75  virtual void Done() { done = true; }
76  virtual ~A() { while (!done) ; }
77};
78struct B: A {};
79A *obj = new B;
80void *Thread1(void *x) {
81  pthread_barrier_wait(&barrier);
82  obj->Done();
83  return NULL;
84}
85int main() {
86  pthread_barrier_init(&barrier, NULL, 2);
87  pthread_t t;
88  pthread_create(&t, NULL, Thread1, NULL);
89  pthread_barrier_wait(&barrier);
90  delete obj;
91  pthread_join(t, NULL);
92  return 0;
93}
94EOF
95
96	c++ -fsanitize=thread -o test test.cc
97	paxctl +a test
98	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
99}
100
101vptr_race_profile_body(){
102	atf_expect_fail "PR toolchain/55760"
103	cat > test.cc << EOF
104#include <pthread.h>
105pthread_barrier_t barrier;
106struct A {
107  volatile bool done;
108  A(): done(false) { }
109  virtual void Done() { done = true; }
110  virtual ~A() { while (!done) ; }
111};
112struct B: A {};
113A *obj = new B;
114void *Thread1(void *x) {
115  pthread_barrier_wait(&barrier);
116  obj->Done();
117  return NULL;
118}
119int main() {
120  pthread_barrier_init(&barrier, NULL, 2);
121  pthread_t t;
122  pthread_create(&t, NULL, Thread1, NULL);
123  pthread_barrier_wait(&barrier);
124  delete obj;
125  pthread_join(t, NULL);
126  return 0;
127}
128EOF
129
130	c++ -fsanitize=thread -o test -pg test.cc
131	paxctl +a test
132	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
133}
134
135vptr_race_pic_body(){
136	cat > test.cc << EOF
137#include <stdio.h>
138#include <stdlib.h>
139int help(int);
140int main(int argc, char **argv) {return help(argc);}
141EOF
142
143	cat > pic.cc << EOF
144#include <pthread.h>
145pthread_barrier_t barrier;
146struct A {
147  volatile bool done;
148  A(): done(false) { }
149  virtual void Done() { done = true; }
150  virtual ~A() { while (!done) ; }
151};
152struct B: A {};
153A *obj = new B;
154void *Thread1(void *x) {
155  pthread_barrier_wait(&barrier);
156  obj->Done();
157  return NULL;
158}
159int help(int argc) {
160  pthread_barrier_init(&barrier, NULL, 2);
161  pthread_t t;
162  pthread_create(&t, NULL, Thread1, NULL);
163  pthread_barrier_wait(&barrier);
164  delete obj;
165  pthread_join(t, NULL);
166  return 0;
167}
168EOF
169
170	c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc
171	c++ -o test test.cc -fsanitize=thread -L. -ltest
172	paxctl +a test
173
174	export LD_LIBRARY_PATH=.
175	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
176}
177vptr_race_pie_body(){
178	
179	#check whether -pie flag is supported on this architecture
180	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
181		atf_set_skip "c++ -pie not supported on this architecture"
182	fi
183	cat > test.cc << EOF
184#include <pthread.h>
185pthread_barrier_t barrier;
186struct A {
187  volatile bool done;
188  A(): done(false) { }
189  virtual void Done() { done = true; }
190  virtual ~A() { while (!done) ; }
191};
192struct B: A {};
193A *obj = new B;
194void *Thread1(void *x) {
195  pthread_barrier_wait(&barrier);
196  obj->Done();
197  return NULL;
198}
199int main() {
200  pthread_barrier_init(&barrier, NULL, 2);
201  pthread_t t;
202  pthread_create(&t, NULL, Thread1, NULL);
203  pthread_barrier_wait(&barrier);
204  delete obj;
205  pthread_join(t, NULL);
206  return 0;
207}
208EOF
209
210	c++ -fsanitize=thread -o test -fpie -pie test.cc
211	paxctl +a test
212	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
213}
214
215
216atf_test_case target_not_supported
217target_not_supported_head()
218{
219	atf_set "descr" "Test forced skip"
220}
221
222target_not_supported_body()
223{
224	atf_skip "Target is not supported"
225}
226
227atf_init_test_cases()
228{
229	test_target
230	test $SUPPORT = 'n' && {
231		atf_add_test_case target_not_supported
232		return 0
233	}
234	atf_add_test_case vptr_race
235	atf_add_test_case vptr_race_profile
236	atf_add_test_case vptr_race_pie
237	atf_add_test_case vptr_race_pic
238}
239