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