(root)/
glibc-2.38/
sysdeps/
unix/
sysv/
linux/
x86/
tst-cet-setcontext-1.c
       1  /* Check getcontext and setcontext on the context from makecontext
       2     with shadow stack.
       3     Copyright (C) 2018-2023 Free Software Foundation, Inc.
       4     This file is part of the GNU C Library.
       5  
       6     The GNU C Library is free software; you can redistribute it and/or
       7     modify it under the terms of the GNU Lesser General Public
       8     License as published by the Free Software Foundation; either
       9     version 2.1 of the License, or (at your option) any later version.
      10  
      11     The GNU C Library is distributed in the hope that it will be useful,
      12     but WITHOUT ANY WARRANTY; without even the implied warranty of
      13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14     Lesser General Public License for more details.
      15  
      16     You should have received a copy of the GNU Lesser General Public
      17     License along with the GNU C Library; if not, see
      18     <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <stdio.h>
      21  #include <stdint.h>
      22  #include <stdlib.h>
      23  #include <ucontext.h>
      24  #include <unistd.h>
      25  #include <sys/mman.h>
      26  #include <stdatomic.h>
      27  #include <x86intrin.h>
      28  
      29  static ucontext_t ctx[5];
      30  static atomic_int done;
      31  
      32  static void
      33  __attribute__((noinline, noclone))
      34  f2 (void)
      35  {
      36    printf ("start f2\n");
      37    done++;
      38    if (setcontext (&ctx[2]) != 0)
      39      {
      40        printf ("%s: setcontext: %m\n", __FUNCTION__);
      41        exit (EXIT_FAILURE);
      42      }
      43  }
      44  
      45  static void
      46  f1 (void)
      47  {
      48    printf ("start f1\n");
      49    if (getcontext (&ctx[2]) != 0)
      50      {
      51        printf ("%s: getcontext: %m\n", __FUNCTION__);
      52        exit (EXIT_FAILURE);
      53      }
      54    if (done)
      55      exit (EXIT_SUCCESS);
      56    f2 ();
      57  }
      58  
      59  static int
      60  do_test (void)
      61  {
      62    char st1[32768];
      63    puts ("making contexts");
      64    if (getcontext (&ctx[0]) != 0)
      65      {
      66        printf ("%s: getcontext: %m\n", __FUNCTION__);
      67        exit (EXIT_FAILURE);
      68      }
      69    if (getcontext (&ctx[1]) != 0)
      70      {
      71        printf ("%s: getcontext: %m\n", __FUNCTION__);
      72        exit (EXIT_FAILURE);
      73      }
      74  
      75    ctx[3].uc_stack.ss_sp = st1;
      76    ctx[3].uc_stack.ss_size = sizeof st1;
      77    ctx[3].uc_link = &ctx[0];
      78    makecontext (&ctx[3], (void (*) (void)) f1, 0);
      79  
      80    ctx[1].uc_stack.ss_sp = st1;
      81    ctx[1].uc_stack.ss_size = sizeof st1;
      82    ctx[1].uc_link = &ctx[0];
      83    makecontext (&ctx[1], (void (*) (void)) f1, 0);
      84  
      85    ctx[4].uc_stack.ss_sp = st1;
      86    ctx[4].uc_stack.ss_size = sizeof st1;
      87    ctx[4].uc_link = &ctx[0];
      88    makecontext (&ctx[4], (void (*) (void)) f1, 0);
      89  
      90    /* NB: When shadow stack is enabled, makecontext calls arch_prctl
      91       with ARCH_CET_ALLOC_SHSTK to allocate a new shadow stack which
      92       can be unmapped.  The base address and size of the new shadow
      93       stack are returned in __ssp[1] and __ssp[2].  makecontext is
      94       called for CTX1, CTX3 and CTX4.  But only CTX1 is used.  New
      95       shadow stacks are allocated in the order of CTX3, CTX1, CTX4.
      96       It is very likely that CTX1's shadow stack is placed between
      97       CTX3 and CTX4.  We munmap CTX3's and CTX4's shadow stacks to
      98       create gaps above and below CTX1's shadow stack.  We check
      99       that setcontext CTX1 works correctly in this case.  */
     100    if (_get_ssp () != 0)
     101      {
     102        if (ctx[3].__ssp[1] != 0
     103  	  && munmap ((void *) (uintptr_t) ctx[3].__ssp[1],
     104  		     (size_t) ctx[3].__ssp[2]) != 0)
     105  	{
     106  	  printf ("%s: munmap: %m\n", __FUNCTION__);
     107  	  exit (EXIT_FAILURE);
     108  	}
     109  
     110        if (ctx[4].__ssp[1] != 0
     111  	  && munmap ((void *) (uintptr_t) ctx[4].__ssp[1],
     112  		     (size_t) ctx[4].__ssp[2]) != 0)
     113  	{
     114  	  printf ("%s: munmap: %m\n", __FUNCTION__);
     115  	  exit (EXIT_FAILURE);
     116  	}
     117      }
     118  
     119    if (setcontext (&ctx[1]) != 0)
     120      {
     121        printf ("%s: setcontext: %m\n", __FUNCTION__);
     122        exit (EXIT_FAILURE);
     123      }
     124    exit (EXIT_FAILURE);
     125  }
     126  
     127  #include <support/test-driver.c>