generic-morestack.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /* 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. #pragma GCC optimize ("no-isolate-erroneous-paths-dereference")
  21. /* powerpc 32-bit not supported. */
  22. #if !defined __powerpc__ || defined __powerpc64__
  23. #include "tconfig.h"
  24. #include "tsystem.h"
  25. #include "coretypes.h"
  26. #include "tm.h"
  27. #include "libgcc_tm.h"
  28. /* If inhibit_libc is defined, we cannot compile this file. The
  29. effect is that people will not be able to use -fsplit-stack. That
  30. is much better than failing the build particularly since people
  31. will want to define inhibit_libc while building a compiler which
  32. can build glibc. */
  33. #ifndef inhibit_libc
  34. #include <assert.h>
  35. #include <errno.h>
  36. #include <signal.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <sys/mman.h>
  41. #include <sys/uio.h>
  42. #include "generic-morestack.h"
  43. /* Some systems use LD_PRELOAD or similar tricks to add hooks to
  44. mmap/munmap. That breaks this code, because when we call mmap
  45. there is enough stack space for the system call but there is not,
  46. in general, enough stack space to run a hook. Try to avoid the
  47. problem by calling syscall directly. We only do this on GNU/Linux
  48. for now, but it should be easy to add support for more systems with
  49. testing. */
  50. #if defined(__gnu_linux__)
  51. #include <sys/syscall.h>
  52. #if defined(SYS_mmap) || defined(SYS_mmap2)
  53. #ifdef SYS_mmap2
  54. #define MORESTACK_MMAP SYS_mmap2
  55. #define MORESTACK_ADJUST_OFFSET(x) ((x) / 4096ULL)
  56. #else
  57. #define MORESTACK_MMAP SYS_mmap
  58. #define MORESTACK_ADJUST_OFFSET(x) (x)
  59. #endif
  60. static void *
  61. morestack_mmap (void *addr, size_t length, int prot, int flags, int fd,
  62. off_t offset)
  63. {
  64. offset = MORESTACK_ADJUST_OFFSET (offset);
  65. #ifdef __s390__
  66. long args[6] = { (long) addr, (long) length, (long) prot, (long) flags,
  67. (long) fd, (long) offset };
  68. return (void *) syscall (MORESTACK_MMAP, args);
  69. #else
  70. return (void *) syscall (MORESTACK_MMAP, addr, length, prot, flags, fd,
  71. offset);
  72. #endif
  73. }
  74. #define mmap morestack_mmap
  75. #endif /* defined(SYS_MMAP) || defined(SYS_mmap2) */
  76. #if defined(SYS_munmap)
  77. static int
  78. morestack_munmap (void * addr, size_t length)
  79. {
  80. return (int) syscall (SYS_munmap, addr, length);
  81. }
  82. #define munmap morestack_munmap
  83. #endif /* defined(SYS_munmap) */
  84. #endif /* defined(__gnu_linux__) */
  85. typedef unsigned uintptr_type __attribute__ ((mode (pointer)));
  86. /* This file contains subroutines that are used by code compiled with
  87. -fsplit-stack. */
  88. /* Declare functions to avoid warnings--there is no header file for
  89. these internal functions. We give most of these functions the
  90. flatten attribute in order to minimize their stack usage--here we
  91. must minimize stack usage even at the cost of code size, and in
  92. general inlining everything will do that. */
  93. extern void
  94. __generic_morestack_set_initial_sp (void *sp, size_t len)
  95. __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
  96. extern void *
  97. __generic_morestack (size_t *frame_size, void *old_stack, size_t param_size)
  98. __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
  99. extern void *
  100. __generic_releasestack (size_t *pavailable)
  101. __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
  102. extern void
  103. __morestack_block_signals (void)
  104. __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
  105. extern void
  106. __morestack_unblock_signals (void)
  107. __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
  108. extern size_t
  109. __generic_findstack (void *stack)
  110. __attribute__ ((no_split_stack, flatten, visibility ("hidden")));
  111. extern void
  112. __morestack_load_mmap (void)
  113. __attribute__ ((no_split_stack, visibility ("hidden")));
  114. extern void *
  115. __morestack_allocate_stack_space (size_t size)
  116. __attribute__ ((visibility ("hidden")));
  117. /* These are functions which -fsplit-stack code can call. These are
  118. not called by the compiler, and are not hidden. FIXME: These
  119. should be in some header file somewhere, somehow. */
  120. extern void *
  121. __splitstack_find (void *, void *, size_t *, void **, void **, void **)
  122. __attribute__ ((visibility ("default")));
  123. extern void
  124. __splitstack_block_signals (int *, int *)
  125. __attribute__ ((visibility ("default")));
  126. extern void
  127. __splitstack_getcontext (void *context[10])
  128. __attribute__ ((no_split_stack, visibility ("default")));
  129. extern void
  130. __splitstack_setcontext (void *context[10])
  131. __attribute__ ((no_split_stack, visibility ("default")));
  132. extern void *
  133. __splitstack_makecontext (size_t, void *context[10], size_t *)
  134. __attribute__ ((visibility ("default")));
  135. extern void *
  136. __splitstack_resetcontext (void *context[10], size_t *)
  137. __attribute__ ((visibility ("default")));
  138. extern void
  139. __splitstack_releasecontext (void *context[10])
  140. __attribute__ ((visibility ("default")));
  141. extern void
  142. __splitstack_block_signals_context (void *context[10], int *, int *)
  143. __attribute__ ((visibility ("default")));
  144. extern void *
  145. __splitstack_find_context (void *context[10], size_t *, void **, void **,
  146. void **)
  147. __attribute__ ((visibility ("default")));
  148. /* These functions must be defined by the processor specific code. */
  149. extern void *__morestack_get_guard (void)
  150. __attribute__ ((no_split_stack, visibility ("hidden")));
  151. extern void __morestack_set_guard (void *)
  152. __attribute__ ((no_split_stack, visibility ("hidden")));
  153. extern void *__morestack_make_guard (void *, size_t)
  154. __attribute__ ((no_split_stack, visibility ("hidden")));
  155. /* When we allocate a stack segment we put this header at the
  156. start. */
  157. struct stack_segment
  158. {
  159. /* The previous stack segment--when a function running on this stack
  160. segment returns, it will run on the previous one. */
  161. struct stack_segment *prev;
  162. /* The next stack segment, if it has been allocated--when a function
  163. is running on this stack segment, the next one is not being
  164. used. */
  165. struct stack_segment *next;
  166. /* The total size of this stack segment. */
  167. size_t size;
  168. /* The stack address when this stack was created. This is used when
  169. popping the stack. */
  170. void *old_stack;
  171. /* A list of memory blocks allocated by dynamic stack
  172. allocation. */
  173. struct dynamic_allocation_blocks *dynamic_allocation;
  174. /* A list of dynamic memory blocks no longer needed. */
  175. struct dynamic_allocation_blocks *free_dynamic_allocation;
  176. /* An extra pointer in case we need some more information some
  177. day. */
  178. void *extra;
  179. };
  180. /* This structure holds the (approximate) initial stack pointer and
  181. size for the system supplied stack for a thread. This is set when
  182. the thread is created. We also store a sigset_t here to hold the
  183. signal mask while splitting the stack, since we don't want to store
  184. that on the stack. */
  185. struct initial_sp
  186. {
  187. /* The initial stack pointer. */
  188. void *sp;
  189. /* The stack length. */
  190. size_t len;
  191. /* A signal mask, put here so that the thread can use it without
  192. needing stack space. */
  193. sigset_t mask;
  194. /* Non-zero if we should not block signals. This is a reversed flag
  195. so that the default zero value is the safe value. The type is
  196. uintptr_type because it replaced one of the void * pointers in
  197. extra. */
  198. uintptr_type dont_block_signals;
  199. /* Some extra space for later extensibility. */
  200. void *extra[4];
  201. };
  202. /* A list of memory blocks allocated by dynamic stack allocation.
  203. This is used for code that calls alloca or uses variably sized
  204. arrays. */
  205. struct dynamic_allocation_blocks
  206. {
  207. /* The next block in the list. */
  208. struct dynamic_allocation_blocks *next;
  209. /* The size of the allocated memory. */
  210. size_t size;
  211. /* The allocated memory. */
  212. void *block;
  213. };
  214. /* These thread local global variables must be shared by all split
  215. stack code across shared library boundaries. Therefore, they have
  216. default visibility. They have extensibility fields if needed for
  217. new versions. If more radical changes are needed, new code can be
  218. written using new variable names, while still using the existing
  219. variables in a backward compatible manner. Symbol versioning is
  220. also used, although, since these variables are only referenced by
  221. code in this file and generic-morestack-thread.c, it is likely that
  222. simply using new names will suffice. */
  223. /* The first stack segment allocated for this thread. */
  224. __thread struct stack_segment *__morestack_segments
  225. __attribute__ ((visibility ("default")));
  226. /* The stack segment that we think we are currently using. This will
  227. be correct in normal usage, but will be incorrect if an exception
  228. unwinds into a different stack segment or if longjmp jumps to a
  229. different stack segment. */
  230. __thread struct stack_segment *__morestack_current_segment
  231. __attribute__ ((visibility ("default")));
  232. /* The initial stack pointer and size for this thread. */
  233. __thread struct initial_sp __morestack_initial_sp
  234. __attribute__ ((visibility ("default")));
  235. /* A static signal mask, to avoid taking up stack space. */
  236. static sigset_t __morestack_fullmask;
  237. /* Page size, as returned from getpagesize(). Set on startup. */
  238. static unsigned int static_pagesize;
  239. /* Set on startup to non-zero value if SPLIT_STACK_GUARD env var is set. */
  240. static int use_guard_page;
  241. /* Convert an integer to a decimal string without using much stack
  242. space. Return a pointer to the part of the buffer to use. We this
  243. instead of sprintf because sprintf will require too much stack
  244. space. */
  245. static char *
  246. print_int (int val, char *buf, int buflen, size_t *print_len)
  247. {
  248. int is_negative;
  249. int i;
  250. unsigned int uval;
  251. uval = (unsigned int) val;
  252. if (val >= 0)
  253. is_negative = 0;
  254. else
  255. {
  256. is_negative = 1;
  257. uval = - uval;
  258. }
  259. i = buflen;
  260. do
  261. {
  262. --i;
  263. buf[i] = '0' + (uval % 10);
  264. uval /= 10;
  265. }
  266. while (uval != 0 && i > 0);
  267. if (is_negative)
  268. {
  269. if (i > 0)
  270. --i;
  271. buf[i] = '-';
  272. }
  273. *print_len = buflen - i;
  274. return buf + i;
  275. }
  276. /* Print the string MSG/LEN, the errno number ERR, and a newline on
  277. stderr. Then crash. */
  278. void
  279. __morestack_fail (const char *, size_t, int) __attribute__ ((noreturn));
  280. void
  281. __morestack_fail (const char *msg, size_t len, int err)
  282. {
  283. char buf[24];
  284. static const char nl[] = "\n";
  285. struct iovec iov[3];
  286. union { char *p; const char *cp; } const_cast;
  287. const_cast.cp = msg;
  288. iov[0].iov_base = const_cast.p;
  289. iov[0].iov_len = len;
  290. /* We can't call strerror, because it may try to translate the error
  291. message, and that would use too much stack space. */
  292. iov[1].iov_base = print_int (err, buf, sizeof buf, &iov[1].iov_len);
  293. const_cast.cp = &nl[0];
  294. iov[2].iov_base = const_cast.p;
  295. iov[2].iov_len = sizeof nl - 1;
  296. /* FIXME: On systems without writev we need to issue three write
  297. calls, or punt on printing errno. For now this is irrelevant
  298. since stack splitting only works on GNU/Linux anyhow. */
  299. writev (2, iov, 3);
  300. abort ();
  301. }
  302. /* Allocate a new stack segment. FRAME_SIZE is the required frame
  303. size. */
  304. static struct stack_segment *
  305. allocate_segment (size_t frame_size)
  306. {
  307. unsigned int pagesize;
  308. unsigned int overhead;
  309. unsigned int allocate;
  310. void *space;
  311. struct stack_segment *pss;
  312. pagesize = static_pagesize;
  313. overhead = sizeof (struct stack_segment);
  314. allocate = pagesize;
  315. if (allocate < MINSIGSTKSZ)
  316. allocate = ((MINSIGSTKSZ + overhead + pagesize - 1)
  317. & ~ (pagesize - 1));
  318. if (allocate < frame_size)
  319. allocate = ((frame_size + overhead + pagesize - 1)
  320. & ~ (pagesize - 1));
  321. if (use_guard_page)
  322. allocate += pagesize;
  323. /* FIXME: If this binary requires an executable stack, then we need
  324. to set PROT_EXEC. Unfortunately figuring that out is complicated
  325. and target dependent. We would need to use dl_iterate_phdr to
  326. see if there is any object which does not have a PT_GNU_STACK
  327. phdr, though only for architectures which use that mechanism. */
  328. space = mmap (NULL, allocate, PROT_READ | PROT_WRITE,
  329. MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  330. if (space == MAP_FAILED)
  331. {
  332. static const char msg[] =
  333. "unable to allocate additional stack space: errno ";
  334. __morestack_fail (msg, sizeof msg - 1, errno);
  335. }
  336. if (use_guard_page)
  337. {
  338. void *guard;
  339. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  340. guard = space;
  341. space = (char *) space + pagesize;
  342. #else
  343. guard = space + allocate - pagesize;
  344. #endif
  345. mprotect (guard, pagesize, PROT_NONE);
  346. allocate -= pagesize;
  347. }
  348. pss = (struct stack_segment *) space;
  349. pss->prev = NULL;
  350. pss->next = NULL;
  351. pss->size = allocate - overhead;
  352. pss->dynamic_allocation = NULL;
  353. pss->free_dynamic_allocation = NULL;
  354. pss->extra = NULL;
  355. return pss;
  356. }
  357. /* Free a list of dynamic blocks. */
  358. static void
  359. free_dynamic_blocks (struct dynamic_allocation_blocks *p)
  360. {
  361. while (p != NULL)
  362. {
  363. struct dynamic_allocation_blocks *next;
  364. next = p->next;
  365. free (p->block);
  366. free (p);
  367. p = next;
  368. }
  369. }
  370. /* Merge two lists of dynamic blocks. */
  371. static struct dynamic_allocation_blocks *
  372. merge_dynamic_blocks (struct dynamic_allocation_blocks *a,
  373. struct dynamic_allocation_blocks *b)
  374. {
  375. struct dynamic_allocation_blocks **pp;
  376. if (a == NULL)
  377. return b;
  378. if (b == NULL)
  379. return a;
  380. for (pp = &a->next; *pp != NULL; pp = &(*pp)->next)
  381. ;
  382. *pp = b;
  383. return a;
  384. }
  385. /* Release stack segments. If FREE_DYNAMIC is non-zero, we also free
  386. any dynamic blocks. Otherwise we return them. */
  387. struct dynamic_allocation_blocks *
  388. __morestack_release_segments (struct stack_segment **pp, int free_dynamic)
  389. {
  390. struct dynamic_allocation_blocks *ret;
  391. struct stack_segment *pss;
  392. ret = NULL;
  393. pss = *pp;
  394. while (pss != NULL)
  395. {
  396. struct stack_segment *next;
  397. unsigned int allocate;
  398. next = pss->next;
  399. if (pss->dynamic_allocation != NULL
  400. || pss->free_dynamic_allocation != NULL)
  401. {
  402. if (free_dynamic)
  403. {
  404. free_dynamic_blocks (pss->dynamic_allocation);
  405. free_dynamic_blocks (pss->free_dynamic_allocation);
  406. }
  407. else
  408. {
  409. ret = merge_dynamic_blocks (pss->dynamic_allocation, ret);
  410. ret = merge_dynamic_blocks (pss->free_dynamic_allocation, ret);
  411. }
  412. }
  413. allocate = pss->size + sizeof (struct stack_segment);
  414. if (munmap (pss, allocate) < 0)
  415. {
  416. static const char msg[] = "munmap of stack space failed: errno ";
  417. __morestack_fail (msg, sizeof msg - 1, errno);
  418. }
  419. pss = next;
  420. }
  421. *pp = NULL;
  422. return ret;
  423. }
  424. /* This function is called by a processor specific function to set the
  425. initial stack pointer for a thread. The operating system will
  426. always create a stack for a thread. Here we record a stack pointer
  427. near the base of that stack. The size argument lets the processor
  428. specific code estimate how much stack space is available on this
  429. initial stack. */
  430. void
  431. __generic_morestack_set_initial_sp (void *sp, size_t len)
  432. {
  433. /* The stack pointer most likely starts on a page boundary. Adjust
  434. to the nearest 512 byte boundary. It's not essential that we be
  435. precise here; getting it wrong will just leave some stack space
  436. unused. */
  437. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  438. sp = (void *) ((((__UINTPTR_TYPE__) sp + 511U) / 512U) * 512U);
  439. #else
  440. sp = (void *) ((((__UINTPTR_TYPE__) sp - 511U) / 512U) * 512U);
  441. #endif
  442. __morestack_initial_sp.sp = sp;
  443. __morestack_initial_sp.len = len;
  444. sigemptyset (&__morestack_initial_sp.mask);
  445. sigfillset (&__morestack_fullmask);
  446. #if defined(__GLIBC__) && defined(__linux__)
  447. /* In glibc, the first two real time signals are used by the NPTL
  448. threading library. By taking them out of the set of signals, we
  449. avoiding copying the signal mask in pthread_sigmask. More
  450. importantly, pthread_sigmask uses less stack space on x86_64. */
  451. sigdelset (&__morestack_fullmask, __SIGRTMIN);
  452. sigdelset (&__morestack_fullmask, __SIGRTMIN + 1);
  453. #endif
  454. }
  455. /* This function is called by a processor specific function which is
  456. run in the prologue when more stack is needed. The processor
  457. specific function handles the details of saving registers and
  458. frobbing the actual stack pointer. This function is responsible
  459. for allocating a new stack segment and for copying a parameter
  460. block from the old stack to the new one. On function entry
  461. *PFRAME_SIZE is the size of the required stack frame--the returned
  462. stack must be at least this large. On function exit *PFRAME_SIZE
  463. is the amount of space remaining on the allocated stack. OLD_STACK
  464. points at the parameters the old stack (really the current one
  465. while this function is running). OLD_STACK is saved so that it can
  466. be returned by a later call to __generic_releasestack. PARAM_SIZE
  467. is the size in bytes of parameters to copy to the new stack. This
  468. function returns a pointer to the new stack segment, pointing to
  469. the memory after the parameters have been copied. The returned
  470. value minus the returned *PFRAME_SIZE (or plus if the stack grows
  471. upward) is the first address on the stack which should not be used.
  472. This function is running on the old stack and has only a limited
  473. amount of stack space available. */
  474. void *
  475. __generic_morestack (size_t *pframe_size, void *old_stack, size_t param_size)
  476. {
  477. size_t frame_size = *pframe_size;
  478. struct stack_segment *current;
  479. struct stack_segment **pp;
  480. struct dynamic_allocation_blocks *dynamic;
  481. char *from;
  482. char *to;
  483. void *ret;
  484. size_t i;
  485. size_t aligned;
  486. current = __morestack_current_segment;
  487. pp = current != NULL ? &current->next : &__morestack_segments;
  488. if (*pp != NULL && (*pp)->size < frame_size)
  489. dynamic = __morestack_release_segments (pp, 0);
  490. else
  491. dynamic = NULL;
  492. current = *pp;
  493. if (current == NULL)
  494. {
  495. current = allocate_segment (frame_size + param_size);
  496. current->prev = __morestack_current_segment;
  497. *pp = current;
  498. }
  499. current->old_stack = old_stack;
  500. __morestack_current_segment = current;
  501. if (dynamic != NULL)
  502. {
  503. /* Move the free blocks onto our list. We don't want to call
  504. free here, as we are short on stack space. */
  505. current->free_dynamic_allocation =
  506. merge_dynamic_blocks (dynamic, current->free_dynamic_allocation);
  507. }
  508. *pframe_size = current->size - param_size;
  509. /* Align the returned stack to a 32-byte boundary. */
  510. aligned = (param_size + 31) & ~ (size_t) 31;
  511. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  512. {
  513. char *bottom = (char *) (current + 1) + current->size;
  514. to = bottom - aligned;
  515. ret = bottom - aligned;
  516. }
  517. #else
  518. to = current + 1;
  519. to += aligned - param_size;
  520. ret = (char *) (current + 1) + aligned;
  521. #endif
  522. /* We don't call memcpy to avoid worrying about the dynamic linker
  523. trying to resolve it. */
  524. from = (char *) old_stack;
  525. for (i = 0; i < param_size; i++)
  526. *to++ = *from++;
  527. return ret;
  528. }
  529. /* This function is called by a processor specific function when it is
  530. ready to release a stack segment. We don't actually release the
  531. stack segment, we just move back to the previous one. The current
  532. stack segment will still be available if we need it in
  533. __generic_morestack. This returns a pointer to the new stack
  534. segment to use, which is the one saved by a previous call to
  535. __generic_morestack. The processor specific function is then
  536. responsible for actually updating the stack pointer. This sets
  537. *PAVAILABLE to the amount of stack space now available. */
  538. void *
  539. __generic_releasestack (size_t *pavailable)
  540. {
  541. struct stack_segment *current;
  542. void *old_stack;
  543. current = __morestack_current_segment;
  544. old_stack = current->old_stack;
  545. current = current->prev;
  546. __morestack_current_segment = current;
  547. if (current != NULL)
  548. {
  549. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  550. *pavailable = (char *) old_stack - (char *) (current + 1);
  551. #else
  552. *pavailable = (char *) (current + 1) + current->size - (char *) old_stack;
  553. #endif
  554. }
  555. else
  556. {
  557. size_t used;
  558. /* We have popped back to the original stack. */
  559. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  560. if ((char *) old_stack >= (char *) __morestack_initial_sp.sp)
  561. used = 0;
  562. else
  563. used = (char *) __morestack_initial_sp.sp - (char *) old_stack;
  564. #else
  565. if ((char *) old_stack <= (char *) __morestack_initial_sp.sp)
  566. used = 0;
  567. else
  568. used = (char *) old_stack - (char *) __morestack_initial_sp.sp;
  569. #endif
  570. if (used > __morestack_initial_sp.len)
  571. *pavailable = 0;
  572. else
  573. *pavailable = __morestack_initial_sp.len - used;
  574. }
  575. return old_stack;
  576. }
  577. /* Block signals while splitting the stack. This avoids trouble if we
  578. try to invoke a signal handler which itself wants to split the
  579. stack. */
  580. extern int pthread_sigmask (int, const sigset_t *, sigset_t *)
  581. __attribute__ ((weak));
  582. void
  583. __morestack_block_signals (void)
  584. {
  585. if (__morestack_initial_sp.dont_block_signals)
  586. ;
  587. else if (pthread_sigmask)
  588. pthread_sigmask (SIG_BLOCK, &__morestack_fullmask,
  589. &__morestack_initial_sp.mask);
  590. else
  591. sigprocmask (SIG_BLOCK, &__morestack_fullmask,
  592. &__morestack_initial_sp.mask);
  593. }
  594. /* Unblock signals while splitting the stack. */
  595. void
  596. __morestack_unblock_signals (void)
  597. {
  598. if (__morestack_initial_sp.dont_block_signals)
  599. ;
  600. else if (pthread_sigmask)
  601. pthread_sigmask (SIG_SETMASK, &__morestack_initial_sp.mask, NULL);
  602. else
  603. sigprocmask (SIG_SETMASK, &__morestack_initial_sp.mask, NULL);
  604. }
  605. /* This function is called to allocate dynamic stack space, for alloca
  606. or a variably sized array. This is a regular function with
  607. sufficient stack space, so we just use malloc to allocate the
  608. space. We attach the allocated blocks to the current stack
  609. segment, so that they will eventually be reused or freed. */
  610. void *
  611. __morestack_allocate_stack_space (size_t size)
  612. {
  613. struct stack_segment *seg, *current;
  614. struct dynamic_allocation_blocks *p;
  615. /* We have to block signals to avoid getting confused if we get
  616. interrupted by a signal whose handler itself uses alloca or a
  617. variably sized array. */
  618. __morestack_block_signals ();
  619. /* Since we don't want to call free while we are low on stack space,
  620. we may have a list of already allocated blocks waiting to be
  621. freed. Release them all, unless we find one that is large
  622. enough. We don't look at every block to see if one is large
  623. enough, just the first one, because we aren't trying to build a
  624. memory allocator here, we're just trying to speed up common
  625. cases. */
  626. current = __morestack_current_segment;
  627. p = NULL;
  628. for (seg = __morestack_segments; seg != NULL; seg = seg->next)
  629. {
  630. p = seg->free_dynamic_allocation;
  631. if (p != NULL)
  632. {
  633. if (p->size >= size)
  634. {
  635. seg->free_dynamic_allocation = p->next;
  636. break;
  637. }
  638. free_dynamic_blocks (p);
  639. seg->free_dynamic_allocation = NULL;
  640. p = NULL;
  641. }
  642. }
  643. if (p == NULL)
  644. {
  645. /* We need to allocate additional memory. */
  646. p = malloc (sizeof (*p));
  647. if (p == NULL)
  648. abort ();
  649. p->size = size;
  650. p->block = malloc (size);
  651. if (p->block == NULL)
  652. abort ();
  653. }
  654. /* If we are still on the initial stack, then we have a space leak.
  655. FIXME. */
  656. if (current != NULL)
  657. {
  658. p->next = current->dynamic_allocation;
  659. current->dynamic_allocation = p;
  660. }
  661. __morestack_unblock_signals ();
  662. return p->block;
  663. }
  664. /* Find the stack segment for STACK and return the amount of space
  665. available. This is used when unwinding the stack because of an
  666. exception, in order to reset the stack guard correctly. */
  667. size_t
  668. __generic_findstack (void *stack)
  669. {
  670. struct stack_segment *pss;
  671. size_t used;
  672. for (pss = __morestack_current_segment; pss != NULL; pss = pss->prev)
  673. {
  674. if ((char *) pss < (char *) stack
  675. && (char *) pss + pss->size > (char *) stack)
  676. {
  677. __morestack_current_segment = pss;
  678. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  679. return (char *) stack - (char *) (pss + 1);
  680. #else
  681. return (char *) (pss + 1) + pss->size - (char *) stack;
  682. #endif
  683. }
  684. }
  685. /* We have popped back to the original stack. */
  686. if (__morestack_initial_sp.sp == NULL)
  687. return 0;
  688. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  689. if ((char *) stack >= (char *) __morestack_initial_sp.sp)
  690. used = 0;
  691. else
  692. used = (char *) __morestack_initial_sp.sp - (char *) stack;
  693. #else
  694. if ((char *) stack <= (char *) __morestack_initial_sp.sp)
  695. used = 0;
  696. else
  697. used = (char *) stack - (char *) __morestack_initial_sp.sp;
  698. #endif
  699. if (used > __morestack_initial_sp.len)
  700. return 0;
  701. else
  702. return __morestack_initial_sp.len - used;
  703. }
  704. /* This function is called at program startup time to make sure that
  705. mmap, munmap, and getpagesize are resolved if linking dynamically.
  706. We want to resolve them while we have enough stack for them, rather
  707. than calling into the dynamic linker while low on stack space.
  708. Similarly, invoke getenv here to check for split-stack related control
  709. variables, since doing do as part of the __morestack path can result
  710. in unwanted use of SSE/AVX registers (see GCC PR 86213). */
  711. void
  712. __morestack_load_mmap (void)
  713. {
  714. /* Call with bogus values to run faster. We don't care if the call
  715. fails. Pass __MORESTACK_CURRENT_SEGMENT to make sure that any
  716. TLS accessor function is resolved. */
  717. mmap (__morestack_current_segment, 0, PROT_READ, MAP_ANONYMOUS, -1, 0);
  718. mprotect (NULL, 0, 0);
  719. munmap (0, static_pagesize);
  720. /* Initialize these values here, so as to avoid dynamic linker
  721. activity as part of a __morestack call. */
  722. static_pagesize = getpagesize();
  723. use_guard_page = getenv ("SPLIT_STACK_GUARD") != 0;
  724. }
  725. /* This function may be used to iterate over the stack segments.
  726. This can be called like this.
  727. void *next_segment = NULL;
  728. void *next_sp = NULL;
  729. void *initial_sp = NULL;
  730. void *stack;
  731. size_t stack_size;
  732. while ((stack = __splitstack_find (next_segment, next_sp, &stack_size,
  733. &next_segment, &next_sp,
  734. &initial_sp)) != NULL)
  735. {
  736. // Stack segment starts at stack and is stack_size bytes long.
  737. }
  738. There is no way to iterate over the stack segments of a different
  739. thread. However, what is permitted is for one thread to call this
  740. with the first two values NULL, to pass next_segment, next_sp, and
  741. initial_sp to a different thread, and then to suspend one way or
  742. another. A different thread may run the subsequent
  743. __morestack_find iterations. Of course, this will only work if the
  744. first thread is suspended during the __morestack_find iterations.
  745. If not, the second thread will be looking at the stack while it is
  746. changing, and anything could happen.
  747. FIXME: This should be declared in some header file, but where? */
  748. void *
  749. __splitstack_find (void *segment_arg, void *sp, size_t *len,
  750. void **next_segment, void **next_sp,
  751. void **initial_sp)
  752. {
  753. struct stack_segment *segment;
  754. void *ret;
  755. char *nsp;
  756. if (segment_arg == (void *) (uintptr_type) 1)
  757. {
  758. char *isp = (char *) *initial_sp;
  759. if (isp == NULL)
  760. return NULL;
  761. *next_segment = (void *) (uintptr_type) 2;
  762. *next_sp = NULL;
  763. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  764. if ((char *) sp >= isp)
  765. return NULL;
  766. *len = (char *) isp - (char *) sp;
  767. return sp;
  768. #else
  769. if ((char *) sp <= (char *) isp)
  770. return NULL;
  771. *len = (char *) sp - (char *) isp;
  772. return (void *) isp;
  773. #endif
  774. }
  775. else if (segment_arg == (void *) (uintptr_type) 2)
  776. return NULL;
  777. else if (segment_arg != NULL)
  778. segment = (struct stack_segment *) segment_arg;
  779. else
  780. {
  781. *initial_sp = __morestack_initial_sp.sp;
  782. segment = __morestack_current_segment;
  783. sp = (void *) &segment;
  784. while (1)
  785. {
  786. if (segment == NULL)
  787. return __splitstack_find ((void *) (uintptr_type) 1, sp, len,
  788. next_segment, next_sp, initial_sp);
  789. if ((char *) sp >= (char *) (segment + 1)
  790. && (char *) sp <= (char *) (segment + 1) + segment->size)
  791. break;
  792. segment = segment->prev;
  793. }
  794. }
  795. if (segment->prev == NULL)
  796. *next_segment = (void *) (uintptr_type) 1;
  797. else
  798. *next_segment = segment->prev;
  799. /* The old_stack value is the address of the function parameters of
  800. the function which called __morestack. So if f1 called f2 which
  801. called __morestack, the stack looks like this:
  802. parameters <- old_stack
  803. return in f1
  804. return in f2
  805. registers pushed by __morestack
  806. The registers pushed by __morestack may not be visible on any
  807. other stack, if we are being called by a signal handler
  808. immediately after the call to __morestack_unblock_signals. We
  809. want to adjust our return value to include those registers. This
  810. is target dependent. */
  811. nsp = (char *) segment->old_stack;
  812. if (nsp == NULL)
  813. {
  814. /* We've reached the top of the stack. */
  815. *next_segment = (void *) (uintptr_type) 2;
  816. }
  817. else
  818. {
  819. #if defined (__x86_64__)
  820. nsp -= 12 * sizeof (void *);
  821. #elif defined (__i386__)
  822. nsp -= 6 * sizeof (void *);
  823. #elif defined __powerpc64__
  824. #elif defined __s390x__
  825. nsp -= 2 * 160;
  826. #elif defined __s390__
  827. nsp -= 2 * 96;
  828. #else
  829. #error "unrecognized target"
  830. #endif
  831. *next_sp = (void *) nsp;
  832. }
  833. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  834. *len = (char *) (segment + 1) + segment->size - (char *) sp;
  835. ret = (void *) sp;
  836. #else
  837. *len = (char *) sp - (char *) (segment + 1);
  838. ret = (void *) (segment + 1);
  839. #endif
  840. return ret;
  841. }
  842. /* Tell the split stack code whether it has to block signals while
  843. manipulating the stack. This is for programs in which some threads
  844. block all signals. If a thread already blocks signals, there is no
  845. need for the split stack code to block them as well. If NEW is not
  846. NULL, then if *NEW is non-zero signals will be blocked while
  847. splitting the stack, otherwise they will not. If OLD is not NULL,
  848. *OLD will be set to the old value. */
  849. void
  850. __splitstack_block_signals (int *new, int *old)
  851. {
  852. if (old != NULL)
  853. *old = __morestack_initial_sp.dont_block_signals ? 0 : 1;
  854. if (new != NULL)
  855. __morestack_initial_sp.dont_block_signals = *new ? 0 : 1;
  856. }
  857. /* The offsets into the arrays used by __splitstack_getcontext and
  858. __splitstack_setcontext. */
  859. enum __splitstack_context_offsets
  860. {
  861. MORESTACK_SEGMENTS = 0,
  862. CURRENT_SEGMENT = 1,
  863. CURRENT_STACK = 2,
  864. STACK_GUARD = 3,
  865. INITIAL_SP = 4,
  866. INITIAL_SP_LEN = 5,
  867. BLOCK_SIGNALS = 6,
  868. NUMBER_OFFSETS = 10
  869. };
  870. /* Get the current split stack context. This may be used for
  871. coroutine switching, similar to getcontext. The argument should
  872. have at least 10 void *pointers for extensibility, although we
  873. don't currently use all of them. This would normally be called
  874. immediately before a call to getcontext or swapcontext or
  875. setjmp. */
  876. void
  877. __splitstack_getcontext (void *context[NUMBER_OFFSETS])
  878. {
  879. memset (context, 0, NUMBER_OFFSETS * sizeof (void *));
  880. context[MORESTACK_SEGMENTS] = (void *) __morestack_segments;
  881. context[CURRENT_SEGMENT] = (void *) __morestack_current_segment;
  882. context[CURRENT_STACK] = (void *) &context;
  883. context[STACK_GUARD] = __morestack_get_guard ();
  884. context[INITIAL_SP] = (void *) __morestack_initial_sp.sp;
  885. context[INITIAL_SP_LEN] = (void *) (uintptr_type) __morestack_initial_sp.len;
  886. context[BLOCK_SIGNALS] = (void *) __morestack_initial_sp.dont_block_signals;
  887. }
  888. /* Set the current split stack context. The argument should be a
  889. context previously passed to __splitstack_getcontext. This would
  890. normally be called immediately after a call to getcontext or
  891. swapcontext or setjmp if something jumped to it. */
  892. void
  893. __splitstack_setcontext (void *context[NUMBER_OFFSETS])
  894. {
  895. __morestack_segments = (struct stack_segment *) context[MORESTACK_SEGMENTS];
  896. __morestack_current_segment =
  897. (struct stack_segment *) context[CURRENT_SEGMENT];
  898. __morestack_set_guard (context[STACK_GUARD]);
  899. __morestack_initial_sp.sp = context[INITIAL_SP];
  900. __morestack_initial_sp.len = (size_t) context[INITIAL_SP_LEN];
  901. __morestack_initial_sp.dont_block_signals =
  902. (uintptr_type) context[BLOCK_SIGNALS];
  903. }
  904. /* Create a new split stack context. This will allocate a new stack
  905. segment which may be used by a coroutine. STACK_SIZE is the
  906. minimum size of the new stack. The caller is responsible for
  907. actually setting the stack pointer. This would normally be called
  908. before a call to makecontext, and the returned stack pointer and
  909. size would be used to set the uc_stack field. A function called
  910. via makecontext on a stack created by __splitstack_makecontext may
  911. not return. Note that the returned pointer points to the lowest
  912. address in the stack space, and thus may not be the value to which
  913. to set the stack pointer. */
  914. void *
  915. __splitstack_makecontext (size_t stack_size, void *context[NUMBER_OFFSETS],
  916. size_t *size)
  917. {
  918. struct stack_segment *segment;
  919. void *initial_sp;
  920. memset (context, 0, NUMBER_OFFSETS * sizeof (void *));
  921. segment = allocate_segment (stack_size);
  922. context[MORESTACK_SEGMENTS] = segment;
  923. context[CURRENT_SEGMENT] = segment;
  924. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  925. initial_sp = (void *) ((char *) (segment + 1) + segment->size);
  926. #else
  927. initial_sp = (void *) (segment + 1);
  928. #endif
  929. context[STACK_GUARD] = __morestack_make_guard (initial_sp, segment->size);
  930. context[INITIAL_SP] = NULL;
  931. context[INITIAL_SP_LEN] = 0;
  932. *size = segment->size;
  933. return (void *) (segment + 1);
  934. }
  935. /* Given an existing split stack context, reset it back to the start
  936. of the stack. Return the stack pointer and size, appropriate for
  937. use with makecontext. This may be used if a coroutine exits, in
  938. order to reuse the stack segments for a new coroutine. */
  939. void *
  940. __splitstack_resetcontext (void *context[10], size_t *size)
  941. {
  942. struct stack_segment *segment;
  943. void *initial_sp;
  944. size_t initial_size;
  945. void *ret;
  946. /* Reset the context assuming that MORESTACK_SEGMENTS, INITIAL_SP
  947. and INITIAL_SP_LEN are correct. */
  948. segment = context[MORESTACK_SEGMENTS];
  949. context[CURRENT_SEGMENT] = segment;
  950. context[CURRENT_STACK] = NULL;
  951. if (segment == NULL)
  952. {
  953. initial_sp = context[INITIAL_SP];
  954. initial_size = (uintptr_type) context[INITIAL_SP_LEN];
  955. ret = initial_sp;
  956. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  957. ret = (void *) ((char *) ret - initial_size);
  958. #endif
  959. }
  960. else
  961. {
  962. #ifdef __LIBGCC_STACK_GROWS_DOWNWARD__
  963. initial_sp = (void *) ((char *) (segment + 1) + segment->size);
  964. #else
  965. initial_sp = (void *) (segment + 1);
  966. #endif
  967. initial_size = segment->size;
  968. ret = (void *) (segment + 1);
  969. }
  970. context[STACK_GUARD] = __morestack_make_guard (initial_sp, initial_size);
  971. context[BLOCK_SIGNALS] = NULL;
  972. *size = initial_size;
  973. return ret;
  974. }
  975. /* Release all the memory associated with a splitstack context. This
  976. may be used if a coroutine exits and the associated stack should be
  977. freed. */
  978. void
  979. __splitstack_releasecontext (void *context[10])
  980. {
  981. __morestack_release_segments (((struct stack_segment **)
  982. &context[MORESTACK_SEGMENTS]),
  983. 1);
  984. }
  985. /* Like __splitstack_block_signals, but operating on CONTEXT, rather
  986. than on the current state. */
  987. void
  988. __splitstack_block_signals_context (void *context[NUMBER_OFFSETS], int *new,
  989. int *old)
  990. {
  991. if (old != NULL)
  992. *old = ((uintptr_type) context[BLOCK_SIGNALS]) != 0 ? 0 : 1;
  993. if (new != NULL)
  994. context[BLOCK_SIGNALS] = (void *) (uintptr_type) (*new ? 0 : 1);
  995. }
  996. /* Find the stack segments associated with a split stack context.
  997. This will return the address of the first stack segment and set
  998. *STACK_SIZE to its size. It will set next_segment, next_sp, and
  999. initial_sp which may be passed to __splitstack_find to find the
  1000. remaining segments. */
  1001. void *
  1002. __splitstack_find_context (void *context[NUMBER_OFFSETS], size_t *stack_size,
  1003. void **next_segment, void **next_sp,
  1004. void **initial_sp)
  1005. {
  1006. void *sp;
  1007. struct stack_segment *segment;
  1008. *initial_sp = context[INITIAL_SP];
  1009. sp = context[CURRENT_STACK];
  1010. if (sp == NULL)
  1011. {
  1012. /* Most likely this context was created but was never used. The
  1013. value 2 is a code used by __splitstack_find to mean that we
  1014. have reached the end of the list of stacks. */
  1015. *next_segment = (void *) (uintptr_type) 2;
  1016. *next_sp = NULL;
  1017. *initial_sp = NULL;
  1018. return NULL;
  1019. }
  1020. segment = context[CURRENT_SEGMENT];
  1021. if (segment == NULL)
  1022. {
  1023. /* Most likely this context was saved by a thread which was not
  1024. created using __splistack_makecontext and which has never
  1025. split the stack. The value 1 is a code used by
  1026. __splitstack_find to look at the initial stack. */
  1027. segment = (struct stack_segment *) (uintptr_type) 1;
  1028. }
  1029. return __splitstack_find (segment, sp, stack_size, next_segment, next_sp,
  1030. initial_sp);
  1031. }
  1032. #endif /* !defined (inhibit_libc) */
  1033. #endif /* not powerpc 32-bit */