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