Remove TLS configure tests.
[platform/upstream/linaro-glibc.git] / nptl / tst-context1.c
1 /* Copyright (C) 2003-2014 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <error.h>
21 #include <limits.h>
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <ucontext.h>
26
27 #define N       4
28 #if __WORDSIZE == 64
29 #define GUARD_PATTERN 0xdeadbeafdeadbeaf
30 #else
31 #define GUARD_PATTERN 0xdeadbeaf
32 #endif
33
34 typedef struct {
35        ucontext_t uctx;
36        unsigned long    guard[3];
37    } tst_context_t;
38
39 static char stacks[N][2 * PTHREAD_STACK_MIN];
40 static tst_context_t ctx[N][2];
41 static volatile int failures;
42
43
44 static void
45 fct (long int n)
46 {
47   char on_stack[1];
48
49   /* Just to use the thread local descriptor.  */
50   printf ("%ld: in %s now, on_stack = %p\n", n, __FUNCTION__, on_stack);
51   errno = 0;
52
53   if (ctx[n][1].uctx.uc_link != &ctx[n][0].uctx)
54     {
55       printf ("context[%ld][1] uc_link damaged, = %p\n", n,
56               ctx[n][1].uctx.uc_link);
57       exit (1);
58     }
59
60   if ((ctx[n][0].guard[0] != GUARD_PATTERN)
61   ||  (ctx[n][0].guard[1] != GUARD_PATTERN)
62   ||  (ctx[n][0].guard[2] != GUARD_PATTERN))
63     {
64       printf ("%ld: %s context[0] overflow detected!\n", n, __FUNCTION__);
65       ++failures;
66     }
67
68   if ((ctx[n][1].guard[0] != GUARD_PATTERN)
69   ||  (ctx[n][1].guard[1] != GUARD_PATTERN)
70   ||  (ctx[n][1].guard[2] != GUARD_PATTERN))
71     {
72       printf ("%ld: %s context[1] overflow detected!\n", n, __FUNCTION__);
73       ++failures;
74     }
75
76   if (n < 0 || n >= N)
77     {
78       printf ("%ld out of range\n", n);
79       exit (1);
80     }
81
82   if (on_stack < stacks[n] || on_stack >= stacks[n] + sizeof (stacks[0]))
83     {
84       printf ("%ld: on_stack not on appropriate stack\n", n);
85       exit (1);
86     }
87 }
88
89
90 static void *
91 tf (void *arg)
92 {
93   int n = (int) (long int) arg;
94
95   ctx[n][0].guard[0] = GUARD_PATTERN;
96   ctx[n][0].guard[1] = GUARD_PATTERN;
97   ctx[n][0].guard[2] = GUARD_PATTERN;
98
99   ctx[n][1].guard[0] = GUARD_PATTERN;
100   ctx[n][1].guard[1] = GUARD_PATTERN;
101   ctx[n][1].guard[2] = GUARD_PATTERN;
102
103   if (getcontext (&ctx[n][1].uctx) != 0)
104     {
105       printf ("%d: cannot get context: %m\n", n);
106       exit (1);
107     }
108
109   printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
110
111   ctx[n][1].uctx.uc_stack.ss_sp = stacks[n];
112   ctx[n][1].uctx.uc_stack.ss_size = sizeof (stacks[n]);
113   ctx[n][1].uctx.uc_link = &ctx[n][0].uctx;
114   makecontext (&ctx[n][1].uctx, (void (*) (void)) fct, 1, (long int) n);
115
116   printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
117
118   if (swapcontext (&ctx[n][0].uctx, &ctx[n][1].uctx) != 0)
119     {
120       ++failures;
121       printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
122     }
123   else
124     printf ("%d: back in %s\n", n, __FUNCTION__);
125
126   return NULL;
127 }
128
129
130 static volatile int global;
131
132
133 static int
134 do_test (void)
135 {
136   int n;
137   pthread_t th[N];
138   ucontext_t mctx;
139
140   puts ("making contexts");
141   if (getcontext (&mctx) != 0)
142     {
143       if (errno == ENOSYS)
144         {
145           puts ("context handling not supported");
146           exit (0);
147         }
148
149       printf ("%s: getcontext: %m\n", __FUNCTION__);
150       exit (1);
151     }
152
153   /* Play some tricks with this context.  */
154   if (++global == 1)
155     if (setcontext (&mctx) != 0)
156       {
157         puts ("setcontext failed");
158         exit (1);
159       }
160   if (global != 2)
161     {
162       puts ("global not incremented twice");
163       exit (1);
164     }
165   puts ("global OK");
166
167   pthread_attr_t at;
168
169   if (pthread_attr_init (&at) != 0)
170     {
171       puts ("attr_init failed");
172       return 1;
173     }
174
175   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
176     {
177       puts ("attr_setstacksize failed");
178       return 1;
179     }
180
181   for (n = 0; n < N; ++n)
182     if (pthread_create (&th[n], &at, tf, (void *) (long int) n) != 0)
183       {
184         puts ("create failed");
185         exit (1);
186       }
187
188   if (pthread_attr_destroy (&at) != 0)
189     {
190       puts ("attr_destroy failed");
191       return 1;
192     }
193
194   for (n = 0; n < N; ++n)
195     if (pthread_join (th[n], NULL) != 0)
196       {
197         puts ("join failed");
198         exit (1);
199       }
200
201   return failures;
202 }
203
204 #define TEST_FUNCTION do_test ()
205 #include "../test-skeleton.c"