summaryrefslogtreecommitdiff
path: root/libc-parts/t-setjmp.c
blob: 4334cd9278028d2374e91f73b6bfcf1aa2418ef7 (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
#define RM_INTERN

#include "setjmp.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

char *program_name = "t-setjmp";

static jmp_buf buf;

static int
recur (int a, int b)
{
  if (a + b == 0)
    _longjmp (buf, 23);

  return recur (a - 1, b);
}

static int
crc (uintptr_t *p, int count)
{
  int v = 0;
  while (count)
    {
      v += *p;
      p ++;
      count --;
    }

  return v;
}

int
main (int argc, char *argv[])
{
  /* Put some data on the stack that the compile will not be able to
     optimize away.  */
  char string[128];
  snprintf (string, sizeof (string), "%s", argv[0]);
  int c = crc ((uintptr_t *) string, sizeof (string) / sizeof (uintptr_t));

  int ret = _setjmp (buf);
  if (ret == 0)
    /* First return.  */
    {
      recur (4, 5);
    }

  assert (ret == 23);
  assert (c == crc ((uintptr_t *) string,
		    sizeof (string) / sizeof (uintptr_t)));

  return 0;
}