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	atf_expect_fail "PR toolchain/55760"
89	cat > test.c << EOF
90#include <pthread.h>
91#include <signal.h>
92#include <stdlib.h>
93#include <errno.h>
94
95pthread_t mainth;
96static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
97static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
98int 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}
108EOF
109
110	cc -fsanitize=thread -static -o test -pg test.c
111	paxctl +a test
112	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
113}
114
115signal_errno_pic_body(){
116	cat > test.c << EOF
117#include <stdio.h>
118#include <stdlib.h>
119int help(int);
120int main(int argc, char **argv) {return help(argc);}
121EOF
122
123	cat > pic.c << EOF
124#include <pthread.h>
125#include <signal.h>
126#include <stdlib.h>
127#include <errno.h>
128
129pthread_t mainth;
130static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
131static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
132int 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}
142EOF
143
144	cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
145	cc -o test test.c -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}
151signal_errno_pie_body(){
152
153	#check whether -pie flag is supported on this architecture
154	if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
155		atf_set_skip "cc -pie not supported on this architecture"
156	fi
157	cat > test.c << EOF
158#include <pthread.h>
159#include <signal.h>
160#include <stdlib.h>
161#include <errno.h>
162
163pthread_t mainth;
164static void MyHandler(int a, siginfo_t *s, void *c) { errno = 1; }
165static void* sendsignal(void *p) { pthread_kill(mainth, SIGPROF); return NULL; }
166int 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}
176EOF
177
178	cc -fsanitize=thread -o test -fpie -pie test.c
179	paxctl +a test
180	atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: signal handler spoils errno" ./test
181}
182
183atf_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