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