1 /* Tests of signal delivery on an alternate stack (nonlethal).
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/xsignal.h>
20 #include <support/support.h>
21 #include <support/check.h>
23 /* C2011 7.4.1.1p5 specifies that only the following operations are
24 guaranteed to be well-defined inside an asynchronous signal handler:
25 * any operation on a lock-free atomic object
26 * assigning a value to an object declared as volatile sig_atomic_t
27 * calling abort, _Exit, quick_exit, or signal
28 * signal may only be called with its first argument equal to the
29 number of the signal that caused the handler to be called
31 We use this list as a guideline for the set of operations that ought
32 also to be safe in a _synchronous_ signal delivered on an alternate
33 signal stack with only MINSIGSTKSZ bytes of space.
35 This test program tests all of the above operations that do not,
36 one way or another, cause the program to be terminated. */
38 /* We do not try to test atomic operations exhaustively, only a simple
39 atomic counter increment. This is only safe if atomic_[u]int is
40 unconditionally lock-free. */
41 #ifdef __STDC_NO_ATOMICS__
42 # define TEST_ATOMIC_OPS 0
44 # include <stdatomic.h>
45 # if ATOMIC_INT_LOCK_FREE != 2
46 # define TEST_ATOMIC_OPS 0
48 # define TEST_ATOMIC_OPS 1
52 static volatile sig_atomic_t signal_flag
= 0;
53 static volatile sig_atomic_t signal_err
= 0;
55 handler_set_flag (int unused
)
61 handler_set_flag_once (int sig
)
64 if (signal (sig
, SIG_IGN
) == SIG_ERR
)
65 /* It is not safe to call FAIL_EXIT1 here. Set another flag instead. */
70 static atomic_uint signal_count
= 0;
72 handler_count_up_1 (int unused
)
74 atomic_fetch_add (&signal_count
, 1);
81 void *sstk
= xalloc_sigstack (0);
84 /* Test 1: setting a volatile sig_atomic_t flag. */
85 sa
.sa_handler
= handler_set_flag
;
86 sa
.sa_flags
= SA_RESTART
| SA_ONSTACK
;
87 sigfillset (&sa
.sa_mask
);
88 if (sigaction (SIGUSR1
, &sa
, 0))
89 FAIL_EXIT1 ("sigaction (SIGUSR1, handler_set_flag): %m\n");
91 TEST_VERIFY_EXIT (signal_flag
== 0);
93 TEST_VERIFY_EXIT (signal_flag
== 1);
96 TEST_VERIFY_EXIT (signal_flag
== 1);
99 /* Test 1: setting a volatile sig_atomic_t flag and then ignoring
100 further delivery of the signal. */
101 sa
.sa_handler
= handler_set_flag_once
;
102 if (sigaction (SIGUSR1
, &sa
, 0))
103 FAIL_EXIT1 ("sigaction (SIGUSR1, handler_set_flag_once): %m\n");
106 TEST_VERIFY_EXIT (signal_flag
== 1);
107 /* Note: if signal_err is 1, a system call failed, but we can't
108 report the error code because errno is indeterminate. */
109 TEST_VERIFY_EXIT (signal_err
== 0);
113 TEST_VERIFY_EXIT (signal_flag
== 0);
114 TEST_VERIFY_EXIT (signal_err
== 0);
117 sa
.sa_handler
= handler_count_up_1
;
118 if (sigaction (SIGUSR1
, &sa
, 0))
119 FAIL_EXIT1 ("sigaction (SIGUSR1, handler_count_up_1): %m\n");
122 TEST_VERIFY_EXIT (atomic_load (&signal_count
) == 1);
124 TEST_VERIFY_EXIT (atomic_load (&signal_count
) == 2);
127 xfree_sigstack (sstk
);
131 #include <support/test-driver.c>