t_tsan_locked_mutex_destroy.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 locked_mutex_destroy
36locked_mutex_destroy_head() {
37	atf_set "descr" "Test thread sanitizer for destroying locked mutex condition"
38	atf_set "require.progs" "c++ paxctl"
39	tsan_available_archs
40}
41
42atf_test_case locked_mutex_destroy_profile
43locked_mutex_destroy_profile_head() {
44	atf_set "descr" "Test thread sanitizer for destroying locked mutex with profiling option"
45	atf_set "require.progs" "c++ paxctl"
46	tsan_available_archs
47}
48atf_test_case locked_mutex_destroy_pic
49locked_mutex_destroy_pic_head() {
50	atf_set "descr" "Test thread sanitizer for destroying locked mutex with position independent code (PIC) flag"
51	atf_set "require.progs" "c++ paxctl"
52	tsan_available_archs
53}
54atf_test_case locked_mutex_destroy_pie
55locked_mutex_destroy_pie_head() {
56	atf_set "descr" "Test thread sanitizer for destroying locked mutex with position independent execution (PIE) flag"
57	atf_set "require.progs" "c++ paxctl"
58	tsan_available_archs
59}
60
61locked_mutex_destroy_body(){
62	cat > test.cc << EOF
63#include <pthread.h>
64#include <stdlib.h>
65
66pthread_mutex_t mutex;
67pthread_barrier_t barrier;
68void *Thread(void *a) {
69  pthread_mutex_lock(&mutex);
70  pthread_barrier_wait(&barrier);
71  return 0;
72}
73
74int main() {
75  pthread_t t;
76  pthread_barrier_init(&barrier, NULL, 2);
77  pthread_mutex_init(&mutex, NULL);
78  pthread_create(&t, NULL, Thread, NULL);
79  pthread_barrier_wait(&barrier);
80  pthread_mutex_destroy(&mutex);
81  pthread_join(t, NULL);
82  return 0;
83}
84EOF
85
86	c++ -fsanitize=thread -o test test.cc
87	paxctl +a test
88	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test
89}
90
91locked_mutex_destroy_profile_body(){
92	cat > test.cc << EOF
93#include <pthread.h>
94#include <stdlib.h>
95
96pthread_mutex_t mutex;
97pthread_barrier_t barrier;
98void *Thread(void *a) {
99  pthread_mutex_lock(&mutex);
100  pthread_barrier_wait(&barrier);
101  return 0;
102}
103
104int main() {
105  pthread_t t;
106  pthread_barrier_init(&barrier, NULL, 2);
107  pthread_mutex_init(&mutex, NULL);
108  pthread_create(&t, NULL, Thread, NULL);
109  pthread_barrier_wait(&barrier);
110  pthread_mutex_destroy(&mutex);
111  pthread_join(t, NULL);
112  return 0;
113}
114EOF
115
116	c++ -fsanitize=thread -o test -pg test.cc
117	paxctl +a test
118	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test
119}
120
121locked_mutex_destroy_pic_body(){
122	cat > test.cc << EOF
123#include <stdio.h>
124#include <stdlib.h>
125int help(int);
126int main(int argc, char **argv) {return help(argc);}
127EOF
128
129	cat > pic.cc << EOF
130#include <pthread.h>
131#include <stdlib.h>
132
133pthread_mutex_t mutex;
134pthread_barrier_t barrier;
135void *Thread(void *a) {
136  pthread_mutex_lock(&mutex);
137  pthread_barrier_wait(&barrier);
138  return 0;
139}
140
141int help(int argc) {
142  pthread_t t;
143  pthread_barrier_init(&barrier, NULL, 2);
144  pthread_mutex_init(&mutex, NULL);
145  pthread_create(&t, NULL, Thread, NULL);
146  pthread_barrier_wait(&barrier);
147  pthread_mutex_destroy(&mutex);
148  pthread_join(t, NULL);
149  return 0;
150}
151EOF
152
153	c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc
154	c++ -o test test.cc -fsanitize=thread -L. -ltest
155	paxctl +a test
156
157	export LD_LIBRARY_PATH=.
158	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test
159}
160locked_mutex_destroy_pie_body(){
161	
162	#check whether -pie flag is supported on this architecture
163	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
164		atf_set_skip "c++ -pie not supported on this architecture"
165	fi
166	cat > test.cc << EOF
167#include <pthread.h>
168#include <stdlib.h>
169
170pthread_mutex_t mutex;
171pthread_barrier_t barrier;
172void *Thread(void *a) {
173  pthread_mutex_lock(&mutex);
174  pthread_barrier_wait(&barrier);
175  return 0;
176}
177
178int main() {
179  pthread_t t;
180  pthread_barrier_init(&barrier, NULL, 2);
181  pthread_mutex_init(&mutex, NULL);
182  pthread_create(&t, NULL, Thread, NULL);
183  pthread_barrier_wait(&barrier);
184  pthread_mutex_destroy(&mutex);
185  pthread_join(t, NULL);
186  return 0;
187}
188EOF
189
190	c++ -fsanitize=thread -o test -fpie -pie test.cc
191	paxctl +a test
192	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test
193}
194
195
196atf_init_test_cases()
197{
198	atf_add_test_case locked_mutex_destroy
199	atf_add_test_case locked_mutex_destroy_profile
200	atf_add_test_case locked_mutex_destroy_pie
201	atf_add_test_case locked_mutex_destroy_pic
202}
203