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