2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_attr_setguardsize 3 (date) "Linux man-pages (unreleased)"
9 pthread_attr_setguardsize, pthread_attr_getguardsize \- set/get guard size
10 attribute in thread attributes object
13 .RI ( libpthread ", " \-lpthread )
16 .B #include <pthread.h>
18 .BI "int pthread_attr_setguardsize(pthread_attr_t *" attr \
19 ", size_t " guardsize );
20 .BI "int pthread_attr_getguardsize(const pthread_attr_t *restrict " attr ,
21 .BI " size_t *restrict " guardsize );
25 .BR pthread_attr_setguardsize ()
26 function sets the guard size attribute of the
27 thread attributes object referred to by
29 to the value specified in
35 then for each new thread created using
37 the system allocates an additional region of at least
39 bytes at the end of the thread's stack to act as the guard area
40 for the stack (but see BUGS).
44 is 0, then new threads created with
46 will not have a guard area.
48 The default guard size is the same as the system page size.
50 If the stack address attribute has been set in
53 .BR pthread_attr_setstack (3)
55 .BR pthread_attr_setstackaddr (3)),
56 meaning that the caller is allocating the thread's stack,
57 then the guard size attribute is ignored
58 (i.e., no guard area is created by the system):
59 it is the application's responsibility to handle stack overflow
62 to manually define a guard area at the end of the stack
63 that it has allocated).
66 .BR pthread_attr_getguardsize ()
67 function returns the guard size attribute of the
68 thread attributes object referred to by
70 in the buffer pointed to by
73 On success, these functions return 0;
74 on error, they return a nonzero error number.
83 On Linux these functions always succeed
84 (but portable and future-proof applications should nevertheless
85 handle a possible error return).
87 For an explanation of the terms used in this section, see
93 Interface Attribute Value
97 .BR pthread_attr_setguardsize (),
98 .BR pthread_attr_getguardsize ()
99 T} Thread safety MT-Safe
107 A guard area consists of virtual memory pages that are protected
108 to prevent read and write access.
109 If a thread overflows its stack into the guard area,
110 then, on most hard architectures, it receives a
112 signal, thus notifying it of the overflow.
113 Guard areas start on page boundaries,
114 and the guard size is internally rounded up to
115 the system page size when creating a thread.
117 .BR pthread_attr_getguardsize ()
118 returns the guard size that was set by
119 .BR pthread_attr_setguardsize ().)
121 Setting a guard size of 0 may be useful to save memory
122 in an application that creates many threads
123 and knows that stack overflow can never occur.
125 Choosing a guard size larger than the default size
126 may be necessary for detecting stack overflows
127 if a thread allocates large data structures on the stack.
129 As at glibc 2.8, the NPTL threading implementation includes
130 the guard area within the stack size allocation,
131 rather than allocating extra space at the end of the stack,
133 (This can result in an
136 .BR pthread_create (3)
137 if the guard size value is too large,
138 leaving no space for the actual stack.)
140 The obsolete LinuxThreads implementation did the right thing,
141 allocating extra space at the end of the stack for the guard area.
142 .\" glibc includes the guardsize within the allocated stack size,
143 .\" which looks pretty clearly to be in violation of POSIX.
145 .\" Filed bug, 22 Oct 2008:
146 .\" https://www.sourceware.org/bugzilla/show_bug.cgi?id=6973
149 .\" https//bugzilla.redhat.com/show_bug.cgi?id=435337
150 .\" Reportedly, LinuxThreads did the right thing, allocating
151 .\" extra space at the end of the stack:
152 .\" http://sourceware.org/ml/libc-alpha/2008-05/msg00086.html
155 .BR pthread_getattr_np (3).
159 .BR pthread_attr_init (3),
160 .BR pthread_attr_setstack (3),
161 .BR pthread_attr_setstacksize (3),
162 .BR pthread_create (3),