t_tsan_vptr_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 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	cat > test.cc << EOF
103#include <pthread.h>
104pthread_barrier_t barrier;
105struct A {
106  volatile bool done;
107  A(): done(false) { }
108  virtual void Done() { done = true; }
109  virtual ~A() { while (!done) ; }
110};
111struct B: A {};
112A *obj = new B;
113void *Thread1(void *x) {
114  pthread_barrier_wait(&barrier);
115  obj->Done();
116  return NULL;
117}
118int main() {
119  pthread_barrier_init(&barrier, NULL, 2);
120  pthread_t t;
121  pthread_create(&t, NULL, Thread1, NULL);
122  pthread_barrier_wait(&barrier);
123  delete obj;
124  pthread_join(t, NULL);
125  return 0;
126}
127EOF
128
129	c++ -fsanitize=thread -o test -pg test.cc
130	paxctl +a test
131	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
132}
133
134vptr_race_pic_body(){
135	cat > test.cc << EOF
136#include <stdio.h>
137#include <stdlib.h>
138int help(int);
139int main(int argc, char **argv) {return help(argc);}
140EOF
141
142	cat > pic.cc << EOF
143#include <pthread.h>
144pthread_barrier_t barrier;
145struct A {
146  volatile bool done;
147  A(): done(false) { }
148  virtual void Done() { done = true; }
149  virtual ~A() { while (!done) ; }
150};
151struct B: A {};
152A *obj = new B;
153void *Thread1(void *x) {
154  pthread_barrier_wait(&barrier);
155  obj->Done();
156  return NULL;
157}
158int help(int argc) {
159  pthread_barrier_init(&barrier, NULL, 2);
160  pthread_t t;
161  pthread_create(&t, NULL, Thread1, NULL);
162  pthread_barrier_wait(&barrier);
163  delete obj;
164  pthread_join(t, NULL);
165  return 0;
166}
167EOF
168
169	c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc
170	c++ -o test test.cc -fsanitize=thread -L. -ltest
171	paxctl +a test
172
173	export LD_LIBRARY_PATH=.
174	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
175}
176vptr_race_pie_body(){
177	
178	#check whether -pie flag is supported on this architecture
179	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
180		atf_set_skip "c++ -pie not supported on this architecture"
181	fi
182	cat > test.cc << EOF
183#include <pthread.h>
184pthread_barrier_t barrier;
185struct A {
186  volatile bool done;
187  A(): done(false) { }
188  virtual void Done() { done = true; }
189  virtual ~A() { while (!done) ; }
190};
191struct B: A {};
192A *obj = new B;
193void *Thread1(void *x) {
194  pthread_barrier_wait(&barrier);
195  obj->Done();
196  return NULL;
197}
198int main() {
199  pthread_barrier_init(&barrier, NULL, 2);
200  pthread_t t;
201  pthread_create(&t, NULL, Thread1, NULL);
202  pthread_barrier_wait(&barrier);
203  delete obj;
204  pthread_join(t, NULL);
205  return 0;
206}
207EOF
208
209	c++ -fsanitize=thread -o test -fpie -pie test.cc
210	paxctl +a test
211	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test
212}
213
214
215atf_test_case target_not_supported
216target_not_supported_head()
217{
218	atf_set "descr" "Test forced skip"
219}
220
221target_not_supported_body()
222{
223	atf_skip "Target is not supported"
224}
225
226atf_init_test_cases()
227{
228	test_target
229	test $SUPPORT = 'n' && {
230		atf_add_test_case target_not_supported
231		return 0
232	}
233	atf_add_test_case vptr_race
234	atf_add_test_case vptr_race_profile
235	atf_add_test_case vptr_race_pie
236	atf_add_test_case vptr_race_pic
237}
238