summaryrefslogtreecommitdiff
path: root/tests/common.h
blob: ffe113a5e3ad5e48dd3582d7f8bbb2c962554c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* Copyright (C) 2004-2020 Free Software Foundation, Inc.
   This snippet is taken from debug/tst-chk1 in the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#define _POSIX_C_SOURCE 2
#define _XOPEN_SOURCE 700

#include <setjmp.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>

#ifndef _FORTIFY_HEADERS_H
#error Never included fortify-headers.h
#endif

volatile int chk_fail_ok;
volatile int ret;
jmp_buf chk_fail_buf;

static void
handler (int sig)
{
  if (chk_fail_ok)
    {
      chk_fail_ok = 0;
      longjmp (chk_fail_buf, 1);
    }
  else
    _exit (127);
}

void
__attribute__((constructor))
set_fortify_handler (void)
{
  struct sigaction sa;

  sa.sa_handler = handler;
  sa.sa_flags = 0;
  sigemptyset (&sa.sa_mask);

  sigaction (SIGILL, &sa, NULL);
  sigaction (SIGTRAP, &sa, NULL);
}

#define FAIL() \
  do { fprintf (stderr, "Failure in %s:%d\n", __FILE__, __LINE__); ret = 1; } while (0)
#define CHK_FAIL_START \
  chk_fail_ok = 1;                              \
  if (! setjmp (chk_fail_buf))                  \
    {
#define CHK_FAIL_END \
      chk_fail_ok = 0;                          \
      FAIL ();                                  \
    }