Wolfgang Thallergcc16 merge934c3885 months agoHistory
1230 lines · 37.0 KB · C
RawDownload
/* Library support for -fsplit-stack.  *//* Copyright (C) 2009-2026 Free Software Foundation, Inc.   Contributed by Ian Lance Taylor <iant@google.com>. This file is part of GCC. GCC is free software; you can redistribute it and/or modify it underthe terms of the GNU General Public License as published by the FreeSoftware Foundation; either version 3, or (at your option) any laterversion. GCC is distributed in the hope that it will be useful, but WITHOUT ANYWARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public Licensefor more details. Under Section 7 of GPL version 3, you are granted additionalpermissions described in the GCC Runtime Library Exception, version3.1, as published by the Free Software Foundation. You should have received a copy of the GNU General Public License anda copy of the GCC Runtime Library Exception along with this program;see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see<http://www.gnu.org/licenses/>.  */ #pragma GCC optimize ("no-isolate-erroneous-paths-dereference") /* powerpc 32-bit not supported.  */#if !defined __powerpc__ || defined __powerpc64__ #include "tconfig.h"#include "tsystem.h"#include "coretypes.h"#include "tm.h"#include "libgcc_tm.h" /* If inhibit_libc is defined, we cannot compile this file.  The   effect is that people will not be able to use -fsplit-stack.  That   is much better than failing the build particularly since people   will want to define inhibit_libc while building a compiler which   can build glibc.  */ #ifndef inhibit_libc #include <assert.h>#include <errno.h>#include <signal.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/mman.h>#include <sys/uio.h> #include "generic-morestack.h" /* Some systems use LD_PRELOAD or similar tricks to add hooks to   mmap/munmap.  That breaks this code, because when we call mmap   there is enough stack space for the system call but there is not,   in general, enough stack space to run a hook.  Try to avoid the   problem by calling syscall directly.  We only do this on GNU/Linux   for now, but it should be easy to add support for more systems with   testing.  */ #if defined(__gnu_linux__) #include <sys/syscall.h> #if defined(SYS_mmap) || defined(SYS_mmap2) #ifdef SYS_mmap2#define MORESTACK_MMAP SYS_mmap2#define MORESTACK_ADJUST_OFFSET(x) ((x) / 4096ULL)#else#define MORESTACK_MMAP SYS_mmap#define MORESTACK_ADJUST_OFFSET(x) (x)#endif static void *morestack_mmap (void *addr, size_t length, int prot, int flags, int fd,		off_t offset){  offset = MORESTACK_ADJUST_OFFSET (offset); #ifdef __s390__  long args[6] = { (long) addr, (long) length, (long) prot, (long) flags,		   (long) fd, (long) offset };  return (void *) syscall (MORESTACK_MMAP, args);#else  return (void *) syscall (MORESTACK_MMAP, addr, length, prot, flags, fd,			   offset);#endif} #define mmap morestack_mmap #endif /* defined(SYS_MMAP) || defined(SYS_mmap2) */ #if defined(SYS_munmap) static intmorestack_munmap (void * addr, size_t length){  return (int) syscall (SYS_munmap, addr, length);} #define munmap morestack_munmap #endif /* defined(SYS_munmap) */ #endif /* defined(__gnu_linux__) */ typedef unsigned uintptr_type __attribute__ ((mode (pointer))); /* This file contains subroutines that are used by code compiled with   -fsplit-stack.  */ /* Declare functions to avoid warnings--there is no header file for   these internal functions.  We give most of these functions the   flatten attribute in order to minimize their stack usage--here we   must minimize stack usage even at the cost of code size, and in   general inlining everything will do that.  */ extern void__generic_morestack_set_initial_sp (void *sp, size_t len)  __attribute__ ((no_split_stack, flatten, visibility ("hidden"))); extern void *__generic_morestack (size_t *frame_size, void *old_stack, size_t param_size)  __attribute__ ((no_split_stack, flatten, visibility ("hidden"))); extern void *__generic_releasestack (size_t *pavailable)  __attribute__ ((no_split_stack, flatten, visibility ("hidden"))); extern void__morestack_block_signals (void)  __attribute__ ((no_split_stack, flatten, visibility ("hidden"))); extern void__morestack_unblock_signals (void)  __attribute__ ((no_split_stack, flatten, visibility ("hidden"))); extern size_t__generic_findstack (void *stack)  __attribute__ ((no_split_stack, flatten, visibility ("hidden"))); extern void__morestack_load_mmap (void)  __attribute__ ((no_split_stack, visibility ("hidden"))); extern void *__morestack_allocate_stack_space (size_t size)  __attribute__ ((visibility ("hidden"))); /* These are functions which -fsplit-stack code can call.  These are   not called by the compiler, and are not hidden.  FIXME: These   should be in some header file somewhere, somehow.  */ extern void *__splitstack_find (void *, void *, size_t *, void **, void **, void **)  __attribute__ ((visibility ("default"))); extern void__splitstack_block_signals (int *, int *)  __attribute__ ((visibility ("default"))); extern void__splitstack_getcontext (void *context[10])  __attribute__ ((no_split_stack, visibility ("default"))); extern void__splitstack_setcontext (void *context[10])  __attribute__ ((no_split_stack, visibility ("default"))); extern void *__splitstack_makecontext (size_t, void *context[10], size_t *)  __attribute__ ((visibility ("default"))); extern void *__splitstack_resetcontext (void *context[10], size_t *)  __attribute__ ((visibility ("default"))); extern void__splitstack_releasecontext (void *context[10])  __attribute__ ((visibility ("default"))); extern void__splitstack_block_signals_context (void *context[10], int *, int *)  __attribute__ ((visibility ("default"))); extern void *__splitstack_find_context (void *context[10], size_t *, void **, void **,			   void **)  __attribute__ ((visibility ("default"))); /* These functions must be defined by the processor specific code.  */ extern void *__morestack_get_guard (void)  __attribute__ ((no_split_stack, visibility ("hidden"))); extern void __morestack_set_guard (void *)  __attribute__ ((no_split_stack, visibility ("hidden"))); extern void *__morestack_make_guard (void *, size_t)  __attribute__ ((no_split_stack, visibility ("hidden"))); /* When we allocate a stack segment we put this header at the   start.  */ struct stack_segment{  /* The previous stack segment--when a function running on this stack     segment returns, it will run on the previous one.  */  struct stack_segment *prev;  /* The next stack segment, if it has been allocated--when a function     is running on this stack segment, the next one is not being     used.  */  struct stack_segment *next;  /* The total size of this stack segment.  */  size_t size;  /* The stack address when this stack was created.  This is used when     popping the stack.  */  void *old_stack;  /* A list of memory blocks allocated by dynamic stack     allocation.  */  struct dynamic_allocation_blocks *dynamic_allocation;  /* A list of dynamic memory blocks no longer needed.  */  struct dynamic_allocation_blocks *free_dynamic_allocation;  /* An extra pointer in case we need some more information some     day.  */  void *extra;}; /* This structure holds the (approximate) initial stack pointer and   size for the system supplied stack for a thread.  This is set when   the thread is created.  We also store a sigset_t here to hold the   signal mask while splitting the stack, since we don't want to store   that on the stack.  */ struct initial_sp{  /* The initial stack pointer.  */  void *sp;  /* The stack length.  */  size_t len;  /* A signal mask, put here so that the thread can use it without     needing stack space.  */  sigset_t mask;  /* Non-zero if we should not block signals.  This is a reversed flag     so that the default zero value is the safe value.  The type is     uintptr_type because it replaced one of the void * pointers in     extra.  */  uintptr_type dont_block_signals;  /* Some extra space for later extensibility.  */  void *extra[4];}; /* A list of memory blocks allocated by dynamic stack allocation.   This is used for code that calls alloca or uses variably sized   arrays.  */ struct dynamic_allocation_blocks{  /* The next block in the list.  */  struct dynamic_allocation_blocks *next;  /* The size of the allocated memory.  */  size_t size;  /* The allocated memory.  */  void *block;}; /* These thread local global variables must be shared by all split   stack code across shared library boundaries.  Therefore, they have   default visibility.  They have extensibility fields if needed for   new versions.  If more radical changes are needed, new code can be   written using new variable names, while still using the existing   variables in a backward compatible manner.  Symbol versioning is   also used, although, since these variables are only referenced by   code in this file and generic-morestack-thread.c, it is likely that   simply using new names will suffice.  */ /* The first stack segment allocated for this thread.  */ __thread struct stack_segment *__morestack_segments  __attribute__ ((visibility ("default"))); /* The stack segment that we think we are currently using.  This will   be correct in normal usage, but will be incorrect if an exception   unwinds into a different stack segment or if longjmp jumps to a   different stack segment.  */ __thread struct stack_segment *__morestack_current_segment  __attribute__ ((visibility ("default"))); /* The initial stack pointer and size for this thread.  */ __thread struct initial_sp __morestack_initial_sp  __attribute__ ((visibility ("default"))); /* A static signal mask, to avoid taking up stack space.  */ static sigset_t __morestack_fullmask; /* Page size, as returned from getpagesize(). Set on startup. */static unsigned int static_pagesize; /* Set on startup to non-zero value if SPLIT_STACK_GUARD env var is set. */static int use_guard_page; /* Convert an integer to a decimal string without using much stack   space.  Return a pointer to the part of the buffer to use.  We this   instead of sprintf because sprintf will require too much stack   space.  */ static char *print_int (int val, char *buf, int buflen, size_t *print_len){  int is_negative;  int i;  unsigned int uval;   uval = (unsigned int) val;  if (val >= 0)    is_negative = 0;  else    {      is_negative = 1;      uval = - uval;    }   i = buflen;  do    {      --i;      buf[i] = '0' + (uval % 10);      uval /= 10;    }  while (uval != 0 && i > 0);   if (is_negative)    {      if (i > 0)	--i;      buf[i] = '-';    }   *print_len = buflen - i;  return buf + i;} /* Print the string MSG/LEN, the errno number ERR, and a newline on   stderr.  Then crash.  */ void__morestack_fail (const char *, size_t, int) __attribute__ ((noreturn)); void__morestack_fail (const char *msg, size_t len, int err){  char buf[24];  static const char nl[] = "\n";  struct iovec iov[3];  union { char *p; const char *cp; } const_cast;   const_cast.cp = msg;  iov[0].iov_base = const_cast.p;  iov[0].iov_len = len;  /* We can't call strerror, because it may try to translate the error     message, and that would use too much stack space.  */  iov[1].iov_base = print_int (err, buf, sizeof buf, &iov[1].iov_len);  const_cast.cp = &nl[0];  iov[2].iov_base = const_cast.p;  iov[2].iov_len = sizeof nl - 1;  /* FIXME: On systems without writev we need to issue three write     calls, or punt on printing errno.  For now this is irrelevant     since stack splitting only works on GNU/Linux anyhow.  */  writev (2, iov, 3);  abort ();} /* Allocate a new stack segment.  FRAME_SIZE is the required frame   size.  */ static struct stack_segment *allocate_segment (size_t frame_size){  unsigned int pagesize;  unsigned int overhead;  unsigned int allocate;  void *space;  struct stack_segment *pss;   pagesize = static_pagesize;  overhead = sizeof (struct stack_segment);   allocate = pagesize;  if (allocate < MINSIGSTKSZ)    allocate = ((MINSIGSTKSZ + overhead + pagesize - 1)		& ~ (pagesize - 1));  if (allocate < frame_size)    allocate = ((frame_size + overhead + pagesize - 1)		& ~ (pagesize - 1));   if (use_guard_page)    allocate += pagesize;   /* FIXME: If this binary requires an executable stack, then we need     to set PROT_EXEC.  Unfortunately figuring that out is complicated     and target dependent.  We would need to use dl_iterate_phdr to     see if there is any object which does not have a PT_GNU_STACK     phdr, though only for architectures which use that mechanism.  */  space = mmap (NULL, allocate, PROT_READ | PROT_WRITE,		MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);  if (space == MAP_FAILED)    {      static const char msg[] =	"unable to allocate additional stack space: errno ";      __morestack_fail (msg, sizeof msg - 1, errno);    }   if (use_guard_page)    {      void *guard; #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__      guard = space;      space = (char *) space + pagesize;#else      guard = space + allocate - pagesize;#endif       mprotect (guard, pagesize, PROT_NONE);      allocate -= pagesize;    }   pss = (struct stack_segment *) space;   pss->prev = NULL;  pss->next = NULL;  pss->size = allocate - overhead;  pss->dynamic_allocation = NULL;  pss->free_dynamic_allocation = NULL;  pss->extra = NULL;   return pss;} /* Free a list of dynamic blocks.  */ static voidfree_dynamic_blocks (struct dynamic_allocation_blocks *p){  while (p != NULL)    {      struct dynamic_allocation_blocks *next;       next = p->next;      free (p->block);      free (p);      p = next;    }} /* Merge two lists of dynamic blocks.  */ static struct dynamic_allocation_blocks *merge_dynamic_blocks (struct dynamic_allocation_blocks *a,		      struct dynamic_allocation_blocks *b){  struct dynamic_allocation_blocks **pp;   if (a == NULL)    return b;  if (b == NULL)    return a;  for (pp = &a->next; *pp != NULL; pp = &(*pp)->next)    ;  *pp = b;  return a;} /* Release stack segments.  If FREE_DYNAMIC is non-zero, we also free   any dynamic blocks.  Otherwise we return them.  */ struct dynamic_allocation_blocks *__morestack_release_segments (struct stack_segment **pp, int free_dynamic){  struct dynamic_allocation_blocks *ret;  struct stack_segment *pss;   ret = NULL;  pss = *pp;  while (pss != NULL)    {      struct stack_segment *next;      unsigned int allocate;       next = pss->next;       if (pss->dynamic_allocation != NULL

Lines 1–500 of 1230.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t