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