Back to index

glibc  2.9
tst-setcontext.c
Go to the documentation of this file.
00001 /* Copyright (C) 2001,2002,2004,2006,2007,2008 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <ucontext.h>
00024 #include <unistd.h>
00025 
00026 static ucontext_t ctx[3];
00027 
00028 static int was_in_f1;
00029 static int was_in_f2;
00030 
00031 static char st2[32768];
00032 
00033 static void
00034 f1 (int a0, int a1, int a2, int a3)
00035 {
00036   printf ("start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
00037 
00038   if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
00039     {
00040       puts ("arg mismatch");
00041       exit (-1);
00042     }
00043 
00044   if (swapcontext (&ctx[1], &ctx[2]) != 0)
00045     {
00046       printf ("%s: swapcontext: %m\n", __FUNCTION__);
00047       exit (1);
00048     }
00049   puts ("finish f1");
00050   was_in_f1 = 1;
00051 }
00052 
00053 static void
00054 f2 (void)
00055 {
00056   char on_stack[1];
00057 
00058   puts ("start f2");
00059 
00060   printf ("&on_stack=%p\n", on_stack);
00061   if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
00062     {
00063       printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
00064       exit (1);
00065     }
00066 
00067   if (swapcontext (&ctx[2], &ctx[1]) != 0)
00068     {
00069       printf ("%s: swapcontext: %m\n", __FUNCTION__);
00070       exit (1);
00071     }
00072   puts ("finish f2");
00073   was_in_f2 = 1;
00074 }
00075 
00076 void
00077 test_stack(volatile int a, volatile int b,
00078            volatile int c, volatile int d)
00079 {
00080   volatile int e = 5;
00081   volatile int f = 6;
00082   ucontext_t uc;
00083 
00084   /* Test for cases where getcontext is clobbering the callers
00085      stack, including parameters.  */
00086   getcontext(&uc);
00087 
00088   if (a != 1)
00089     {
00090       printf ("%s: getcontext clobbers parm a\n", __FUNCTION__);
00091       exit (1);
00092     }
00093 
00094   if (b != 2)
00095     {
00096       printf ("%s: getcontext clobbers parm b\n", __FUNCTION__);
00097       exit (1);
00098     }
00099 
00100   if (c != 3)
00101     {
00102       printf ("%s: getcontext clobbers parm c\n", __FUNCTION__);
00103       exit (1);
00104     }
00105 
00106   if (d != 4)
00107     {
00108       printf ("%s: getcontext clobbers parm d\n", __FUNCTION__);
00109       exit (1);
00110     }
00111 
00112   if (e != 5)
00113     {
00114       printf ("%s: getcontext clobbers varible e\n", __FUNCTION__);
00115       exit (1);
00116     }
00117 
00118   if (f != 6)
00119     {
00120       printf ("%s: getcontext clobbers variable f\n", __FUNCTION__);
00121       exit (1);
00122     }
00123 }
00124 
00125 volatile int global;
00126 
00127 
00128 static int back_in_main;
00129 
00130 
00131 static void
00132 check_called (void)
00133 {
00134   if (back_in_main == 0)
00135     {
00136       puts ("program did no reach main again");
00137       _exit (1);
00138     }
00139 }
00140 
00141 
00142 int
00143 main (void)
00144 {
00145   atexit (check_called);
00146 
00147   char st1[32768];
00148 
00149   puts ("making contexts");
00150   if (getcontext (&ctx[1]) != 0)
00151     {
00152       if (errno == ENOSYS)
00153        {
00154          back_in_main = 1;
00155          exit (0);
00156        }
00157 
00158       printf ("%s: getcontext: %m\n", __FUNCTION__);
00159       exit (1);
00160     }
00161 
00162   test_stack (1, 2, 3, 4);
00163 
00164   /* Play some tricks with this context.  */
00165   if (++global == 1)
00166     if (setcontext (&ctx[1]) != 0)
00167       {
00168        printf ("%s: setcontext: %m\n", __FUNCTION__);
00169        exit (1);
00170       }
00171   if (global != 2)
00172     {
00173       printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
00174       exit (1);
00175     }
00176 
00177   ctx[1].uc_stack.ss_sp = st1;
00178   ctx[1].uc_stack.ss_size = sizeof st1;
00179   ctx[1].uc_link = &ctx[0];
00180   {
00181     ucontext_t tempctx = ctx[1];
00182     makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
00183 
00184     /* Without this check, a stub makecontext can make us spin forever.  */
00185     if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
00186       {
00187        puts ("makecontext was a no-op, presuming not implemented");
00188        return 0;
00189       }
00190   }
00191 
00192   if (getcontext (&ctx[2]) != 0)
00193     {
00194       printf ("%s: second getcontext: %m\n", __FUNCTION__);
00195       exit (1);
00196     }
00197   ctx[2].uc_stack.ss_sp = st2;
00198   ctx[2].uc_stack.ss_size = sizeof st2;
00199   ctx[2].uc_link = &ctx[1];
00200   makecontext (&ctx[2], f2, 0);
00201 
00202   puts ("swapping contexts");
00203   if (swapcontext (&ctx[0], &ctx[2]) != 0)
00204     {
00205       printf ("%s: swapcontext: %m\n", __FUNCTION__);
00206       exit (1);
00207     }
00208   puts ("back at main program");
00209   back_in_main = 1;
00210 
00211   if (was_in_f1 == 0)
00212     {
00213       puts ("didn't reach f1");
00214       exit (1);
00215     }
00216   if (was_in_f2 == 0)
00217     {
00218       puts ("didn't reach f2");
00219       exit (1);
00220     }
00221 
00222   puts ("test succeeded");
00223   return 0;
00224 }