Add to git.
[pthrlib.git] / src / test_context.c
1 /* Test context switching.
2  * Copyright (C) 2001-2003 Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_context.c,v 1.4 2003/02/05 22:13:33 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <assert.h>
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #ifdef HAVE_SETJMP_H
31 #include <setjmp.h>
32 #endif
33
34 #include "pthr_context.h"
35
36 #define STACK_SIZE 4096
37 #define MARKER_SIZE 16
38
39 static unsigned char stack [MARKER_SIZE+STACK_SIZE+MARKER_SIZE];
40 static mctx_t ctx, calling_ctx;
41 static int called = 0;
42 static void fn (void *);
43 static void setjmp_test (void *);
44 static jmp_buf jb;
45
46 #define DATA ((void *) 0x12546731)
47
48 int
49 main ()
50 {
51   int i;
52
53   /* Place magic numbers into the marker areas at each end of the stack, so
54    * we can detect stack overrun.
55    */
56   memset (stack, 0xeb, MARKER_SIZE);
57   memset (stack + MARKER_SIZE+STACK_SIZE, 0xeb, MARKER_SIZE);
58   mctx_set (&ctx, fn, DATA, stack + MARKER_SIZE, STACK_SIZE);
59   mctx_switch (&calling_ctx, &ctx);
60   assert (called == 1);
61
62   /* Verify the ends of the stack haven't been overrun. */
63   for (i = 0; i < MARKER_SIZE; ++i)
64     assert (stack[i] == 0xeb && stack[i + MARKER_SIZE+STACK_SIZE] == 0xeb);
65
66   /* Check setjmp/longjmp work on the alternate stack. This was found to be
67    * a problem on Solaris.
68    */
69   mctx_set (&ctx, setjmp_test, DATA, stack + MARKER_SIZE, STACK_SIZE);
70   mctx_switch (&calling_ctx, &ctx);
71
72   exit (0);
73 }
74
75 static void
76 fn (void *data)
77 {
78   assert (data == DATA);
79   called = 1;
80   mctx_restore (&calling_ctx);
81   abort ();
82 }
83
84 static void call_longjmp (void);
85
86 static void
87 setjmp_test (void *data)
88 {
89   if (setjmp (jb) == 0)
90     call_longjmp ();
91
92   mctx_restore (&calling_ctx);
93 }
94
95 static void
96 call_longjmp ()
97 {
98   longjmp (jb, 1);
99 }