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