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