Handle invalid buffer pointers when count is zero.
[glibc/history.git] / nptl / tst-context1.c
blobfcbeadeea03fe316125995594735c80af0c1cdb9
1 /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <limits.h>
23 #include <pthread.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ucontext.h>
28 #define N 4
29 #if __WORDSIZE == 64
30 #define GUARD_PATTERN 0xdeadbeafdeadbeaf
31 #else
32 #define GUARD_PATTERN 0xdeadbeaf
33 #endif
35 typedef struct {
36 ucontext_t uctx;
37 unsigned long guard[3];
38 } tst_context_t;
40 static char stacks[N][2 * PTHREAD_STACK_MIN];
41 static tst_context_t ctx[N][2];
42 static volatile int failures;
45 static void
46 fct (long int n)
48 char on_stack[1];
50 /* Just to use the thread local descriptor. */
51 printf ("%ld: in %s now, on_stack = %p\n", n, __FUNCTION__, on_stack);
52 errno = 0;
54 if (ctx[n][1].uctx.uc_link != &ctx[n][0].uctx)
56 printf ("context[%ld][1] uc_link damaged, = %p\n", n,
57 ctx[n][1].uctx.uc_link);
58 exit (1);
61 if ((ctx[n][0].guard[0] != GUARD_PATTERN)
62 || (ctx[n][0].guard[1] != GUARD_PATTERN)
63 || (ctx[n][0].guard[2] != GUARD_PATTERN))
65 printf ("%ld: %s context[0] overflow detected!\n", n, __FUNCTION__);
66 ++failures;
69 if ((ctx[n][1].guard[0] != GUARD_PATTERN)
70 || (ctx[n][1].guard[1] != GUARD_PATTERN)
71 || (ctx[n][1].guard[2] != GUARD_PATTERN))
73 printf ("%ld: %s context[1] overflow detected!\n", n, __FUNCTION__);
74 ++failures;
77 if (n < 0 || n >= N)
79 printf ("%ld out of range\n", n);
80 exit (1);
83 if (on_stack < stacks[n] || on_stack >= stacks[n] + sizeof (stacks[0]))
85 printf ("%ld: on_stack not on appropriate stack\n", n);
86 exit (1);
91 static void *
92 tf (void *arg)
94 int n = (int) (long int) arg;
96 ctx[n][0].guard[0] = GUARD_PATTERN;
97 ctx[n][0].guard[1] = GUARD_PATTERN;
98 ctx[n][0].guard[2] = GUARD_PATTERN;
100 ctx[n][1].guard[0] = GUARD_PATTERN;
101 ctx[n][1].guard[1] = GUARD_PATTERN;
102 ctx[n][1].guard[2] = GUARD_PATTERN;
104 if (getcontext (&ctx[n][1].uctx) != 0)
106 printf ("%d: cannot get context: %m\n", n);
107 exit (1);
110 printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
112 ctx[n][1].uctx.uc_stack.ss_sp = stacks[n];
113 ctx[n][1].uctx.uc_stack.ss_size = sizeof (stacks[n]);
114 ctx[n][1].uctx.uc_link = &ctx[n][0].uctx;
115 makecontext (&ctx[n][1].uctx, (void (*) (void)) fct, 1, (long int) n);
117 printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
119 if (swapcontext (&ctx[n][0].uctx, &ctx[n][1].uctx) != 0)
121 ++failures;
122 printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
124 else
125 printf ("%d: back in %s\n", n, __FUNCTION__);
127 return NULL;
131 static volatile int global;
134 static int
135 do_test (void)
137 int n;
138 pthread_t th[N];
139 ucontext_t mctx;
141 puts ("making contexts");
142 if (getcontext (&mctx) != 0)
144 if (errno == ENOSYS)
146 puts ("context handling not supported");
147 exit (0);
150 printf ("%s: getcontext: %m\n", __FUNCTION__);
151 exit (1);
154 /* Play some tricks with this context. */
155 if (++global == 1)
156 if (setcontext (&mctx) != 0)
158 puts ("setcontext failed");
159 exit (1);
161 if (global != 2)
163 puts ("global not incremented twice");
164 exit (1);
166 puts ("global OK");
168 pthread_attr_t at;
170 if (pthread_attr_init (&at) != 0)
172 puts ("attr_init failed");
173 return 1;
176 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
178 puts ("attr_setstacksize failed");
179 return 1;
182 for (n = 0; n < N; ++n)
183 if (pthread_create (&th[n], &at, tf, (void *) (long int) n) != 0)
185 puts ("create failed");
186 exit (1);
189 if (pthread_attr_destroy (&at) != 0)
191 puts ("attr_destroy failed");
192 return 1;
195 for (n = 0; n < N; ++n)
196 if (pthread_join (th[n], NULL) != 0)
198 puts ("join failed");
199 exit (1);
202 return failures;
205 #define TEST_FUNCTION do_test ()
206 #include "../test-skeleton.c"