t_tsan_signal_errno.sh revision 1.1 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 test_target()
30 {
31 SUPPORT='n'
32 if uname -m | grep -q "amd64" && command -v cc >/dev/null 2>&1 && \
33 ! echo __clang__ | cc -E - | grep -q __clang__; then
34 # only clang with major version newer than 7 is supported
35 CLANG_MAJOR=`echo __clang_major__ | cc -E - | grep -o '^[[:digit:]]'`
36 if [ "$CLANG_MAJOR" -ge "7" ]; then
37 SUPPORT='y'
38 fi
39 fi
40 }
41
42 atf_test_case signal_errno
43 signal_errno_head() {
44 atf_set "descr" "Test thread sanitizer for errno modification in signal condition"
45 atf_set "require.progs" "cc paxctl"
46 }
47
48 atf_test_case signal_errno_profile
49 signal_errno_profile_head() {
50 atf_set "descr" "Test thread sanitizer for errno modification in signal with profiling option"
51 atf_set "require.progs" "cc paxctl"
52 }
53 atf_test_case signal_errno_pic
54 signal_errno_pic_head() {
55 atf_set "descr" "Test thread sanitizer for errno modification in signal with position independent code (PIC) flag"
56 atf_set "require.progs" "cc paxctl"
57 }
58 atf_test_case signal_errno_pie
59 signal_errno_pie_head() {
60 atf_set "descr" "Test thread sanitizer for errno modification in signal with position independent execution (PIE) flag"
61 atf_set "require.progs" "cc paxctl"
62 }
63
64 signal_errno_body(){
65 cat > test.c << EOF
66 #include <pthread.h>
67 #include <signal.h>
68 #include <stdlib.h>
69 #include <errno.h>
70
71 pthread_t mainth;
72 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
73 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
74 int main() {
75 mainth = pthread_self();
76 struct sigaction act = {};
77 act.sa_sigaction = &MyHandler;
78 sigaction(SIGPROF, &act, 0);
79 pthread_t th;
80 pthread_create(&th, 0, sendsignal, 0);
81 pthread_join(th, 0);
82 return 0;
83 }
84 EOF
85
86 cc -fsanitize=thread -o test test.c
87 paxctl +a test
88 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
89 }
90
91 signal_errno_profile_body(){
92 cat > test.c << EOF
93 #include <pthread.h>
94 #include <signal.h>
95 #include <stdlib.h>
96 #include <errno.h>
97
98 pthread_t mainth;
99 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
100 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
101 int main() {
102 mainth = pthread_self();
103 struct sigaction act = {};
104 act.sa_sigaction = &MyHandler;
105 sigaction(SIGPROF, &act, 0);
106 pthread_t th;
107 pthread_create(&th, 0, sendsignal, 0);
108 pthread_join(th, 0);
109 return 0;
110 }
111 EOF
112
113 cc -fsanitize=thread -o test -pg test.c
114 paxctl +a test
115 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
116 }
117
118 signal_errno_pic_body(){
119 cat > test.c << EOF
120 #include <stdio.h>
121 #include <stdlib.h>
122 int help(int);
123 int main(int argc, char **argv) {return help(argc);}
124 EOF
125
126 cat > pic.c << EOF
127 #include <pthread.h>
128 #include <signal.h>
129 #include <stdlib.h>
130 #include <errno.h>
131
132 pthread_t mainth;
133 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
134 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
135 int help(int argc) {
136 mainth = pthread_self();
137 struct sigaction act = {};
138 act.sa_sigaction = &MyHandler;
139 sigaction(SIGPROF, &act, 0);
140 pthread_t th;
141 pthread_create(&th, 0, sendsignal, 0);
142 pthread_join(th, 0);
143 return 0;
144 }
145 EOF
146
147 cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
148 cc -o test test.c -fsanitize=thread -L. -ltest
149 paxctl +a test
150
151 export LD_LIBRARY_PATH=.
152 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
153 }
154 signal_errno_pie_body(){
155
156 #check whether -pie flag is supported on this architecture
157 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
158 atf_set_skip "cc -pie not supported on this architecture"
159 fi
160 cat > test.c << EOF
161 #include <pthread.h>
162 #include <signal.h>
163 #include <stdlib.h>
164 #include <errno.h>
165
166 pthread_t mainth;
167 static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
168 static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
169 int main() {
170 mainth = pthread_self();
171 struct sigaction act = {};
172 act.sa_sigaction = &MyHandler;
173 sigaction(SIGPROF, &act, 0);
174 pthread_t th;
175 pthread_create(&th, 0, sendsignal, 0);
176 pthread_join(th, 0);
177 return 0;
178 }
179 EOF
180
181 cc -fsanitize=thread -o test -fpie -pie test.c
182 paxctl +a test
183 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
184 }
185
186
187 atf_test_case target_not_supported
188 target_not_supported_head()
189 {
190 atf_set "descr" "Test forced skip"
191 }
192
193 atf_init_test_cases()
194 {
195 test_target
196 test $SUPPORT = 'n' && {
197 atf_add_test_case target_not_supported
198 return 0
199 }
200 atf_add_test_case signal_errno
201 atf_add_test_case signal_errno_profile
202 atf_add_test_case signal_errno_pie
203 atf_add_test_case signal_errno_pic
204 }
205