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