1PTHREAD_ATTR_SETGUARDSIZE(3)Linux Programmer's ManuaPlTHREAD_ATTR_SETGUARDSIZE(3)
2
3
4
6 pthread_attr_setguardsize, pthread_attr_getguardsize - set/get guard
7 size attribute in thread attributes object
8
10 #include <pthread.h>
11
12 int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
13 int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,
14 size_t *restrict guardsize);
15
16 Compile and link with -pthread.
17
19 The pthread_attr_setguardsize() function sets the guard size attribute
20 of the thread attributes object referred to by attr to the value speci‐
21 fied in guardsize.
22
23 If guardsize is greater than 0, then for each new thread created using
24 attr the system allocates an additional region of at least guardsize
25 bytes at the end of the thread's stack to act as the guard area for the
26 stack (but see BUGS).
27
28 If guardsize is 0, then new threads created with attr will not have a
29 guard area.
30
31 The default guard size is the same as the system page size.
32
33 If the stack address attribute has been set in attr (using
34 pthread_attr_setstack(3) or pthread_attr_setstackaddr(3)), meaning that
35 the caller is allocating the thread's stack, then the guard size attri‐
36 bute is ignored (i.e., no guard area is created by the system): it is
37 the application's responsibility to handle stack overflow (perhaps by
38 using mprotect(2) to manually define a guard area at the end of the
39 stack that it has allocated).
40
41 The pthread_attr_getguardsize() function returns the guard size attri‐
42 bute of the thread attributes object referred to by attr in the buffer
43 pointed to by guardsize.
44
46 On success, these functions return 0; on error, they return a nonzero
47 error number.
48
50 POSIX.1 documents an EINVAL error if attr or guardsize is invalid. On
51 Linux these functions always succeed (but portable and future-proof ap‐
52 plications should nevertheless handle a possible error return).
53
55 These functions are provided by glibc since version 2.1.
56
58 For an explanation of the terms used in this section, see at‐
59 tributes(7).
60
61 ┌────────────────────────────────────────────┬───────────────┬─────────┐
62 │Interface │ Attribute │ Value │
63 ├────────────────────────────────────────────┼───────────────┼─────────┤
64 │pthread_attr_setguardsize(), │ Thread safety │ MT-Safe │
65 │pthread_attr_getguardsize() │ │ │
66 └────────────────────────────────────────────┴───────────────┴─────────┘
67
69 POSIX.1-2001, POSIX.1-2008.
70
72 A guard area consists of virtual memory pages that are protected to
73 prevent read and write access. If a thread overflows its stack into
74 the guard area, then, on most hard architectures, it receives a SIGSEGV
75 signal, thus notifying it of the overflow. Guard areas start on page
76 boundaries, and the guard size is internally rounded up to the system
77 page size when creating a thread. (Nevertheless, pthread_attr_get‐
78 guardsize() returns the guard size that was set by pthread_attr_set‐
79 guardsize().)
80
81 Setting a guard size of 0 may be useful to save memory in an applica‐
82 tion that creates many threads and knows that stack overflow can never
83 occur.
84
85 Choosing a guard size larger than the default size may be necessary for
86 detecting stack overflows if a thread allocates large data structures
87 on the stack.
88
90 As at glibc 2.8, the NPTL threading implementation includes the guard
91 area within the stack size allocation, rather than allocating extra
92 space at the end of the stack, as POSIX.1 requires. (This can result
93 in an EINVAL error from pthread_create(3) if the guard size value is
94 too large, leaving no space for the actual stack.)
95
96 The obsolete LinuxThreads implementation did the right thing, allocat‐
97 ing extra space at the end of the stack for the guard area.
98
100 See pthread_getattr_np(3).
101
103 mmap(2), mprotect(2), pthread_attr_init(3), pthread_attr_setstack(3),
104 pthread_attr_setstacksize(3), pthread_create(3), pthreads(7)
105
107 This page is part of release 5.12 of the Linux man-pages project. A
108 description of the project, information about reporting bugs, and the
109 latest version of this page, can be found at
110 https://www.kernel.org/doc/man-pages/.
111
112
113
114Linux 2021-03-22 PTHREAD_ATTR_SETGUARDSIZE(3)