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