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