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