Back to index

glibc  2.9
tst-makecontext2.c
Go to the documentation of this file.
00001 /* Copyright (C) 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 <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <ucontext.h>
00024 #include <tst-stack-align.h>
00025 
00026 ucontext_t ucp, ucp2;
00027 char st1[262144] __attribute__((aligned (16)));
00028 
00029 void
00030 cf (int i, int j)
00031 {
00032   if (i != 78 || j != 274)
00033     {
00034       printf ("i %d j %d\n", i, j);
00035       exit (1);
00036     }
00037   else if (TEST_STACK_ALIGN ())
00038     {
00039       puts ("insufficiently aligned stack");
00040       exit (2);
00041     }
00042 }
00043 
00044 int
00045 do_test (void)
00046 {
00047   for (size_t j = 32; j < 64; j += sizeof (long))
00048     {
00049       if (getcontext (&ucp) != 0)
00050        {
00051          if (errno == ENOSYS)
00052            {
00053              puts ("context handling not supported");
00054              return 0;
00055            }
00056 
00057          puts ("getcontext failed");
00058          return 1;
00059        }
00060       ucp.uc_link = &ucp2;
00061       ucp.uc_stack.ss_sp = st1;
00062       ucp.uc_stack.ss_size = sizeof (st1) - j;
00063       memset (&st1[sizeof (st1) - j], 0x55, j);
00064       makecontext (&ucp, (void (*) (void)) cf, 2, 78, 274);
00065       if (swapcontext (&ucp2, &ucp) != 0)
00066        {
00067          puts ("setcontext failed");
00068          return 1;
00069        }
00070 
00071       for (size_t i = j; i > 0; i--)
00072        if (st1[sizeof (st1) - j + i - 1] != 0x55)
00073          { printf ("fail %zd %zd\n", i, j); break; }
00074     }
00075 
00076   return 0;
00077 }
00078 
00079 #define TEST_FUNCTION do_test ()
00080 #include "../test-skeleton.c"