1 /* Library support for -fsplit-stack. */
2 /* Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Ian Lance Taylor <iant@google.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
31 /* If inhibit_libc is defined, we can not compile this file. The
32 effect is that people will not be able to use -fsplit-stack. That
33 is much better than failing the build particularly since people
34 will want to define inhibit_libc while building a compiler which
47 #include "generic-morestack.h"
49 /* This file contains subroutines that are used by code compiled with
52 /* Declare functions to avoid warnings--there is no header file for
53 these internal functions. We give most of these functions the
54 flatten attribute in order to minimize their stack usage--here we
55 must minimize stack usage even at the cost of code size, and in
56 general inlining everything will do that. */
59 __generic_morestack_set_initial_sp (void *sp
, size_t len
)
60 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
63 __generic_morestack (size_t *frame_size
, void *old_stack
, size_t param_size
)
64 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
67 __generic_releasestack (size_t *pavailable
)
68 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
71 __morestack_block_signals (void)
72 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
75 __morestack_unblock_signals (void)
76 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
79 __generic_findstack (void *stack
)
80 __attribute__ ((no_split_stack
, flatten
, visibility ("hidden")));
83 __morestack_load_mmap (void)
84 __attribute__ ((no_split_stack
, visibility ("hidden")));
87 __morestack_allocate_stack_space (size_t size
)
88 __attribute__ ((visibility ("hidden")));
90 /* This is a function which -fsplit-stack code can call to get a list
91 of the stacks. Since it is not called only by the compiler, it is
95 __splitstack_find (void *, void *, size_t *, void **, void **, void **)
96 __attribute__ ((visibility ("default")));
98 /* When we allocate a stack segment we put this header at the
103 /* The previous stack segment--when a function running on this stack
104 segment returns, it will run on the previous one. */
105 struct stack_segment
*prev
;
106 /* The next stack segment, if it has been allocated--when a function
107 is running on this stack segment, the next one is not being
109 struct stack_segment
*next
;
110 /* The total size of this stack segment. */
112 /* The stack address when this stack was created. This is used when
113 popping the stack. */
115 /* A list of memory blocks allocated by dynamic stack
117 struct dynamic_allocation_blocks
*dynamic_allocation
;
118 /* A list of dynamic memory blocks no longer needed. */
119 struct dynamic_allocation_blocks
*free_dynamic_allocation
;
120 /* An extra pointer in case we need some more information some
125 /* This structure holds the (approximate) initial stack pointer and
126 size for the system supplied stack for a thread. This is set when
127 the thread is created. We also store a sigset_t here to hold the
128 signal mask while splitting the stack, since we don't want to store
129 that on the stack. */
133 /* The initial stack pointer. */
135 /* The stack length. */
137 /* A signal mask, put here so that the thread can use it without
138 needing stack space. */
140 /* Some extra space for later extensibility. */
144 /* A list of memory blocks allocated by dynamic stack allocation.
145 This is used for code that calls alloca or uses variably sized
148 struct dynamic_allocation_blocks
150 /* The next block in the list. */
151 struct dynamic_allocation_blocks
*next
;
152 /* The size of the allocated memory. */
154 /* The allocated memory. */
158 /* These thread local global variables must be shared by all split
159 stack code across shared library boundaries. Therefore, they have
160 default visibility. They have extensibility fields if needed for
161 new versions. If more radical changes are needed, new code can be
162 written using new variable names, while still using the existing
163 variables in a backward compatible manner. Symbol versioning is
164 also used, although, since these variables are only referenced by
165 code in this file and generic-morestack-thread.c, it is likely that
166 simply using new names will suffice. */
168 /* The first stack segment allocated for this thread. */
170 __thread
struct stack_segment
*__morestack_segments
171 __attribute__ ((visibility ("default")));
173 /* The stack segment that we think we are currently using. This will
174 be correct in normal usage, but will be incorrect if an exception
175 unwinds into a different stack segment or if longjmp jumps to a
176 different stack segment. */
178 __thread
struct stack_segment
*__morestack_current_segment
179 __attribute__ ((visibility ("default")));
181 /* The initial stack pointer and size for this thread. */
183 __thread
struct initial_sp __morestack_initial_sp
184 __attribute__ ((visibility ("default")));
186 /* A static signal mask, to avoid taking up stack space. */
188 static sigset_t __morestack_fullmask
;
190 /* Convert an integer to a decimal string without using much stack
191 space. Return a pointer to the part of the buffer to use. We this
192 instead of sprintf because sprintf will require too much stack
196 print_int (int val
, char *buf
, int buflen
, size_t *print_len
)
202 uval
= (unsigned int) val
;
215 buf
[i
] = '0' + (uval
% 10);
218 while (uval
!= 0 && i
> 0);
227 *print_len
= buflen
- i
;
231 /* Print the string MSG/LEN, the errno number ERR, and a newline on
232 stderr. Then crash. */
235 __morestack_fail (const char *, size_t, int) __attribute__ ((noreturn
));
238 __morestack_fail (const char *msg
, size_t len
, int err
)
241 static const char nl
[] = "\n";
243 union { char *p
; const char *cp
; } const_cast;
246 iov
[0].iov_base
= const_cast.p
;
247 iov
[0].iov_len
= len
;
248 /* We can't call strerror, because it may try to translate the error
249 message, and that would use too much stack space. */
250 iov
[1].iov_base
= print_int (err
, buf
, sizeof buf
, &iov
[1].iov_len
);
251 const_cast.cp
= &nl
[0];
252 iov
[2].iov_base
= const_cast.p
;
253 iov
[2].iov_len
= sizeof nl
- 1;
254 /* FIXME: On systems without writev we need to issue three write
255 calls, or punt on printing errno. For now this is irrelevant
256 since stack splitting only works on GNU/Linux anyhow. */
261 /* Allocate a new stack segment. FRAME_SIZE is the required frame
264 static struct stack_segment
*
265 allocate_segment (size_t frame_size
)
267 static unsigned int static_pagesize
;
268 static int use_guard_page
;
269 unsigned int pagesize
;
270 unsigned int overhead
;
271 unsigned int allocate
;
273 struct stack_segment
*pss
;
275 pagesize
= static_pagesize
;
280 pagesize
= getpagesize ();
282 #ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
283 p
= __sync_val_compare_and_swap (&static_pagesize
, 0, pagesize
);
285 /* Just hope this assignment is atomic. */
286 static_pagesize
= pagesize
;
290 use_guard_page
= getenv ("SPLIT_STACK_GUARD") != 0;
292 /* FIXME: I'm not sure this assert should be in the released
294 assert (p
== 0 || p
== pagesize
);
297 overhead
= sizeof (struct stack_segment
);
300 if (allocate
< MINSIGSTKSZ
)
301 allocate
= ((MINSIGSTKSZ
+ overhead
+ pagesize
- 1)
303 if (allocate
< frame_size
)
304 allocate
= ((frame_size
+ overhead
+ pagesize
- 1)
308 allocate
+= pagesize
;
310 /* FIXME: If this binary requires an executable stack, then we need
311 to set PROT_EXEC. Unfortunately figuring that out is complicated
312 and target dependent. We would need to use dl_iterate_phdr to
313 see if there is any object which does not have a PT_GNU_STACK
314 phdr, though only for architectures which use that mechanism. */
315 space
= mmap (NULL
, allocate
, PROT_READ
| PROT_WRITE
,
316 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
317 if (space
== MAP_FAILED
)
319 static const char msg
[] =
320 "unable to allocate additional stack space: errno ";
321 __morestack_fail (msg
, sizeof msg
- 1, errno
);
328 #ifdef STACK_GROWS_DOWNWARD
330 space
= (char *) space
+ pagesize
;
332 guard
= space
+ allocate
- pagesize
;
335 mprotect (guard
, pagesize
, PROT_NONE
);
336 allocate
-= pagesize
;
339 pss
= (struct stack_segment
*) space
;
341 pss
->prev
= __morestack_current_segment
;
343 pss
->size
= allocate
- overhead
;
344 pss
->dynamic_allocation
= NULL
;
345 pss
->free_dynamic_allocation
= NULL
;
348 if (__morestack_current_segment
!= NULL
)
349 __morestack_current_segment
->next
= pss
;
351 __morestack_segments
= pss
;
356 /* Free a list of dynamic blocks. */
359 free_dynamic_blocks (struct dynamic_allocation_blocks
*p
)
363 struct dynamic_allocation_blocks
*next
;
372 /* Merge two lists of dynamic blocks. */
374 static struct dynamic_allocation_blocks
*
375 merge_dynamic_blocks (struct dynamic_allocation_blocks
*a
,
376 struct dynamic_allocation_blocks
*b
)
378 struct dynamic_allocation_blocks
**pp
;
384 for (pp
= &a
->next
; *pp
!= NULL
; pp
= &(*pp
)->next
)
390 /* Release stack segments. If FREE_DYNAMIC is non-zero, we also free
391 any dynamic blocks. Otherwise we return them. */
393 struct dynamic_allocation_blocks
*
394 __morestack_release_segments (struct stack_segment
**pp
, int free_dynamic
)
396 struct dynamic_allocation_blocks
*ret
;
397 struct stack_segment
*pss
;
403 struct stack_segment
*next
;
404 unsigned int allocate
;
408 if (pss
->dynamic_allocation
!= NULL
409 || pss
->free_dynamic_allocation
!= NULL
)
413 free_dynamic_blocks (pss
->dynamic_allocation
);
414 free_dynamic_blocks (pss
->free_dynamic_allocation
);
418 ret
= merge_dynamic_blocks (pss
->dynamic_allocation
, ret
);
419 ret
= merge_dynamic_blocks (pss
->free_dynamic_allocation
, ret
);
423 allocate
= pss
->size
+ sizeof (struct stack_segment
);
424 if (munmap (pss
, allocate
) < 0)
426 static const char msg
[] = "munmap of stack space failed: errno ";
427 __morestack_fail (msg
, sizeof msg
- 1, errno
);
437 /* This function is called by a processor specific function to set the
438 initial stack pointer for a thread. The operating system will
439 always create a stack for a thread. Here we record a stack pointer
440 near the base of that stack. The size argument lets the processor
441 specific code estimate how much stack space is available on this
445 __generic_morestack_set_initial_sp (void *sp
, size_t len
)
447 /* The stack pointer most likely starts on a page boundary. Adjust
448 to the nearest 512 byte boundary. It's not essential that we be
449 precise here; getting it wrong will just leave some stack space
451 #ifdef STACK_GROWS_DOWNWARD
452 sp
= (void *) ((((__UINTPTR_TYPE__
) sp
+ 511U) / 512U) * 512U);
454 sp
= (void *) ((((__UINTPTR_TYPE__
) sp
- 511U) / 512U) * 512U);
457 __morestack_initial_sp
.sp
= sp
;
458 __morestack_initial_sp
.len
= len
;
459 sigemptyset (&__morestack_initial_sp
.mask
);
461 sigfillset (&__morestack_fullmask
);
463 /* On Linux, the first two real time signals are used by the NPTL
464 threading library. By taking them out of the set of signals, we
465 avoiding copying the signal mask in pthread_sigmask. More
466 importantly, pthread_sigmask uses less stack space on x86_64. */
467 sigdelset (&__morestack_fullmask
, __SIGRTMIN
);
468 sigdelset (&__morestack_fullmask
, __SIGRTMIN
+ 1);
472 /* This function is called by a processor specific function which is
473 run in the prologue when more stack is needed. The processor
474 specific function handles the details of saving registers and
475 frobbing the actual stack pointer. This function is responsible
476 for allocating a new stack segment and for copying a parameter
477 block from the old stack to the new one. On function entry
478 *PFRAME_SIZE is the size of the required stack frame--the returned
479 stack must be at least this large. On function exit *PFRAME_SIZE
480 is the amount of space remaining on the allocated stack. OLD_STACK
481 points at the parameters the old stack (really the current one
482 while this function is running). OLD_STACK is saved so that it can
483 be returned by a later call to __generic_releasestack. PARAM_SIZE
484 is the size in bytes of parameters to copy to the new stack. This
485 function returns a pointer to the new stack segment, pointing to
486 the memory after the parameters have been copied. The returned
487 value minus the returned *PFRAME_SIZE (or plus if the stack grows
488 upward) is the first address on the stack which should not be used.
490 This function is running on the old stack and has only a limited
491 amount of stack space available. */
494 __generic_morestack (size_t *pframe_size
, void *old_stack
, size_t param_size
)
496 size_t frame_size
= *pframe_size
;
497 struct stack_segment
*current
;
498 struct stack_segment
**pp
;
499 struct dynamic_allocation_blocks
*dynamic
;
505 current
= __morestack_current_segment
;
507 pp
= current
!= NULL
? ¤t
->next
: &__morestack_segments
;
508 if (*pp
!= NULL
&& (*pp
)->size
< frame_size
)
509 dynamic
= __morestack_release_segments (pp
, 0);
515 current
= allocate_segment (frame_size
);
517 current
->old_stack
= old_stack
;
519 __morestack_current_segment
= current
;
523 /* Move the free blocks onto our list. We don't want to call
524 free here, as we are short on stack space. */
525 current
->free_dynamic_allocation
=
526 merge_dynamic_blocks (dynamic
, current
->free_dynamic_allocation
);
529 *pframe_size
= current
->size
- param_size
;
531 #ifdef STACK_GROWS_DOWNWARD
533 char *bottom
= (char *) (current
+ 1) + current
->size
;
534 to
= bottom
- param_size
;
535 ret
= bottom
- param_size
;
539 ret
= (char *) (current
+ 1) + param_size
;
542 /* We don't call memcpy to avoid worrying about the dynamic linker
543 trying to resolve it. */
544 from
= (char *) old_stack
;
545 for (i
= 0; i
< param_size
; i
++)
551 /* This function is called by a processor specific function when it is
552 ready to release a stack segment. We don't actually release the
553 stack segment, we just move back to the previous one. The current
554 stack segment will still be available if we need it in
555 __generic_morestack. This returns a pointer to the new stack
556 segment to use, which is the one saved by a previous call to
557 __generic_morestack. The processor specific function is then
558 responsible for actually updating the stack pointer. This sets
559 *PAVAILABLE to the amount of stack space now available. */
562 __generic_releasestack (size_t *pavailable
)
564 struct stack_segment
*current
;
567 current
= __morestack_current_segment
;
568 old_stack
= current
->old_stack
;
569 current
= current
->prev
;
570 __morestack_current_segment
= current
;
574 #ifdef STACK_GROWS_DOWNWARD
575 *pavailable
= (char *) old_stack
- (char *) (current
+ 1);
577 *pavailable
= (char *) (current
+ 1) + current
->size
- (char *) old_stack
;
584 /* We have popped back to the original stack. */
585 #ifdef STACK_GROWS_DOWNWARD
586 if ((char *) old_stack
>= (char *) __morestack_initial_sp
.sp
)
589 used
= (char *) __morestack_initial_sp
.sp
- (char *) old_stack
;
591 if ((char *) old_stack
<= (char *) __morestack_initial_sp
.sp
)
594 used
= (char *) old_stack
- (char *) __morestack_initial_sp
.sp
;
597 if (used
> __morestack_initial_sp
.len
)
600 *pavailable
= __morestack_initial_sp
.len
- used
;
606 /* Block signals while splitting the stack. This avoids trouble if we
607 try to invoke a signal handler which itself wants to split the
610 extern int pthread_sigmask (int, const sigset_t
*, sigset_t
*)
611 __attribute__ ((weak
));
614 __morestack_block_signals (void)
617 pthread_sigmask (SIG_BLOCK
, &__morestack_fullmask
,
618 &__morestack_initial_sp
.mask
);
620 sigprocmask (SIG_BLOCK
, &__morestack_fullmask
,
621 &__morestack_initial_sp
.mask
);
624 /* Unblock signals while splitting the stack. */
627 __morestack_unblock_signals (void)
630 pthread_sigmask (SIG_SETMASK
, &__morestack_initial_sp
.mask
, NULL
);
632 sigprocmask (SIG_SETMASK
, &__morestack_initial_sp
.mask
, NULL
);
635 /* This function is called to allocate dynamic stack space, for alloca
636 or a variably sized array. This is a regular function with
637 sufficient stack space, so we just use malloc to allocate the
638 space. We attach the allocated blocks to the current stack
639 segment, so that they will eventually be reused or freed. */
642 __morestack_allocate_stack_space (size_t size
)
644 struct stack_segment
*seg
, *current
;
645 struct dynamic_allocation_blocks
*p
;
647 /* We have to block signals to avoid getting confused if we get
648 interrupted by a signal whose handler itself uses alloca or a
649 variably sized array. */
650 __morestack_block_signals ();
652 /* Since we don't want to call free while we are low on stack space,
653 we may have a list of already allocated blocks waiting to be
654 freed. Release them all, unless we find one that is large
655 enough. We don't look at every block to see if one is large
656 enough, just the first one, because we aren't trying to build a
657 memory allocator here, we're just trying to speed up common
660 current
= __morestack_current_segment
;
662 for (seg
= __morestack_segments
; seg
!= NULL
; seg
= seg
->next
)
664 p
= seg
->free_dynamic_allocation
;
669 seg
->free_dynamic_allocation
= p
->next
;
673 free_dynamic_blocks (p
);
674 seg
->free_dynamic_allocation
= NULL
;
681 /* We need to allocate additional memory. */
682 p
= malloc (sizeof (*p
));
686 p
->block
= malloc (size
);
687 if (p
->block
== NULL
)
691 /* If we are still on the initial stack, then we have a space leak.
695 p
->next
= current
->dynamic_allocation
;
696 current
->dynamic_allocation
= p
;
699 __morestack_unblock_signals ();
704 /* Find the stack segment for STACK and return the amount of space
705 available. This is used when unwinding the stack because of an
706 exception, in order to reset the stack guard correctly. */
709 __generic_findstack (void *stack
)
711 struct stack_segment
*pss
;
714 for (pss
= __morestack_current_segment
; pss
!= NULL
; pss
= pss
->prev
)
716 if ((char *) pss
< (char *) stack
717 && (char *) pss
+ pss
->size
> (char *) stack
)
719 __morestack_current_segment
= pss
;
720 #ifdef STACK_GROWS_DOWNWARD
721 return (char *) stack
- (char *) (pss
+ 1);
723 return (char *) (pss
+ 1) + pss
->size
- (char *) stack
;
728 /* We have popped back to the original stack. */
729 #ifdef STACK_GROWS_DOWNWARD
730 if ((char *) stack
>= (char *) __morestack_initial_sp
.sp
)
733 used
= (char *) __morestack_initial_sp
.sp
- (char *) stack
;
735 if ((char *) stack
<= (char *) __morestack_initial_sp
.sp
)
738 used
= (char *) stack
- (char *) __morestack_initial_sp
.sp
;
741 if (used
> __morestack_initial_sp
.len
)
744 return __morestack_initial_sp
.len
- used
;
747 /* This function is called at program startup time to make sure that
748 mmap, munmap, and getpagesize are resolved if linking dynamically.
749 We want to resolve them while we have enough stack for them, rather
750 than calling into the dynamic linker while low on stack space. */
753 __morestack_load_mmap (void)
755 /* Call with bogus values to run faster. We don't care if the call
756 fails. Pass __MORESTACK_CURRENT_SEGMENT to make sure that any
757 TLS accessor function is resolved. */
758 mmap (__morestack_current_segment
, 0, PROT_READ
, MAP_ANONYMOUS
, -1, 0);
759 mprotect (NULL
, 0, 0);
760 munmap (0, getpagesize ());
763 /* This function may be used to iterate over the stack segments.
764 This can be called like this.
765 void *next_segment = NULL;
766 void *next_sp = NULL;
767 void *initial_sp = NULL;
770 while ((stack = __splitstack_find (next_segment, next_sp, &stack_size,
771 &next_segment, &next_sp,
772 &initial_sp)) != NULL)
774 // Stack segment starts at stack and is stack_size bytes long.
777 There is no way to iterate over the stack segments of a different
778 thread. However, what is permitted is for one thread to call this
779 with the first two values NULL, to pass next_segment, next_sp, and
780 initial_sp to a different thread, and then to suspend one way or
781 another. A different thread may run the subsequent
782 __morestack_find iterations. Of course, this will only work if the
783 first thread is suspended during the __morestack_find iterations.
784 If not, the second thread will be looking at the stack while it is
785 changing, and anything could happen.
787 FIXME: This should be declared in some header file, but where? */
790 __splitstack_find (void *segment_arg
, void *sp
, size_t *len
,
791 void **next_segment
, void **next_sp
,
794 struct stack_segment
*segment
;
798 if (segment_arg
== (void *) 1)
800 char *isp
= (char *) *initial_sp
;
802 *next_segment
= (void *) 2;
804 #ifdef STACK_GROWS_DOWNWARD
805 if ((char *) sp
>= isp
)
807 *len
= (char *) isp
- (char *) sp
;
810 if ((char *) sp
<= (char *) isp
)
812 *len
= (char *) sp
- (char *) isp
;
816 else if (segment_arg
== (void *) 2)
818 else if (segment_arg
!= NULL
)
819 segment
= (struct stack_segment
*) segment_arg
;
822 *initial_sp
= __morestack_initial_sp
.sp
;
823 segment
= __morestack_current_segment
;
824 sp
= (void *) &segment
;
828 return __splitstack_find ((void *) 1, sp
, len
, next_segment
,
829 next_sp
, initial_sp
);
830 if ((char *) sp
>= (char *) (segment
+ 1)
831 && (char *) sp
<= (char *) (segment
+ 1) + segment
->size
)
833 segment
= segment
->prev
;
837 if (segment
->prev
== NULL
)
838 *next_segment
= (void *) 1;
840 *next_segment
= segment
->prev
;
842 /* The old_stack value is the address of the function parameters of
843 the function which called __morestack. So if f1 called f2 which
844 called __morestack, the stack looks like this:
846 parameters <- old_stack
849 data pushed by __morestack
851 On x86, the data pushed by __morestack includes the saved value
852 of the ebp/rbp register. We want our caller to be able to see
853 that value, which can not be found on any other stack. So we
854 adjust accordingly. This may need to be tweaked for other
857 nsp
= (char *) segment
->old_stack
;
858 #ifdef STACK_GROWS_DOWNWARD
859 nsp
-= 3 * sizeof (void *);
861 nsp
+= 3 * sizeof (void *);
863 *next_sp
= (void *) nsp
;
865 #ifdef STACK_GROWS_DOWNWARD
866 *len
= (char *) (segment
+ 1) + segment
->size
- (char *) sp
;
869 *len
= (char *) sp
- (char *) (segment
+ 1);
870 ret
= (void *) (segment
+ 1);
876 #endif /* !defined (inhibit_libc) */