summaryrefslogtreecommitdiff
path: root/tests/common.h
diff options
context:
space:
mode:
Diffstat (limited to 'tests/common.h')
-rw-r--r--tests/common.h66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/common.h b/tests/common.h
new file mode 100644
index 0000000..9567653
--- /dev/null
+++ b/tests/common.h
@@ -0,0 +1,66 @@
1/* Copyright (C) 2004-2020 Free Software Foundation, Inc.
2 This snippet is taken from debug/tst-chk1 in the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
17
18#define _POSIX_C_SOURCE 2
19#define _XOPEN_SOURCE 700
20
21#include <setjmp.h>
22#include <unistd.h>
23#include <signal.h>
24#include <stdio.h>
25
26volatile int chk_fail_ok;
27volatile int ret;
28jmp_buf chk_fail_buf;
29
30static void
31handler (int sig)
32{
33 if (chk_fail_ok)
34 {
35 chk_fail_ok = 0;
36 longjmp (chk_fail_buf, 1);
37 }
38 else
39 _exit (127);
40}
41
42void
43__attribute__((constructor))
44set_fortify_handler (void)
45{
46 struct sigaction sa;
47
48 sa.sa_handler = handler;
49 sa.sa_flags = 0;
50 sigemptyset (&sa.sa_mask);
51
52 sigaction (SIGILL, &sa, NULL);
53 sigaction (SIGTRAP, &sa, NULL);
54}
55
56#define FAIL() \
57 do { fprintf (stderr, "Failure in %s:%d\n", __FILE__, __LINE__); ret = 1; } while (0)
58#define CHK_FAIL_START \
59 chk_fail_ok = 1; \
60 if (! setjmp (chk_fail_buf)) \
61 {
62#define CHK_FAIL_END \
63 chk_fail_ok = 0; \
64 FAIL (); \
65 }
66