t_tsan_signal_errno.sh revision 1.4
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
29tsan_available_archs()
30{
31	atf_set "require.arch" "x86_64"
32}
33
34atf_test_case signal_errno
35signal_errno_head() {
36	atf_set "descr" "Test thread sanitizer for errno modification in signal condition"
37	atf_set "require.progs" "cc paxctl"
38	tsan_available_archs
39}
40
41atf_test_case signal_errno_profile
42signal_errno_profile_head() {
43	atf_set "descr" "Test thread sanitizer for errno modification in signal with profiling option"
44	atf_set "require.progs" "cc paxctl"
45	tsan_available_archs
46}
47atf_test_case signal_errno_pic
48signal_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" "cc paxctl"
51	tsan_available_archs
52}
53atf_test_case signal_errno_pie
54signal_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" "cc paxctl"
57	tsan_available_archs
58}
59
60signal_errno_body(){
61	cat > test.c << EOF
62#include <pthread.h>
63#include <signal.h>
64#include <stdlib.h>
65#include <errno.h>
66
67pthread_t mainth;
68static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
69static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
70int 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}
80EOF
81
82	cc -fsanitize=thread -o test test.c
83	paxctl +a test
84	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
85}
86
87signal_errno_profile_body(){
88	cat > test.c << EOF
89#include <pthread.h>
90#include <signal.h>
91#include <stdlib.h>
92#include <errno.h>
93
94pthread_t mainth;
95static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
96static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
97int main() {
98  mainth = pthread_self();
99  struct sigaction act = {};
100  act.sa_sigaction = &MyHandler;
101  sigaction(SIGPROF, &act, 0);
102  pthread_t th;
103  pthread_create(&th, 0, sendsignal, 0);
104  pthread_join(th, 0);
105  return 0;
106}
107EOF
108
109	cc -fsanitize=thread -o test -pg test.c
110	paxctl +a test
111	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
112}
113
114signal_errno_pic_body(){
115	cat > test.c << EOF
116#include <stdio.h>
117#include <stdlib.h>
118int help(int);
119int main(int argc, char **argv) {return help(argc);}
120EOF
121
122	cat > pic.c << EOF
123#include <pthread.h>
124#include <signal.h>
125#include <stdlib.h>
126#include <errno.h>
127
128pthread_t mainth;
129static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
130static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
131int help(int argc) {
132  mainth = pthread_self();
133  struct sigaction act = {};
134  act.sa_sigaction = &MyHandler;
135  sigaction(SIGPROF, &act, 0);
136  pthread_t th;
137  pthread_create(&th, 0, sendsignal, 0);
138  pthread_join(th, 0);
139  return 0;
140}
141EOF
142
143	cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
144	cc -o test test.c -fsanitize=thread -L. -ltest
145	paxctl +a test
146
147	export LD_LIBRARY_PATH=.
148	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
149}
150signal_errno_pie_body(){
151	
152	#check whether -pie flag is supported on this architecture
153	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then 
154		atf_set_skip "cc -pie not supported on this architecture"
155	fi
156	cat > test.c << EOF
157#include <pthread.h>
158#include <signal.h>
159#include <stdlib.h>
160#include <errno.h>
161
162pthread_t mainth;
163static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
164static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
165int main() {
166  mainth = pthread_self();
167  struct sigaction act = {};
168  act.sa_sigaction = &MyHandler;
169  sigaction(SIGPROF, &act, 0);
170  pthread_t th;
171  pthread_create(&th, 0, sendsignal, 0);
172  pthread_join(th, 0);
173  return 0;
174}
175EOF
176
177	cc -fsanitize=thread -o test -fpie -pie test.c
178	paxctl +a test
179	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
180}
181
182atf_init_test_cases()
183{
184	atf_add_test_case signal_errno
185	atf_add_test_case signal_errno_profile
186	atf_add_test_case signal_errno_pie
187	atf_add_test_case signal_errno_pic
188}
189