Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a9c4797c0b35d6b273d17967dded9182e31793ed
[simgrid.git] / tools / cmake / test_prog / prog_stacksetup.c
1 /* Copyright (c) 2010, 2014-2016. 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   for (int i = 0; i < 1024; i++)
30     garbage[i] = 'X';
31   handler_addr = (char *) &dummy;
32   swapcontext(&uc_handler, &uc_main);
33   return;
34 }
35
36 int main(int argc, char *argv[])
37 {
38   int sksize = 32768;
39   char *skbuf = (char *) malloc(sksize * 2 + 2 * sizeof(union alltypes));
40   if (skbuf == NULL)
41     exit(1);
42   for (int i = 0; i < sksize * 2 + 2 * sizeof(union alltypes); i++)
43     skbuf[i] = 'A';
44   char *skaddr = skbuf + sizeof(union alltypes);
45
46   if (getcontext(&uc_handler) != 0)
47     exit(1);
48   uc_handler.uc_link = NULL;
49   uc_handler.uc_stack.ss_sp = (void *) (skaddr + sksize);
50   uc_handler.uc_stack.ss_size = sksize;
51   uc_handler.uc_stack.ss_flags = 0;
52   makecontext(&uc_handler, handler, 0);
53   swapcontext(&uc_main, &uc_handler);
54
55   if (handler_addr == (char *) 0xDEAD)
56     exit(1);
57   if (handler_addr < skaddr + sksize) {
58     /* stack was placed into lower area */
59     if (*(skaddr + sksize) != 'A')
60       printf("(skaddr)+(sksize)-%d;(sksize)-%d", sizeof(union alltypes), sizeof(union alltypes));
61     else
62       printf("(skaddr)+(sksize);(sksize)");
63   } else {
64     /* stack was placed into higher area */
65     if (*(skaddr + sksize * 2) != 'A')
66       printf("(skaddr);(sksize)-%d", sizeof(union alltypes));
67     else
68       printf("(skaddr);(sksize)");
69   }
70   return 0;
71 }