t_tsan_data_race.sh revision 1.2
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 c++ >/dev/null 2>&1 && \ 33 ! echo __clang__ | c++ -E - | grep -q __clang__; then 34 # only clang with major version newer than 7 is supported 35 CLANG_MAJOR=`echo __clang_major__ | c++ -E - | grep -o '^[[:digit:]]'` 36 if [ "$CLANG_MAJOR" -ge "7" ]; then 37 SUPPORT='y' 38 fi 39 fi 40} 41 42atf_test_case data_race 43data_race_head() { 44 atf_set "descr" "Test thread sanitizer for data race condition" 45 atf_set "require.progs" "c++ paxctl" 46} 47 48atf_test_case data_race_profile 49data_race_profile_head() { 50 atf_set "descr" "Test thread sanitizer for data race with profiling option" 51 atf_set "require.progs" "c++ paxctl" 52} 53atf_test_case data_race_pic 54data_race_pic_head() { 55 atf_set "descr" "Test thread sanitizer for data race with position independent code (PIC) flag" 56 atf_set "require.progs" "c++ paxctl" 57} 58atf_test_case data_race_pie 59data_race_pie_head() { 60 atf_set "descr" "Test thread sanitizer for data race with position independent execution (PIE) flag" 61 atf_set "require.progs" "c++ paxctl" 62} 63 64data_race_body(){ 65 cat > test.cc << EOF 66#include <pthread.h> 67int GlobalData; pthread_barrier_t barrier; 68void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; } 69int main() { 70 pthread_t t; 71 pthread_barrier_init(&barrier, NULL, 2); 72 pthread_create(&t, NULL, Thread, NULL); 73 pthread_barrier_wait(&barrier); 74 GlobalData = 43; 75 pthread_join(t, NULL); 76 return 0; 77} 78EOF 79 80 c++ -fsanitize=thread -o test test.cc 81 paxctl +a test 82 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test 83} 84 85data_race_profile_body(){ 86 cat > test.cc << EOF 87#include <pthread.h> 88int GlobalData; pthread_barrier_t barrier; 89void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; } 90int main() { 91 pthread_t t; 92 pthread_barrier_init(&barrier, NULL, 2); 93 pthread_create(&t, NULL, Thread, NULL); 94 pthread_barrier_wait(&barrier); 95 GlobalData = 43; 96 pthread_join(t, NULL); 97 return 0; 98} 99EOF 100 101 c++ -fsanitize=thread -o test -pg test.cc 102 paxctl +a test 103 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test 104} 105 106data_race_pic_body(){ 107 cat > test.cc << EOF 108#include <stdio.h> 109#include <stdlib.h> 110int help(int); 111int main(int argc, char **argv) {return help(argc);} 112EOF 113 114 cat > pic.cc << EOF 115#include <pthread.h> 116int GlobalData; pthread_barrier_t barrier; 117void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; } 118int help(int argc) { 119 pthread_t t; 120 pthread_barrier_init(&barrier, NULL, 2); 121 pthread_create(&t, NULL, Thread, NULL); 122 pthread_barrier_wait(&barrier); 123 GlobalData = 43; 124 pthread_join(t, NULL); 125 return 0; 126} 127EOF 128 129 c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc 130 c++ -o test test.cc -fsanitize=thread -L. -ltest 131 paxctl +a test 132 133 export LD_LIBRARY_PATH=. 134 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test 135} 136data_race_pie_body(){ 137 138 #check whether -pie flag is supported on this architecture 139 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 140 atf_set_skip "c++ -pie not supported on this architecture" 141 fi 142 cat > test.cc << EOF 143#include <pthread.h> 144int GlobalData; pthread_barrier_t barrier; 145void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; } 146int main() { 147 pthread_t t; 148 pthread_barrier_init(&barrier, NULL, 2); 149 pthread_create(&t, NULL, Thread, NULL); 150 pthread_barrier_wait(&barrier); 151 GlobalData = 43; 152 pthread_join(t, NULL); 153 return 0; 154} 155EOF 156 157 c++ -fsanitize=thread -o test -fpie -pie test.cc 158 paxctl +a test 159 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test 160} 161 162 163atf_test_case target_not_supported 164target_not_supported_head() 165{ 166 atf_set "descr" "Test forced skip" 167} 168 169target_not_supported_body() 170{ 171 atf_skip "Target is not supported" 172} 173 174atf_init_test_cases() 175{ 176 test_target 177 test $SUPPORT = 'n' && { 178 atf_add_test_case target_not_supported 179 return 0 180 } 181 atf_add_test_case data_race 182 atf_add_test_case data_race_profile 183 atf_add_test_case data_race_pie 184 atf_add_test_case data_race_pic 185} 186