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 atf_set "require.arch" "x86_64" 32} 33 34atf_test_case thread_leak 35thread_leak_head() { 36 atf_set "descr" "Test thread sanitizer for thread leak condition" 37 atf_set "require.progs" "c++ paxctl" 38 test_target 39} 40 41atf_test_case thread_leak_profile 42thread_leak_profile_head() { 43 atf_set "descr" "Test thread sanitizer for thread leak with profiling option" 44 atf_set "require.progs" "c++ paxctl" 45 test_target 46} 47atf_test_case thread_leak_pic 48thread_leak_pic_head() { 49 atf_set "descr" "Test thread sanitizer for thread leak with position independent code (PIC) flag" 50 atf_set "require.progs" "c++ paxctl" 51 test_target 52} 53atf_test_case thread_leak_pie 54thread_leak_pie_head() { 55 atf_set "descr" "Test thread sanitizer for thread leak with position independent execution (PIE) flag" 56 atf_set "require.progs" "c++ paxctl" 57 test_target 58} 59 60thread_leak_body(){ 61 cat > test.cc << EOF 62#include <pthread.h> 63#include <unistd.h> 64 65int GlobalData; 66pthread_barrier_t barrier; 67void *Thread(void *a) { 68 pthread_barrier_wait(&barrier); 69 return 0; 70} 71 72int main() { 73 pthread_t t; 74 pthread_barrier_init(&barrier, NULL, 2); 75 pthread_create(&t, NULL, Thread, NULL); 76 pthread_barrier_wait(&barrier); 77 sleep(1); 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: thread leak" ./test 85} 86 87thread_leak_profile_body(){ 88 atf_expect_fail "PR toolchain/55760" 89 cat > test.cc << EOF 90#include <pthread.h> 91#include <unistd.h> 92 93int GlobalData; 94pthread_barrier_t barrier; 95void *Thread(void *a) { 96 pthread_barrier_wait(&barrier); 97 return 0; 98} 99 100int main() { 101 pthread_t t; 102 pthread_barrier_init(&barrier, NULL, 2); 103 pthread_create(&t, NULL, Thread, NULL); 104 pthread_barrier_wait(&barrier); 105 sleep(1); 106 return 0; 107} 108EOF 109 110 c++ -fsanitize=thread -static -o test -pg test.cc 111 paxctl +a test 112 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 113} 114 115thread_leak_pic_body(){ 116 cat > test.cc << EOF 117#include <stdio.h> 118#include <stdlib.h> 119int help(int); 120int main(int argc, char **argv) {return help(argc);} 121EOF 122 123 cat > pic.cc << EOF 124#include <pthread.h> 125#include <unistd.h> 126 127int GlobalData; 128pthread_barrier_t barrier; 129void *Thread(void *a) { 130 pthread_barrier_wait(&barrier); 131 return 0; 132} 133 134int help(int argc) { 135 pthread_t t; 136 pthread_barrier_init(&barrier, NULL, 2); 137 pthread_create(&t, NULL, Thread, NULL); 138 pthread_barrier_wait(&barrier); 139 sleep(1); 140 return 0; 141} 142EOF 143 144 c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc 145 c++ -o test test.cc -fsanitize=thread -L. -ltest 146 paxctl +a test 147 148 export LD_LIBRARY_PATH=. 149 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 150} 151thread_leak_pie_body(){ 152 153 #check whether -pie flag is supported on this architecture 154 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 155 atf_set_skip "c++ -pie not supported on this architecture" 156 fi 157 cat > test.cc << EOF 158#include <pthread.h> 159#include <unistd.h> 160 161int GlobalData; 162pthread_barrier_t barrier; 163void *Thread(void *a) { 164 pthread_barrier_wait(&barrier); 165 return 0; 166} 167 168int main() { 169 pthread_t t; 170 pthread_barrier_init(&barrier, NULL, 2); 171 pthread_create(&t, NULL, Thread, NULL); 172 pthread_barrier_wait(&barrier); 173 sleep(1); 174 return 0; 175} 176EOF 177 178 c++ -fsanitize=thread -o test -fpie -pie test.cc 179 paxctl +a test 180 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test 181} 182 183 184atf_test_case target_not_supported 185target_not_supported_head() 186{ 187 atf_set "descr" "Test forced skip" 188} 189 190target_not_supported_body() 191{ 192 atf_skip "Target is not supported" 193} 194 195atf_init_test_cases() 196{ 197 atf_add_test_case thread_leak 198 atf_add_test_case thread_leak_profile 199 atf_add_test_case thread_leak_pie 200 atf_add_test_case thread_leak_pic 201} 202