OSDN Git Service

(split) LDP: Update original to LDP v3.38.
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_attr_setguardsize.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH PTHREAD_ATTR_SETGUARDSIZE 3 2008-10-24 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_attr_setguardsize, pthread_attr_getguardsize \- set/get guard size
27 attribute in thread attributes object
28 .SH SYNOPSIS
29 .nf
30 .B #include <pthread.h>
31
32 .BI "int pthread_attr_setguardsize(pthread_attr_t *" attr \
33 ", size_t " guardsize );
34 .BI "int pthread_attr_getguardsize(pthread_attr_t *" attr \
35 ", size_t *" guardsize );
36 .sp
37 Compile and link with \fI\-pthread\fP.
38 .fi
39 .SH DESCRIPTION
40 The
41 .BR pthread_attr_setguardsize ()
42 function sets the guard size attribute of the
43 thread attributes object referred to by
44 .I attr
45 to the value specified in
46 .IR guardsize .
47
48 If
49 .I guardsize
50 is greater than 0,
51 then for each new thread created using
52 .I attr
53 the system allocates an additional region of at least
54 .I guardsize
55 bytes at the end of the thread's stack to act as the guard area
56 for the stack (but see BUGS).
57
58 If
59 .I guardsize
60 is 0, then new threads created with
61 .I attr
62 will not have a guard area.
63
64 The default guard size is the same as the system page size.
65
66 If the stack address attribute has been set in
67 .I attr
68 (using
69 .BR pthread_attr_setstack (3)
70 or
71 .BR pthread_attr_setstackaddr (3)),
72 meaning that the caller is allocating the thread's stack,
73 then the guard size attribute is ignored
74 (i.e., no guard area is created by the system):
75 it is the application's responsibility to handle stack overflow
76 (perhaps by using
77 .BR mprotect (2)
78 to manually define a guard area at the end of the stack
79 that it has allocated).
80
81 The
82 .BR pthread_attr_getguardsize ()
83 function returns the guard size attribute of the
84 thread attributes object referred to by
85 .I attr
86 in the buffer pointed to by
87 .IR guardsize .
88 .SH RETURN VALUE
89 On success, these functions return 0;
90 on error, they return a nonzero error number.
91 .SH ERRORS
92 POSIX.1-2001 documents an
93 .B EINVAL
94 error if
95 .I attr
96 or
97 .I guardsize
98 is invalid.
99 On Linux these functions always succeed
100 (but portable and future-proof applications should nevertheless
101 handle a possible error return).
102 .SH VERSIONS
103 These functions are provided by glibc since version 2.1.
104 .SH CONFORMING TO
105 POSIX.1-2001.
106 .SH NOTES
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
111 .B SIGSEGV
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.
116 (Nevertheless,
117 .BR pthread_attr_getguardsize ()
118 returns the guard size that was set by
119 .BR pthread_attr_setguardsize ().)
120
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.
124
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.
128 .SH BUGS
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,
132 as POSIX.1 requires.
133 (This can result in an
134 .B EINVAL
135 error from
136 .BR pthread_create (3)
137 if the guard size value is too large,
138 leaving no space for the actual stack.)
139
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.
144 .\"
145 .\" Filed bug, 22 Oct 2008:
146 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=6973
147 .\"
148 .\" Older reports:
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
153 .SH EXAMPLE
154 See
155 .BR pthread_getattr_np (3).
156 .SH SEE ALSO
157 .BR mmap (2),
158 .BR mprotect (2),
159 .BR pthread_attr_init (3),
160 .BR pthread_attr_setstack (3),
161 .BR pthread_attr_setstacksize (3),
162 .BR pthread_create (3),
163 .BR pthreads (7)