1276630Skib# Copyright (c) 2018 The NetBSD Foundation, Inc. 2276630Skib# All rights reserved. 3276630Skib# 4276630Skib# This code is derived from software contributed to The NetBSD Foundation 5276630Skib# by Yang Zheng. 6276630Skib# 7276630Skib# Redistribution and use in source and binary forms, with or without 8276630Skib# modification, are permitted provided that the following conditions 9276630Skib# are met: 10276630Skib# 1. Redistributions of source code must retain the above copyright 11276630Skib# notice, this list of conditions and the following disclaimer. 12276630Skib# 2. Redistributions in binary form must reproduce the above copyright 13276630Skib# notice, this list of conditions and the following disclaimer in the 14276630Skib# documentation and/or other materials provided with the distribution. 15276630Skib# 16276630Skib# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17276630Skib# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18276630Skib# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19276630Skib# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20276630Skib# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21276630Skib# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22276630Skib# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23276630Skib# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24276630Skib# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25276630Skib# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26276630Skib# POSSIBILITY OF SUCH DAMAGE. 27276630Skib# 28276630Skib 29276630Skib 30276630Skibtsan_available_archs() 31276630Skib{ 32276630Skib atf_set "require.arch" "x86_64" 33276630Skib} 34276630Skib 35276630Skibatf_test_case lock_order_inversion 36276630Skiblock_order_inversion_head() { 37276630Skib atf_set "descr" "Test thread sanitizer for lock order inversion condition" 38276630Skib atf_set "require.progs" "c++ paxctl" 39276630Skib tsan_available_archs 40276630Skib} 41276630Skib 42276630Skibatf_test_case lock_order_inversion_profile 43276630Skiblock_order_inversion_profile_head() { 44276630Skib atf_set "descr" "Test thread sanitizer for lock order inversion with profiling option" 45276630Skib atf_set "require.progs" "c++ paxctl" 46276630Skib tsan_available_archs 47276630Skib} 48276630Skibatf_test_case lock_order_inversion_pic 49276630Skiblock_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} 54atf_test_case lock_order_inversion_pie 55lock_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 61lock_order_inversion_body(){ 62 cat > test.cc << EOF 63#include <pthread.h> 64 65pthread_mutex_t l1, l2; 66int 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} 80EOF 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 87lock_order_inversion_profile_body(){ 88 atf_expect_fail "PR toolchain/55760" 89 cat > test.cc << EOF 90#include <pthread.h> 91 92pthread_mutex_t l1, l2; 93int 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} 107EOF 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 114lock_order_inversion_pic_body(){ 115 cat > test.cc << EOF 116#include <stdio.h> 117#include <stdlib.h> 118int help(int); 119int main(int argc, char **argv) {return help(argc);} 120EOF 121 122 cat > pic.cc << EOF 123#include <pthread.h> 124 125pthread_mutex_t l1, l2; 126int 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} 140EOF 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} 149lock_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 158pthread_mutex_t l1, l2; 159int 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} 173EOF 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 180atf_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