t_tsan_lock_order_inversion.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 lock_order_inversion
47lock_order_inversion_head() {
48	atf_set "descr" "Test thread sanitizer for lock order inversion condition"
49	atf_set "require.progs" "c++ paxctl"
50}
51
52atf_test_case lock_order_inversion_profile
53lock_order_inversion_profile_head() {
54	atf_set "descr" "Test thread sanitizer for lock order inversion with profiling option"
55	atf_set "require.progs" "c++ paxctl"
56}
57atf_test_case lock_order_inversion_pic
58lock_order_inversion_pic_head() {
59	atf_set "descr" "Test thread sanitizer for lock order inversion with position independent code (PIC) flag"
60	atf_set "require.progs" "c++ paxctl"
61}
62atf_test_case lock_order_inversion_pie
63lock_order_inversion_pie_head() {
64	atf_set "descr" "Test thread sanitizer for lock order inversion with position independent execution (PIE) flag"
65	atf_set "require.progs" "c++ paxctl"
66}
67
68lock_order_inversion_body(){
69	cat > test.cc << EOF
70#include <pthread.h>
71
72pthread_mutex_t l1, l2;
73int main() {
74  pthread_mutex_init(&l1, NULL);
75  pthread_mutex_init(&l2, NULL);
76  pthread_mutex_lock(&l2);
77  pthread_mutex_lock(&l1);
78  pthread_mutex_unlock(&l1);
79  pthread_mutex_unlock(&l2);
80
81  pthread_mutex_lock(&l1);
82  pthread_mutex_lock(&l2);
83  pthread_mutex_unlock(&l2);
84  pthread_mutex_unlock(&l1);
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: lock-order-inversion" ./test
92}
93
94lock_order_inversion_profile_body(){
95	cat > test.cc << EOF
96#include <pthread.h>
97
98pthread_mutex_t l1, l2;
99int main() {
100  pthread_mutex_init(&l1, NULL);
101  pthread_mutex_init(&l2, NULL);
102  pthread_mutex_lock(&l2);
103  pthread_mutex_lock(&l1);
104  pthread_mutex_unlock(&l1);
105  pthread_mutex_unlock(&l2);
106
107  pthread_mutex_lock(&l1);
108  pthread_mutex_lock(&l2);
109  pthread_mutex_unlock(&l2);
110  pthread_mutex_unlock(&l1);
111  return 0;
112}
113EOF
114
115	c++ -fsanitize=thread -o test -pg test.cc
116	paxctl +a test
117	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
118}
119
120lock_order_inversion_pic_body(){
121	cat > test.cc << EOF
122#include <stdio.h>
123#include <stdlib.h>
124int help(int);
125int main(int argc, char **argv) {return help(argc);}
126EOF
127
128	cat > pic.cc << EOF
129#include <pthread.h>
130
131pthread_mutex_t l1, l2;
132int help(int argc) {
133  pthread_mutex_init(&l1, NULL);
134  pthread_mutex_init(&l2, NULL);
135  pthread_mutex_lock(&l2);
136  pthread_mutex_lock(&l1);
137  pthread_mutex_unlock(&l1);
138  pthread_mutex_unlock(&l2);
139
140  pthread_mutex_lock(&l1);
141  pthread_mutex_lock(&l2);
142  pthread_mutex_unlock(&l2);
143  pthread_mutex_unlock(&l1);
144  return 0;
145}
146EOF
147
148	c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc
149	c++ -o test test.cc -fsanitize=thread -L. -ltest
150	paxctl +a test
151
152	export LD_LIBRARY_PATH=.
153	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
154}
155lock_order_inversion_pie_body(){
156	
157	#check whether -pie flag is supported on this architecture
158	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
159		atf_set_skip "c++ -pie not supported on this architecture"
160	fi
161	cat > test.cc << EOF
162#include <pthread.h>
163
164pthread_mutex_t l1, l2;
165int main() {
166  pthread_mutex_init(&l1, NULL);
167  pthread_mutex_init(&l2, NULL);
168  pthread_mutex_lock(&l2);
169  pthread_mutex_lock(&l1);
170  pthread_mutex_unlock(&l1);
171  pthread_mutex_unlock(&l2);
172
173  pthread_mutex_lock(&l1);
174  pthread_mutex_lock(&l2);
175  pthread_mutex_unlock(&l2);
176  pthread_mutex_unlock(&l1);
177  return 0;
178}
179EOF
180
181	c++ -fsanitize=thread -o test -fpie -pie test.cc
182	paxctl +a test
183	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
184}
185
186
187atf_test_case target_not_supported
188target_not_supported_head()
189{
190	atf_set "descr" "Test forced skip"
191}
192
193target_not_supported_body()
194{
195	atf_skip "Target is not supported"
196}
197
198atf_init_test_cases()
199{
200	test_target
201	test $SUPPORT = 'n' && {
202		atf_add_test_case target_not_supported
203		return 0
204	}
205	atf_add_test_case lock_order_inversion
206	atf_add_test_case lock_order_inversion_profile
207	atf_add_test_case lock_order_inversion_pie
208	atf_add_test_case lock_order_inversion_pic
209}
210