t_tsan_vptr_race.sh revision 1.7
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 29 30test_target() 31{ 32 atf_set "require.arch" "x86_64" 33} 34 35atf_test_case vptr_race 36vptr_race_head() { 37 atf_set "descr" "Test thread sanitizer for vptr race condition" 38 atf_set "require.progs" "c++ paxctl" 39 test_target 40} 41 42atf_test_case vptr_race_profile 43vptr_race_profile_head() { 44 atf_set "descr" "Test thread sanitizer for vptr race with profiling option" 45 atf_set "require.progs" "c++ paxctl" 46 test_target 47} 48atf_test_case vptr_race_pic 49vptr_race_pic_head() { 50 atf_set "descr" "Test thread sanitizer for vptr race with position independent code (PIC) flag" 51 atf_set "require.progs" "c++ paxctl" 52 test_target 53} 54atf_test_case vptr_race_pie 55vptr_race_pie_head() { 56 atf_set "descr" "Test thread sanitizer for vptr race with position independent execution (PIE) flag" 57 atf_set "require.progs" "c++ paxctl" 58 test_target 59} 60 61vptr_race_body(){ 62 cat > test.cc << EOF 63#include <pthread.h> 64pthread_barrier_t barrier; 65struct A { 66 volatile bool done; 67 A(): done(false) { } 68 virtual void Done() { done = true; } 69 virtual ~A() { while (!done) ; } 70}; 71struct B: A {}; 72A *obj = new B; 73void *Thread1(void *x) { 74 pthread_barrier_wait(&barrier); 75 obj->Done(); 76 return NULL; 77} 78int main() { 79 pthread_barrier_init(&barrier, NULL, 2); 80 pthread_t t; 81 pthread_create(&t, NULL, Thread1, NULL); 82 pthread_barrier_wait(&barrier); 83 delete obj; 84 pthread_join(t, NULL); 85 return 0; 86} 87EOF 88 89 c++ -fsanitize=thread -o test test.cc 90 paxctl +a test 91 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test 92} 93 94vptr_race_profile_body(){ 95 atf_expect_fail "PR toolchain/55760" 96 cat > test.cc << EOF 97#include <pthread.h> 98pthread_barrier_t barrier; 99struct A { 100 volatile bool done; 101 A(): done(false) { } 102 virtual void Done() { done = true; } 103 virtual ~A() { while (!done) ; } 104}; 105struct B: A {}; 106A *obj = new B; 107void *Thread1(void *x) { 108 pthread_barrier_wait(&barrier); 109 obj->Done(); 110 return NULL; 111} 112int main() { 113 pthread_barrier_init(&barrier, NULL, 2); 114 pthread_t t; 115 pthread_create(&t, NULL, Thread1, NULL); 116 pthread_barrier_wait(&barrier); 117 delete obj; 118 pthread_join(t, NULL); 119 return 0; 120} 121EOF 122 123 c++ -fsanitize=thread -static -o test -pg test.cc 124 paxctl +a test 125 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test 126} 127 128vptr_race_pic_body(){ 129 cat > test.cc << EOF 130#include <stdio.h> 131#include <stdlib.h> 132int help(int); 133int main(int argc, char **argv) {return help(argc);} 134EOF 135 136 cat > pic.cc << EOF 137#include <pthread.h> 138pthread_barrier_t barrier; 139struct A { 140 volatile bool done; 141 A(): done(false) { } 142 virtual void Done() { done = true; } 143 virtual ~A() { while (!done) ; } 144}; 145struct B: A {}; 146A *obj = new B; 147void *Thread1(void *x) { 148 pthread_barrier_wait(&barrier); 149 obj->Done(); 150 return NULL; 151} 152int help(int argc) { 153 pthread_barrier_init(&barrier, NULL, 2); 154 pthread_t t; 155 pthread_create(&t, NULL, Thread1, NULL); 156 pthread_barrier_wait(&barrier); 157 delete obj; 158 pthread_join(t, NULL); 159 return 0; 160} 161EOF 162 163 c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc 164 c++ -o test test.cc -fsanitize=thread -L. -ltest 165 paxctl +a test 166 167 export LD_LIBRARY_PATH=. 168 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test 169} 170vptr_race_pie_body(){ 171 172 #check whether -pie flag is supported on this architecture 173 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 174 atf_set_skip "c++ -pie not supported on this architecture" 175 fi 176 cat > test.cc << EOF 177#include <pthread.h> 178pthread_barrier_t barrier; 179struct A { 180 volatile bool done; 181 A(): done(false) { } 182 virtual void Done() { done = true; } 183 virtual ~A() { while (!done) ; } 184}; 185struct B: A {}; 186A *obj = new B; 187void *Thread1(void *x) { 188 pthread_barrier_wait(&barrier); 189 obj->Done(); 190 return NULL; 191} 192int main() { 193 pthread_barrier_init(&barrier, NULL, 2); 194 pthread_t t; 195 pthread_create(&t, NULL, Thread1, NULL); 196 pthread_barrier_wait(&barrier); 197 delete obj; 198 pthread_join(t, NULL); 199 return 0; 200} 201EOF 202 203 c++ -fsanitize=thread -o test -fpie -pie test.cc 204 paxctl +a test 205 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race on vptr \(ctor/dtor vs virtual call\)" ./test 206} 207 208 209atf_test_case target_not_supported 210target_not_supported_head() 211{ 212 atf_set "descr" "Test forced skip" 213} 214 215target_not_supported_body() 216{ 217 atf_skip "Target is not supported" 218} 219 220atf_init_test_cases() 221{ 222 atf_add_test_case vptr_race 223 atf_add_test_case vptr_race_profile 224 atf_add_test_case vptr_race_pie 225 atf_add_test_case vptr_race_pic 226} 227