root/maint/gnulib/tests/altstack-util.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. prepare_alternate_stack
  2. check_alternate_stack_no_overflow

   1 /* Some auxiliary stuff for defining an alternate stack.
   2    Copyright (C) 2010-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 2 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 /* Written by Eric Blake and Bruno Haible.  */
  18 
  19 #include <stdint.h> /* uintptr_t */
  20 #include <string.h> /* for memset */
  21 
  22 #define MYSTACK_SIZE (1 << 24)
  23 
  24 /* glibc says: Users should use SIGSTKSZ as the size of user-supplied
  25    buffers.  We want to detect stack overflow of the alternate stack
  26    in a nicer manner than just crashing, so we overallocate in
  27    comparison to what we hand libsigsegv.  Also, we intentionally hand
  28    an unaligned pointer, to ensure the alternate stack still ends up
  29    aligned.  */
  30 #define MYSTACK_CRUMPLE_ZONE 8192
  31 static char mystack_storage[MYSTACK_SIZE + 2 * MYSTACK_CRUMPLE_ZONE + 31];
  32 static char *mystack; /* MYSTACK_SIZE bytes in the middle of storage. */
  33 
  34 static void
  35 prepare_alternate_stack (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37 #ifdef SIGSTKSZ
  38   if (MYSTACK_SIZE < SIGSTKSZ)
  39     {
  40       size_t size = SIGSTKSZ;
  41       printf ("SIGSTKSZ=%zu exceeds MYSTACK_SIZE=%d\n", size, MYSTACK_SIZE);
  42       exit (1);
  43     }
  44 #endif
  45   memset (mystack_storage, 's', sizeof mystack_storage);
  46   mystack = (char *) ((uintptr_t) (mystack_storage + MYSTACK_CRUMPLE_ZONE) | 31);
  47 }
  48 
  49 static void
  50 check_alternate_stack_no_overflow (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52   unsigned int i;
  53 
  54   for (i = MYSTACK_CRUMPLE_ZONE; i > 0; i--)
  55     if (*(mystack - i) != 's')
  56       {
  57         printf ("Alternate stack was exceeded by %u bytes!!\n", i);
  58         exit (1);
  59       }
  60   for (i = MYSTACK_CRUMPLE_ZONE; i > 0; i--)
  61     if (*(mystack + MYSTACK_SIZE - 1 + i) != 's')
  62       {
  63         printf ("Alternate stack was exceeded by %u bytes!!\n", i);
  64         exit (1);
  65       }
  66 }

/* [previous][next][first][last][top][bottom][index][help] */