Home | History | Annotate | Line # | Download | only in c++
      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 tsan_available_archs()
     30 {
     31 	atf_set "require.arch" "x86_64"
     32 }
     33 
     34 atf_test_case signal_errno
     35 signal_errno_head() {
     36 	atf_set "descr" "Test thread sanitizer for errno modification in signal condition"
     37 	atf_set "require.progs" "c++ paxctl"
     38 	tsan_available_archs
     39 }
     40 
     41 atf_test_case signal_errno_profile
     42 signal_errno_profile_head() {
     43 	atf_set "descr" "Test thread sanitizer for errno modification in signal with profiling option"
     44 	atf_set "require.progs" "c++ paxctl"
     45 	tsan_available_archs
     46 }
     47 atf_test_case signal_errno_pic
     48 signal_errno_pic_head() {
     49 	atf_set "descr" "Test thread sanitizer for errno modification in signal with position independent code (PIC) flag"
     50 	atf_set "require.progs" "c++ paxctl"
     51 	tsan_available_archs
     52 }
     53 atf_test_case signal_errno_pie
     54 signal_errno_pie_head() {
     55 	atf_set "descr" "Test thread sanitizer for errno modification in signal with position independent execution (PIE) flag"
     56 	atf_set "require.progs" "c++ paxctl"
     57 	tsan_available_archs
     58 }
     59 
     60 signal_errno_body(){
     61 	cat > test.cc << EOF
     62 #include <pthread.h>
     63 #include <signal.h>
     64 #include <stdlib.h>
     65 #include <errno.h>
     66 
     67 pthread_t mainth;
     68 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
     69 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
     70 int main() {
     71   mainth = pthread_self();
     72   struct sigaction act = {};
     73   act.sa_sigaction = &MyHandler;
     74   sigaction(SIGPROF, &act, 0);
     75   pthread_t th;
     76   pthread_create(&th, 0, sendsignal, 0);
     77   pthread_join(th, 0);
     78   return 0;
     79 }
     80 EOF
     81 
     82 	c++ -fsanitize=thread -o test test.cc
     83 	paxctl +a test
     84 	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
     85 }
     86 
     87 signal_errno_profile_body(){
     88 	atf_expect_fail "PR toolchain/55760"
     89 	cat > test.cc << EOF
     90 #include <pthread.h>
     91 #include <signal.h>
     92 #include <stdlib.h>
     93 #include <errno.h>
     94 
     95 pthread_t mainth;
     96 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
     97 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
     98 int main() {
     99   mainth = pthread_self();
    100   struct sigaction act = {};
    101   act.sa_sigaction = &MyHandler;
    102   sigaction(SIGPROF, &act, 0);
    103   pthread_t th;
    104   pthread_create(&th, 0, sendsignal, 0);
    105   pthread_join(th, 0);
    106   return 0;
    107 }
    108 EOF
    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: signal handler spoils errno" ./test
    113 }
    114 
    115 signal_errno_pic_body(){
    116 	cat > test.cc << EOF
    117 #include <stdio.h>
    118 #include <stdlib.h>
    119 int help(int);
    120 int main(int argc, char **argv) {return help(argc);}
    121 EOF
    122 
    123 	cat > pic.cc << EOF
    124 #include <pthread.h>
    125 #include <signal.h>
    126 #include <stdlib.h>
    127 #include <errno.h>
    128 
    129 pthread_t mainth;
    130 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
    131 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
    132 int help(int argc) {
    133   mainth = pthread_self();
    134   struct sigaction act = {};
    135   act.sa_sigaction = &MyHandler;
    136   sigaction(SIGPROF, &act, 0);
    137   pthread_t th;
    138   pthread_create(&th, 0, sendsignal, 0);
    139   pthread_join(th, 0);
    140   return 0;
    141 }
    142 EOF
    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: signal handler spoils errno" ./test
    150 }
    151 signal_errno_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 <signal.h>
    160 #include <stdlib.h>
    161 #include <errno.h>
    162 
    163 pthread_t mainth;
    164 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
    165 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
    166 int main() {
    167   mainth = pthread_self();
    168   struct sigaction act = {};
    169   act.sa_sigaction = &MyHandler;
    170   sigaction(SIGPROF, &act, 0);
    171   pthread_t th;
    172   pthread_create(&th, 0, sendsignal, 0);
    173   pthread_join(th, 0);
    174   return 0;
    175 }
    176 EOF
    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: signal handler spoils errno" ./test
    181 }
    182 
    183 atf_init_test_cases()
    184 {
    185 	atf_add_test_case signal_errno
    186 	atf_add_test_case signal_errno_profile
    187 	atf_add_test_case signal_errno_pie
    188 	atf_add_test_case signal_errno_pic
    189 }
    190