OSDN Git Service

13d3a326aa768a7d464e32b4b4818b78fa519e09
[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 .SH DESCRIPTION
39 The
40 .BR pthread_attr_setguardsize ()
41 function sets the guard size attribute of the
42 thread attributes object referred to by
43 .I attr
44 to the value specified in
45 .IR guardsize .
46
47 If
48 .I guardsize
49 is greater than 0,
50 then for each new thread created using
51 .I attr
52 the system allocates an additional region of at least
53 .I guardsize
54 bytes at the end of the thread's stack to act as the guard area
55 for the stack (but see BUGS).
56
57 If
58 .I guardsize
59 is 0, then new threads created with
60 .I attr
61 will not have a guard area.
62
63 The default guard size is the same as the system page size.
64
65 If the stack address attribute has been set in
66 .I attr
67 (using
68 .BR pthread_attr_setstack (3)
69 or
70 .BR pthread_attr_setstackaddr (3)),
71 meaning that the caller is allocating the thread's stack,
72 then the guard size attribute is ignored
73 (i.e., no guard area is created by the system):
74 it is the application's responsibility to handle stack overflow
75 (perhaps by using
76 .BR mprotect (2)
77 to manually define a guard area at the end of the stack
78 that it has allocated).
79
80 The
81 .BR pthread_attr_getguardsize ()
82 function returns the guard size attribute of the
83 thread attributes object referred to by
84 .I attr
85 in the buffer pointed to by
86 .IR guardsize .
87 .SH RETURN VALUE
88 On success, these functions return 0;
89 on error, they return a nonzero error number.
90 .SH ERRORS
91 POSIX.1-2001 documents an
92 .B EINVAL
93 error if
94 .I attr
95 or
96 .I guardsize
97 is invalid.
98 On Linux these functions always succeed
99 (but portable and future-proof applications should nevertheless
100 handle a possible error return).
101 .SH VERSIONS
102 These functions are provided by glibc since version 2.1.
103 .SH CONFORMING TO
104 POSIX.1-2001.
105 .SH NOTES
106 A guard area consists of virtual memory pages that are protected
107 to prevent read and write access.
108 If a thread overflows its stack into the guard area,
109 then, on most hard architectures, it receives a
110 .B SIGSEGV
111 signal, thus notifying it of the overflow.
112 Guard areas start on page boundaries,
113 and the guard size is internally rounded up to
114 the system page size when creating a thread.
115 (Nevertheless,
116 .BR pthread_attr_getguardsize ()
117 returns the guard size that was set by
118 .BR pthread_attr_setguardsize ().)
119
120 Setting a guard size of 0 may be useful to save memory
121 in an application that creates many threads
122 and knows that stack overflow can never occur.
123
124 Choosing a guard size larger than the default size
125 may be necessary for detecting stack overflows
126 if a thread allocates large data structures on the stack.
127 .SH BUGS
128 As at glibc 2.8, the NPTL threading implementation includes
129 the guard area within the stack size allocation,
130 rather than allocating extra space at the end of the stack,
131 as POSIX.1 requires.
132 (This can result in an
133 .B EINVAL
134 error from
135 .BR pthread_create (3)
136 if the guard size value is too large,
137 leaving no space for the actual stack.)
138
139 The obsolete LinuxThreads implementation did the right thing,
140 allocating extra space at the end of the stack for the guard area.
141 .\" glibc includes the guardsize within the allocated stack size,
142 .\" which looks pretty clearly to be in violation of POSIX.
143 .\"
144 .\" Filed bug, 22 Oct 2008:
145 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=6973
146 .\"
147 .\" Older reports:
148 .\" https//bugzilla.redhat.com/show_bug.cgi?id=435337
149 .\" Reportedly, LinuxThreads did the right thing, allocating
150 .\" extra space at the end of the stack:
151 .\" http://sourceware.org/ml/libc-alpha/2008-05/msg00086.html
152 .SH EXAMPLE
153 See
154 .BR pthread_getattr_np (3).
155 .SH SEE ALSO
156 .BR mmap (2),
157 .BR mprotect (2),
158 .BR pthread_attr_init (3),
159 .BR pthread_attr_setstack (3),
160 .BR pthread_attr_setstacksize (3),
161 .BR pthread_create (3),
162 .BR pthreads (7)