generic-morestack-thread.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Thread library support for -fsplit-stack. */
  2. /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. Contributed by Ian Lance Taylor <iant@google.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "tconfig.h"
  21. #include "tsystem.h"
  22. #include "coretypes.h"
  23. #include "tm.h"
  24. #include "libgcc_tm.h"
  25. /* If inhibit_libc is defined, we cannot compile this file. The
  26. effect is that people will not be able to use -fsplit-stack. That
  27. is much better than failing the build particularly since people
  28. will want to define inhibit_libc while building a compiler which
  29. can build glibc. */
  30. #ifndef inhibit_libc
  31. #include <errno.h>
  32. #include <signal.h>
  33. #include <pthread.h>
  34. #include "generic-morestack.h"
  35. /* We declare the pthread functions we need as weak, so that
  36. libgcc_s.so does not need to be linked against -lpthread. */
  37. extern int pthread_once (pthread_once_t *, void (*) (void))
  38. __attribute__ ((weak));
  39. extern int pthread_key_create (pthread_key_t *, void (*) (void *))
  40. __attribute__ ((weak));
  41. extern int pthread_setspecific (pthread_key_t, const void *)
  42. __attribute__ ((weak));
  43. extern int pthread_sigmask (int, const sigset_t *, sigset_t *)
  44. __attribute__ ((weak));
  45. /* The key for the list of stack segments to free when the thread
  46. exits. This is created by pthread_key_create. */
  47. static pthread_key_t segment_list_key;
  48. /* Used to only run create_key once. */
  49. static pthread_once_t create_key_once = PTHREAD_ONCE_INIT;
  50. /* Release all the segments for a thread. This is the destructor
  51. function used by pthread_key_create, and is called when a thread
  52. exits. */
  53. static void
  54. free_segments (void* arg)
  55. {
  56. /* We must block signals in case the signal handler tries to split
  57. the stack. We leave them blocked while the thread exits. */
  58. if (pthread_sigmask)
  59. {
  60. sigset_t mask;
  61. sigfillset (&mask);
  62. pthread_sigmask (SIG_BLOCK, &mask, NULL);
  63. }
  64. __morestack_release_segments ((struct stack_segment **) arg, 1);
  65. }
  66. /* Set up the key for the list of segments. This is called via
  67. pthread_once. */
  68. static void
  69. create_key (void)
  70. {
  71. int err;
  72. err = pthread_key_create (&segment_list_key, free_segments);
  73. if (err != 0)
  74. {
  75. static const char msg[] = "pthread_key_create failed: errno ";
  76. __morestack_fail (msg, sizeof msg - 1, err);
  77. }
  78. }
  79. /* Pass information from the pthread_create wrapper to
  80. stack_split_initialize_thread. */
  81. struct pthread_create_args
  82. {
  83. void *(*start_routine) (void *);
  84. void *arg;
  85. };
  86. /* Initialize a thread. This is called via pthread_create. It calls
  87. a target dependent function to set up any required stack guard. */
  88. static void* stack_split_initialize_thread (void *)
  89. __attribute__ ((no_split_stack));
  90. static void *
  91. stack_split_initialize_thread (void *varg)
  92. {
  93. struct pthread_create_args *args = (struct pthread_create_args *) varg;
  94. int err;
  95. void *(*start_routine) (void *);
  96. void *arg;
  97. __stack_split_initialize ();
  98. err = pthread_setspecific (segment_list_key, (void *) &__morestack_segments);
  99. if (err != 0)
  100. {
  101. static const char msg[] = "pthread_setspecific failed: errno ";
  102. __morestack_fail (msg, sizeof msg - 1, err);
  103. }
  104. start_routine = args->start_routine;
  105. arg = args->arg;
  106. free (args);
  107. return (*start_routine) (arg);
  108. }
  109. /* This function wraps calls to pthread_create to make sure that the
  110. stack guard is initialized for new threads. FIXME: This hack will
  111. not be necessary if glibc supports -fsplit-stack directly. */
  112. int __wrap_pthread_create (pthread_t *, const pthread_attr_t *,
  113. void *(*start_routine) (void *), void *)
  114. __attribute__ ((visibility ("hidden")));
  115. extern int __real_pthread_create (pthread_t *, const pthread_attr_t *,
  116. void *(*start_routine) (void *), void *)
  117. __attribute__ ((weak));
  118. int
  119. __wrap_pthread_create (pthread_t *tid, const pthread_attr_t *attr,
  120. void *(*start_routine) (void *), void *arg)
  121. {
  122. int err;
  123. struct pthread_create_args* args;
  124. err = pthread_once (&create_key_once, create_key);
  125. if (err != 0)
  126. {
  127. static const char msg[] = "pthread_once failed: errno ";
  128. __morestack_fail (msg, sizeof msg - 1, err);
  129. }
  130. args = malloc (sizeof (struct pthread_create_args));
  131. if (args == NULL)
  132. return EAGAIN;
  133. args->start_routine = start_routine;
  134. args->arg = arg;
  135. return __real_pthread_create (tid, attr, stack_split_initialize_thread, args);
  136. }
  137. #endif /* !defined (inhibit_libc) */