t_tsan_lock_order_inversion.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
29tsan_available_archs()
30{
31	atf_set "require.arch" "x86_64"
32}
33
34atf_test_case lock_order_inversion
35lock_order_inversion_head() {
36	atf_set "descr" "Test thread sanitizer for lock order inversion condition"
37	atf_set "require.progs" "cc paxctl"
38	tsan_available_archs
39}
40
41atf_test_case lock_order_inversion_profile
42lock_order_inversion_profile_head() {
43	atf_set "descr" "Test thread sanitizer for lock order inversion with profiling option"
44	atf_set "require.progs" "cc paxctl"
45	tsan_available_archs
46}
47atf_test_case lock_order_inversion_pic
48lock_order_inversion_pic_head() {
49	atf_set "descr" "Test thread sanitizer for lock order inversion with position independent code (PIC) flag"
50	atf_set "require.progs" "cc paxctl"
51	tsan_available_archs
52}
53atf_test_case lock_order_inversion_pie
54lock_order_inversion_pie_head() {
55	atf_set "descr" "Test thread sanitizer for lock order inversion with position independent execution (PIE) flag"
56	atf_set "require.progs" "cc paxctl"
57	tsan_available_archs
58}
59
60lock_order_inversion_body(){
61	cat > test.c << EOF
62#include <pthread.h>
63
64pthread_mutex_t l1, l2;
65int main() {
66  pthread_mutex_init(&l1, NULL);
67  pthread_mutex_init(&l2, NULL);
68  pthread_mutex_lock(&l2);
69  pthread_mutex_lock(&l1);
70  pthread_mutex_unlock(&l1);
71  pthread_mutex_unlock(&l2);
72
73  pthread_mutex_lock(&l1);
74  pthread_mutex_lock(&l2);
75  pthread_mutex_unlock(&l2);
76  pthread_mutex_unlock(&l1);
77  return 0;
78}
79EOF
80
81	cc -fsanitize=thread -o test test.c
82	paxctl +a test
83	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
84}
85
86lock_order_inversion_profile_body(){
87	cat > test.c << EOF
88#include <pthread.h>
89
90pthread_mutex_t l1, l2;
91int main() {
92  pthread_mutex_init(&l1, NULL);
93  pthread_mutex_init(&l2, NULL);
94  pthread_mutex_lock(&l2);
95  pthread_mutex_lock(&l1);
96  pthread_mutex_unlock(&l1);
97  pthread_mutex_unlock(&l2);
98
99  pthread_mutex_lock(&l1);
100  pthread_mutex_lock(&l2);
101  pthread_mutex_unlock(&l2);
102  pthread_mutex_unlock(&l1);
103  return 0;
104}
105EOF
106
107	cc -fsanitize=thread -o test -pg test.c
108	paxctl +a test
109	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
110}
111
112lock_order_inversion_pic_body(){
113	cat > test.c << EOF
114#include <stdio.h>
115#include <stdlib.h>
116int help(int);
117int main(int argc, char **argv) {return help(argc);}
118EOF
119
120	cat > pic.c << EOF
121#include <pthread.h>
122
123pthread_mutex_t l1, l2;
124int help(int argc) {
125  pthread_mutex_init(&l1, NULL);
126  pthread_mutex_init(&l2, NULL);
127  pthread_mutex_lock(&l2);
128  pthread_mutex_lock(&l1);
129  pthread_mutex_unlock(&l1);
130  pthread_mutex_unlock(&l2);
131
132  pthread_mutex_lock(&l1);
133  pthread_mutex_lock(&l2);
134  pthread_mutex_unlock(&l2);
135  pthread_mutex_unlock(&l1);
136  return 0;
137}
138EOF
139
140	cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
141	cc -o test test.c -fsanitize=thread -L. -ltest
142	paxctl +a test
143
144	export LD_LIBRARY_PATH=.
145	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
146}
147lock_order_inversion_pie_body(){
148	
149	#check whether -pie flag is supported on this architecture
150	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
151		atf_set_skip "cc -pie not supported on this architecture"
152	fi
153	cat > test.c << EOF
154#include <pthread.h>
155
156pthread_mutex_t l1, l2;
157int main() {
158  pthread_mutex_init(&l1, NULL);
159  pthread_mutex_init(&l2, NULL);
160  pthread_mutex_lock(&l2);
161  pthread_mutex_lock(&l1);
162  pthread_mutex_unlock(&l1);
163  pthread_mutex_unlock(&l2);
164
165  pthread_mutex_lock(&l1);
166  pthread_mutex_lock(&l2);
167  pthread_mutex_unlock(&l2);
168  pthread_mutex_unlock(&l1);
169  return 0;
170}
171EOF
172
173	cc -fsanitize=thread -o test -fpie -pie test.c
174	paxctl +a test
175	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
176}
177
178atf_init_test_cases()
179{
180	atf_add_test_case lock_order_inversion
181	atf_add_test_case lock_order_inversion_profile
182	atf_add_test_case lock_order_inversion_pie
183	atf_add_test_case lock_order_inversion_pic
184}
185