t_tsan_thread_leak.sh revision 1.3
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 # Detect address space larger than 32 bits 33 maxaddress=`sysctl vm.maxaddress|awk '{print $3}'` 34 if [ $maxaddress -gt 4294967295 ]; then 35 if command -v cc >/dev/null 2>&1; then 36 if ! echo __clang__ | cc -E - | grep -q __clang__; then 37 SUPPORT='y' 38 elif ! cc -v 2>&1 | awk '/gcc version/{print $3}' | \ 39 awk -F '.' '($0+0) > 9 {exit 1}'; then 40 SUPPORT='y' 41 fi 42 fi 43 fi 44} 45 46atf_test_case thread_leak 47thread_leak_head() { 48 atf_set "descr" "Test thread sanitizer for thread leak condition" 49 atf_set "require.progs" "cc paxctl" 50} 51 52atf_test_case thread_leak_profile 53thread_leak_profile_head() { 54 atf_set "descr" "Test thread sanitizer for thread leak with profiling option" 55 atf_set "require.progs" "cc paxctl" 56} 57atf_test_case thread_leak_pic 58thread_leak_pic_head() { 59 atf_set "descr" "Test thread sanitizer for thread leak with position independent code (PIC) flag" 60 atf_set "require.progs" "cc paxctl" 61} 62atf_test_case thread_leak_pie 63thread_leak_pie_head() { 64 atf_set "descr" "Test thread sanitizer for thread leak with position independent execution (PIE) flag" 65 atf_set "require.progs" "cc paxctl" 66} 67 68thread_leak_body(){ 69 cat > test.c << EOF 70#include <pthread.h> 71#include <unistd.h> 72 73int GlobalData; 74pthread_barrier_t barrier; 75void *Thread(void *a) { 76 pthread_barrier_wait(&barrier); 77 return 0; 78} 79 80int main() { 81 pthread_t t; 82 pthread_barrier_init(&barrier, NULL, 2); 83 pthread_create(&t, NULL, Thread, NULL); 84 pthread_barrier_wait(&barrier); 85 sleep(1); 86 return 0; 87} 88EOF 89 90 cc -fsanitize=thread -o test test.c 91 paxctl +a test 92 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 93} 94 95thread_leak_profile_body(){ 96 cat > test.c << EOF 97#include <pthread.h> 98#include <unistd.h> 99 100int GlobalData; 101pthread_barrier_t barrier; 102void *Thread(void *a) { 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_create(&t, NULL, Thread, NULL); 111 pthread_barrier_wait(&barrier); 112 sleep(1); 113 return 0; 114} 115EOF 116 117 cc -fsanitize=thread -o test -pg test.c 118 paxctl +a test 119 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 120} 121 122thread_leak_pic_body(){ 123 cat > test.c << EOF 124#include <stdio.h> 125#include <stdlib.h> 126int help(int); 127int main(int argc, char **argv) {return help(argc);} 128EOF 129 130 cat > pic.c << EOF 131#include <pthread.h> 132#include <unistd.h> 133 134int GlobalData; 135pthread_barrier_t barrier; 136void *Thread(void *a) { 137 pthread_barrier_wait(&barrier); 138 return 0; 139} 140 141int help(int argc) { 142 pthread_t t; 143 pthread_barrier_init(&barrier, NULL, 2); 144 pthread_create(&t, NULL, Thread, NULL); 145 pthread_barrier_wait(&barrier); 146 sleep(1); 147 return 0; 148} 149EOF 150 151 cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c 152 cc -o test test.c -fsanitize=thread -L. -ltest 153 paxctl +a test 154 155 export LD_LIBRARY_PATH=. 156 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 157} 158thread_leak_pie_body(){ 159 160 #check whether -pie flag is supported on this architecture 161 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 162 atf_set_skip "cc -pie not supported on this architecture" 163 fi 164 cat > test.c << EOF 165#include <pthread.h> 166#include <unistd.h> 167 168int GlobalData; 169pthread_barrier_t barrier; 170void *Thread(void *a) { 171 pthread_barrier_wait(&barrier); 172 return 0; 173} 174 175int main() { 176 pthread_t t; 177 pthread_barrier_init(&barrier, NULL, 2); 178 pthread_create(&t, NULL, Thread, NULL); 179 pthread_barrier_wait(&barrier); 180 sleep(1); 181 return 0; 182} 183EOF 184 185 cc -fsanitize=thread -o test -fpie -pie test.c 186 paxctl +a test 187 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 188} 189 190 191atf_test_case target_not_supported 192target_not_supported_head() 193{ 194 atf_set "descr" "Test forced skip" 195} 196 197target_not_supported_body() 198{ 199 atf_skip "Target is not supported" 200} 201 202atf_init_test_cases() 203{ 204 test_target 205 test $SUPPORT = 'n' && { 206 atf_add_test_case target_not_supported 207 return 0 208 } 209 atf_add_test_case thread_leak 210 atf_add_test_case thread_leak_profile 211 atf_add_test_case thread_leak_pie 212 atf_add_test_case thread_leak_pic 213} 214