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
29test_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
46atf_test_case signal_errno
47signal_errno_head() {
48	atf_set "descr" "Test thread sanitizer for errno modification in signal condition"
49	atf_set "require.progs" "cc paxctl"
50}
51
52atf_test_case signal_errno_profile
53signal_errno_profile_head() {
54	atf_set "descr" "Test thread sanitizer for errno modification in signal with profiling option"
55	atf_set "require.progs" "cc paxctl"
56}
57atf_test_case signal_errno_pic
58signal_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" "cc paxctl"
61}
62atf_test_case signal_errno_pie
63signal_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" "cc paxctl"
66}
67
68signal_errno_body(){
69	cat > test.c << EOF
70#include <pthread.h>
71#include <signal.h>
72#include <stdlib.h>
73#include <errno.h>
74
75pthread_t mainth;
76static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
77static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
78int 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}
88EOF
89
90	cc -fsanitize=thread -o test test.c
91	paxctl +a test
92	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
93}
94
95signal_errno_profile_body(){
96	cat > test.c << EOF
97#include <pthread.h>
98#include <signal.h>
99#include <stdlib.h>
100#include <errno.h>
101
102pthread_t mainth;
103static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
104static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
105int 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}
115EOF
116
117	cc -fsanitize=thread -o test -pg test.c
118	paxctl +a test
119	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
120}
121
122signal_errno_pic_body(){
123	cat > test.c << EOF
124#include <stdio.h>
125#include <stdlib.h>
126int help(int);
127int main(int argc, char **argv) {return help(argc);}
128EOF
129
130	cat > pic.c << EOF
131#include <pthread.h>
132#include <signal.h>
133#include <stdlib.h>
134#include <errno.h>
135
136pthread_t mainth;
137static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
138static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
139int 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}
149EOF
150
151	cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
152	cc -o test test.c -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}
158signal_errno_pie_body(){
159	
160	#check whether -pie flag is supported on this architecture
161	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
162		atf_set_skip "cc -pie not supported on this architecture"
163	fi
164	cat > test.c << EOF
165#include <pthread.h>
166#include <signal.h>
167#include <stdlib.h>
168#include <errno.h>
169
170pthread_t mainth;
171static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
172static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
173int 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}
183EOF
184
185	cc -fsanitize=thread -o test -fpie -pie test.c
186	paxctl +a test
187	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
188}
189
190
191atf_test_case target_not_supported
192target_not_supported_head()
193{
194	atf_set "descr" "Test forced skip"
195}
196
197target_not_supported_body()
198{
199	atf_skip "Target is not supported"
200}
201
202atf_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