Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / tools / cmake / test_prog / prog_stacksetup.c
1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifdef __APPLE__
7 #define _XOPEN_SOURCE 700
8 #endif
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ucontext.h>
14
15 union alltypes {
16   long l;
17   double d;
18   void *vp;
19   void (*fp) (void);
20   char *cp;
21 };
22 static char *handler_addr = (char *) 0xDEAD;
23 static ucontext_t uc_handler;
24 static ucontext_t uc_main;
25 void handler(void)
26 {
27   char garbage[1024];
28   auto int dummy;
29   int i;
30   for (i = 0; i < 1024; i++)
31     garbage[i] = 'X';
32   handler_addr = (char *) &dummy;
33   swapcontext(&uc_handler, &uc_main);
34   return;
35 }
36
37 int main(int argc, char *argv[])
38 {
39   int sksize = 32768;
40   char *skbuf = (char *) malloc(sksize * 2 + 2 * sizeof(union alltypes));
41   if (skbuf == NULL)
42     exit(1);
43   int i;
44   for (i = 0; i < sksize * 2 + 2 * sizeof(union alltypes); i++)
45     skbuf[i] = 'A';
46   char *skaddr = skbuf + sizeof(union alltypes);
47
48   if (getcontext(&uc_handler) != 0)
49     exit(1);
50   uc_handler.uc_link = NULL;
51   uc_handler.uc_stack.ss_sp = (void *) (skaddr + sksize);
52   uc_handler.uc_stack.ss_size = sksize;
53   uc_handler.uc_stack.ss_flags = 0;
54   makecontext(&uc_handler, handler, 0);
55   swapcontext(&uc_main, &uc_handler);
56
57   if (handler_addr == (char *) 0xDEAD)
58     exit(1);
59   if (handler_addr < skaddr + sksize) {
60     /* stack was placed into lower area */
61     if (*(skaddr + sksize) != 'A')
62       printf("(skaddr)+(sksize)-%zu;(sksize)-%zu", sizeof(union alltypes), sizeof(union alltypes));
63     else
64       printf("(skaddr)+(sksize);(sksize)");
65   } else {
66     /* stack was placed into higher area */
67     if (*(skaddr + sksize * 2) != 'A')
68       printf("(skaddr);(sksize)-%zu", sizeof(union alltypes));
69     else
70       printf("(skaddr);(sksize)");
71   }
72   return 0;
73 }