Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
did I mention that I hate blank lines?
[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 volatile 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   FILE *f;
39   char result[1024];
40   int sksize = 32768;
41   char *skbuf = (char *) malloc(sksize * 2 + 2 * sizeof(union alltypes));
42   if (skbuf == NULL)
43     exit(1);
44   for (int 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       sprintf(result, "(skaddr)+(sksize)-%d,(sksize)-%d", sizeof(union alltypes), sizeof(union alltypes));
63     else
64       strcpy(result, "(skaddr)+(sksize),(sksize)");
65   } else {
66     /* stack was placed into higher area */
67     if (*(skaddr + sksize * 2) != 'A')
68       sprintf(result, "(skaddr),(sksize)-%d", sizeof(union alltypes));
69     else
70       strcpy(result, "(skaddr),(sksize)");
71   }
72   if ((f = fopen("conftestval", "w")) == NULL)
73     exit(1);
74   fprintf(f, "%s\n", result);
75   fclose(f);
76   exit(0);
77   return 1;
78 }