Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8bffd64f21855bf5c7fde83275dc249f0a7ed9ee
[simgrid.git] / buildtools / Cmake / test_prog / prog_stacksetup.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #if defined OSX
8 #define _XOPEN_SOURCE
9 #endif
10
11 #ifdef _XBT_WIN32
12         #include "win32_ucontext.h"
13         #include "win32_ucontext.c"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #if defined(TEST_sigstack) || defined(TEST_sigaltstack)
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #endif
24
25 #if defined(TEST_makecontext)
26 #ifdef _XBT_WIN32
27         #include "win32_ucontext.h"
28 #else
29         #include <ucontext.h>
30 #endif
31
32 #endif
33 union alltypes {
34     long   l;
35     double d;
36     void  *vp;
37     void (*fp)(void);
38     char  *cp;
39 };
40 static volatile char *handler_addr = (char *)0xDEAD;
41 #if defined(TEST_sigstack) || defined(TEST_sigaltstack)
42 static volatile int handler_done = 0;
43 void handler(int sig)
44 {
45     char garbage[1024];
46     int i;
47     auto int dummy;
48     for (i = 0; i < 1024; i++)
49         garbage[i] = 'X';
50     handler_addr = (char *)&dummy;
51     handler_done = 1;
52     return;
53 }
54 #endif
55 #if defined(TEST_makecontext)
56 static ucontext_t uc_handler;
57 static ucontext_t uc_main;
58 void handler(void)
59 {
60     char garbage[1024];
61     int i;
62     auto int dummy;
63     for (i = 0; i < 1024; i++)
64         garbage[i] = 'X';
65     handler_addr = (char *)&dummy;
66     swapcontext(&uc_handler, &uc_main);
67     return;
68 }
69 #endif
70 int main(int argc, char *argv[])
71 {
72     FILE *f;
73     char *skaddr;
74     char *skbuf;
75     int sksize;
76     char result[1024];
77     int i;
78     sksize = 32768;
79     skbuf = (char *)malloc(sksize*2+2*sizeof(union alltypes));
80     if (skbuf == NULL)
81         exit(1);
82     for (i = 0; i < sksize*2+2*sizeof(union alltypes); i++)
83         skbuf[i] = 'A';
84     skaddr = skbuf+sizeof(union alltypes);
85 #if defined(TEST_sigstack) || defined(TEST_sigaltstack)
86     {
87         struct sigaction sa;
88 #if defined(TEST_sigstack)
89         struct sigstack ss;
90 #elif defined(TEST_sigaltstack) && defined(HAVE_STACK_T)
91         stack_t ss;
92 #else
93         struct sigaltstack ss;
94 #endif
95 #if defined(TEST_sigstack)
96         ss.ss_sp      = (void *)(skaddr + sksize);
97         ss.ss_onstack = 0;
98         if (sigstack(&ss, NULL) < 0)
99             exit(1);
100 #elif defined(TEST_sigaltstack)
101         ss.ss_sp    = (void *)(skaddr + sksize);
102         ss.ss_size  = sksize;
103         ss.ss_flags = 0;
104         if (sigaltstack(&ss, NULL) < 0)
105             exit(1);
106 #endif
107         memset((void *)&sa, 0, sizeof(struct sigaction));
108         sa.sa_handler = handler;
109         sa.sa_flags = SA_ONSTACK;
110         sigemptyset(&sa.sa_mask);
111         sigaction(SIGUSR1, &sa, NULL);
112         kill(getpid(), SIGUSR1);
113         while (!handler_done)
114             /*nop*/;
115     }
116 #endif
117 #if defined(TEST_makecontext)
118     {
119         if (getcontext(&uc_handler) != 0)
120             exit(1);
121         uc_handler.uc_link = NULL;
122         uc_handler.uc_stack.ss_sp    = (void *)(skaddr + sksize);
123         uc_handler.uc_stack.ss_size  = sksize;
124         uc_handler.uc_stack.ss_flags = 0;
125         makecontext(&uc_handler, handler, 0);
126         swapcontext(&uc_main, &uc_handler);
127     }
128 #endif
129     if (handler_addr == (char *)0xDEAD)
130         exit(1);
131     if (handler_addr < skaddr+sksize) {
132         /* stack was placed into lower area */
133         if (*(skaddr+sksize) != 'A')
134              sprintf(result, "(skaddr)+(sksize)-%d,(sksize)-%d",
135                      sizeof(union alltypes), sizeof(union alltypes));
136         else
137              strcpy(result, "(skaddr)+(sksize),(sksize)");
138     }
139     else {
140         /* stack was placed into higher area */
141         if (*(skaddr+sksize*2) != 'A')
142             sprintf(result, "(skaddr),(sksize)-%d", sizeof(union alltypes));
143         else
144             strcpy(result, "(skaddr),(sksize)");
145     }
146     if ((f = fopen("conftestval", "w")) == NULL)
147         exit(1);
148     fprintf(f, "%s\n", result);
149     fclose(f);
150     exit(0);
151         return 1;
152 }