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 29test_target() 30{ 31 SUPPORT='n' 32 if uname -m | grep -q "amd64" && command -v cc >/dev/null 2>&1 && \ 33 ! echo __clang__ | cc -E - | grep -q __clang__; then 34 # only clang with major version newer than 7 is supported 35 CLANG_MAJOR=`echo __clang_major__ | cc -E - | grep -o '^[[:digit:]]'` 36 if [ "$CLANG_MAJOR" -ge "7" ]; then 37 SUPPORT='y' 38 fi 39 fi 40} 41 42atf_test_case locked_mutex_destroy 43locked_mutex_destroy_head() { 44 atf_set "descr" "Test thread sanitizer for destroying locked mutex condition" 45 atf_set "require.progs" "cc paxctl" 46} 47 48atf_test_case locked_mutex_destroy_profile 49locked_mutex_destroy_profile_head() { 50 atf_set "descr" "Test thread sanitizer for destroying locked mutex with profiling option" 51 atf_set "require.progs" "cc paxctl" 52} 53atf_test_case locked_mutex_destroy_pic 54locked_mutex_destroy_pic_head() { 55 atf_set "descr" "Test thread sanitizer for destroying locked mutex with position independent code (PIC) flag" 56 atf_set "require.progs" "cc paxctl" 57} 58atf_test_case locked_mutex_destroy_pie 59locked_mutex_destroy_pie_head() { 60 atf_set "descr" "Test thread sanitizer for destroying locked mutex with position independent execution (PIE) flag" 61 atf_set "require.progs" "cc paxctl" 62} 63 64locked_mutex_destroy_body(){ 65 cat > test.c << EOF 66#include <pthread.h> 67#include <stdlib.h> 68 69pthread_mutex_t mutex; 70pthread_barrier_t barrier; 71void *Thread(void *a) { 72 pthread_mutex_lock(&mutex); 73 pthread_barrier_wait(&barrier); 74 return 0; 75} 76 77int main() { 78 pthread_t t; 79 pthread_barrier_init(&barrier, NULL, 2); 80 pthread_mutex_init(&mutex, NULL); 81 pthread_create(&t, NULL, Thread, NULL); 82 pthread_barrier_wait(&barrier); 83 pthread_mutex_destroy(&mutex); 84 pthread_join(t, NULL); 85 return 0; 86} 87EOF 88 89 cc -fsanitize=thread -o test test.c 90 paxctl +a test 91 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test 92} 93 94locked_mutex_destroy_profile_body(){ 95 cat > test.c << EOF 96#include <pthread.h> 97#include <stdlib.h> 98 99pthread_mutex_t mutex; 100pthread_barrier_t barrier; 101void *Thread(void *a) { 102 pthread_mutex_lock(&mutex); 103 pthread_barrier_wait(&barrier); 104 return 0; 105} 106 107int main() { 108 pthread_t t; 109 pthread_barrier_init(&barrier, NULL, 2); 110 pthread_mutex_init(&mutex, NULL); 111 pthread_create(&t, NULL, Thread, NULL); 112 pthread_barrier_wait(&barrier); 113 pthread_mutex_destroy(&mutex); 114 pthread_join(t, NULL); 115 return 0; 116} 117EOF 118 119 cc -fsanitize=thread -o test -pg test.c 120 paxctl +a test 121 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test 122} 123 124locked_mutex_destroy_pic_body(){ 125 cat > test.c << EOF 126#include <stdio.h> 127#include <stdlib.h> 128int help(int); 129int main(int argc, char **argv) {return help(argc);} 130EOF 131 132 cat > pic.c << EOF 133#include <pthread.h> 134#include <stdlib.h> 135 136pthread_mutex_t mutex; 137pthread_barrier_t barrier; 138void *Thread(void *a) { 139 pthread_mutex_lock(&mutex); 140 pthread_barrier_wait(&barrier); 141 return 0; 142} 143 144int help(int argc) { 145 pthread_t t; 146 pthread_barrier_init(&barrier, NULL, 2); 147 pthread_mutex_init(&mutex, NULL); 148 pthread_create(&t, NULL, Thread, NULL); 149 pthread_barrier_wait(&barrier); 150 pthread_mutex_destroy(&mutex); 151 pthread_join(t, NULL); 152 return 0; 153} 154EOF 155 156 cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c 157 cc -o test test.c -fsanitize=thread -L. -ltest 158 paxctl +a test 159 160 export LD_LIBRARY_PATH=. 161 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test 162} 163locked_mutex_destroy_pie_body(){ 164 165 #check whether -pie flag is supported on this architecture 166 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 167 atf_set_skip "cc -pie not supported on this architecture" 168 fi 169 cat > test.c << EOF 170#include <pthread.h> 171#include <stdlib.h> 172 173pthread_mutex_t mutex; 174pthread_barrier_t barrier; 175void *Thread(void *a) { 176 pthread_mutex_lock(&mutex); 177 pthread_barrier_wait(&barrier); 178 return 0; 179} 180 181int main() { 182 pthread_t t; 183 pthread_barrier_init(&barrier, NULL, 2); 184 pthread_mutex_init(&mutex, NULL); 185 pthread_create(&t, NULL, Thread, NULL); 186 pthread_barrier_wait(&barrier); 187 pthread_mutex_destroy(&mutex); 188 pthread_join(t, NULL); 189 return 0; 190} 191EOF 192 193 cc -fsanitize=thread -o test -fpie -pie test.c 194 paxctl +a test 195 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: destroy of a locked mutex" ./test 196} 197 198 199atf_test_case target_not_supported 200target_not_supported_head() 201{ 202 atf_set "descr" "Test forced skip" 203} 204 205target_not_supported_body() 206{ 207 atf_skip "Target is not supported" 208} 209 210atf_init_test_cases() 211{ 212 test_target 213 test $SUPPORT = 'n' && { 214 atf_add_test_case target_not_supported 215 return 0 216 } 217 atf_add_test_case locked_mutex_destroy 218 atf_add_test_case locked_mutex_destroy_profile 219 atf_add_test_case locked_mutex_destroy_pie 220 atf_add_test_case locked_mutex_destroy_pic 221} 222