Home | History | Annotate | Line # | Download | only in c++
      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 
     30 tsan_available_archs()
     31 {
     32 	atf_set "require.arch" "x86_64"
     33 }
     34 
     35 atf_test_case lock_order_inversion
     36 lock_order_inversion_head() {
     37 	atf_set "descr" "Test thread sanitizer for lock order inversion condition"
     38 	atf_set "require.progs" "c++ paxctl"
     39 	tsan_available_archs
     40 }
     41 
     42 atf_test_case lock_order_inversion_profile
     43 lock_order_inversion_profile_head() {
     44 	atf_set "descr" "Test thread sanitizer for lock order inversion with profiling option"
     45 	atf_set "require.progs" "c++ paxctl"
     46 	tsan_available_archs
     47 }
     48 atf_test_case lock_order_inversion_pic
     49 lock_order_inversion_pic_head() {
     50 	atf_set "descr" "Test thread sanitizer for lock order inversion with position independent code (PIC) flag"
     51 	atf_set "require.progs" "c++ paxctl"
     52 	tsan_available_archs
     53 }
     54 atf_test_case lock_order_inversion_pie
     55 lock_order_inversion_pie_head() {
     56 	atf_set "descr" "Test thread sanitizer for lock order inversion with position independent execution (PIE) flag"
     57 	atf_set "require.progs" "c++ paxctl"
     58 	tsan_available_archs
     59 }
     60 
     61 lock_order_inversion_body(){
     62 	cat > test.cc << EOF
     63 #include <pthread.h>
     64 
     65 pthread_mutex_t l1, l2;
     66 int main() {
     67   pthread_mutex_init(&l1, NULL);
     68   pthread_mutex_init(&l2, NULL);
     69   pthread_mutex_lock(&l2);
     70   pthread_mutex_lock(&l1);
     71   pthread_mutex_unlock(&l1);
     72   pthread_mutex_unlock(&l2);
     73 
     74   pthread_mutex_lock(&l1);
     75   pthread_mutex_lock(&l2);
     76   pthread_mutex_unlock(&l2);
     77   pthread_mutex_unlock(&l1);
     78   return 0;
     79 }
     80 EOF
     81 
     82 	c++ -fsanitize=thread -o test test.cc
     83 	paxctl +a test
     84 	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
     85 }
     86 
     87 lock_order_inversion_profile_body(){
     88 	atf_expect_fail "PR toolchain/55760"
     89 	cat > test.cc << EOF
     90 #include <pthread.h>
     91 
     92 pthread_mutex_t l1, l2;
     93 int main() {
     94   pthread_mutex_init(&l1, NULL);
     95   pthread_mutex_init(&l2, NULL);
     96   pthread_mutex_lock(&l2);
     97   pthread_mutex_lock(&l1);
     98   pthread_mutex_unlock(&l1);
     99   pthread_mutex_unlock(&l2);
    100 
    101   pthread_mutex_lock(&l1);
    102   pthread_mutex_lock(&l2);
    103   pthread_mutex_unlock(&l2);
    104   pthread_mutex_unlock(&l1);
    105   return 0;
    106 }
    107 EOF
    108 
    109 	c++ -fsanitize=thread -static -o test -pg test.cc
    110 	paxctl +a test
    111 	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
    112 }
    113 
    114 lock_order_inversion_pic_body(){
    115 	cat > test.cc << 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.cc << EOF
    123 #include <pthread.h>
    124 
    125 pthread_mutex_t l1, l2;
    126 int help(int argc) {
    127   pthread_mutex_init(&l1, NULL);
    128   pthread_mutex_init(&l2, NULL);
    129   pthread_mutex_lock(&l2);
    130   pthread_mutex_lock(&l1);
    131   pthread_mutex_unlock(&l1);
    132   pthread_mutex_unlock(&l2);
    133 
    134   pthread_mutex_lock(&l1);
    135   pthread_mutex_lock(&l2);
    136   pthread_mutex_unlock(&l2);
    137   pthread_mutex_unlock(&l1);
    138   return 0;
    139 }
    140 EOF
    141 
    142 	c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc
    143 	c++ -o test test.cc -fsanitize=thread -L. -ltest
    144 	paxctl +a test
    145 
    146 	export LD_LIBRARY_PATH=.
    147 	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
    148 }
    149 lock_order_inversion_pie_body(){
    150 
    151 	#check whether -pie flag is supported on this architecture
    152 	if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
    153 		atf_set_skip "c++ -pie not supported on this architecture"
    154 	fi
    155 	cat > test.cc << EOF
    156 #include <pthread.h>
    157 
    158 pthread_mutex_t l1, l2;
    159 int main() {
    160   pthread_mutex_init(&l1, NULL);
    161   pthread_mutex_init(&l2, NULL);
    162   pthread_mutex_lock(&l2);
    163   pthread_mutex_lock(&l1);
    164   pthread_mutex_unlock(&l1);
    165   pthread_mutex_unlock(&l2);
    166 
    167   pthread_mutex_lock(&l1);
    168   pthread_mutex_lock(&l2);
    169   pthread_mutex_unlock(&l2);
    170   pthread_mutex_unlock(&l1);
    171   return 0;
    172 }
    173 EOF
    174 
    175 	c++ -fsanitize=thread -o test -fpie -pie test.cc
    176 	paxctl +a test
    177 	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: lock-order-inversion" ./test
    178 }
    179 
    180 atf_init_test_cases()
    181 {
    182 	atf_add_test_case lock_order_inversion
    183 	atf_add_test_case lock_order_inversion_profile
    184 	atf_add_test_case lock_order_inversion_pie
    185 	atf_add_test_case lock_order_inversion_pic
    186 }
    187