xref: /netbsd-src/lib/libpthread/pthread_attr_getstack.3 (revision 57b4a3d756e4d57df484941a05e92a293c1930fd)
1*57b4a3d7Sriastradh.\"	$NetBSD: pthread_attr_getstack.3,v 1.9 2023/12/07 16:55:01 riastradh Exp $
22bf45345Sjruoho.\"
32bf45345Sjruoho.\" Copyright (c) 2010 Jukka Ruohonen <jruohonen@iki.fi>
42bf45345Sjruoho.\" All rights reserved.
52bf45345Sjruoho.\"
62bf45345Sjruoho.\" Redistribution and use in source and binary forms, with or without
72bf45345Sjruoho.\" modification, are permitted provided that the following conditions
82bf45345Sjruoho.\" are met:
92bf45345Sjruoho.\"
102bf45345Sjruoho.\" 1. Redistributions of source code must retain the above copyright
112bf45345Sjruoho.\"    notice, this list of conditions and the following disclaimer.
122bf45345Sjruoho.\" 2. Redistributions in binary form must reproduce the above copyright
132bf45345Sjruoho.\"    notice, this list of conditions and the following disclaimer in the
142bf45345Sjruoho.\"    documentation and/or other materials provided with the distribution.
152bf45345Sjruoho.\"
162bf45345Sjruoho.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172bf45345Sjruoho.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182bf45345Sjruoho.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192bf45345Sjruoho.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202bf45345Sjruoho.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212bf45345Sjruoho.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222bf45345Sjruoho.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232bf45345Sjruoho.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242bf45345Sjruoho.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252bf45345Sjruoho.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262bf45345Sjruoho.\" POSSIBILITY OF SUCH DAMAGE.
272bf45345Sjruoho.\"
289c4ae7f5Sjruoho.Dd July 9, 2010
292bf45345Sjruoho.Dt PTHREAD_ATTR_GETSTACK 3
302bf45345Sjruoho.Os
312bf45345Sjruoho.Sh NAME
323e5ec8a3Sabhinav.Nm pthread_attr_getstack ,
333e5ec8a3Sabhinav.Nm pthread_attr_setstack ,
343e5ec8a3Sabhinav.Nm pthread_attr_getstacksize ,
353e5ec8a3Sabhinav.Nm pthread_attr_setstacksize ,
363e5ec8a3Sabhinav.Nm pthread_attr_getstackaddr ,
373e5ec8a3Sabhinav.Nm pthread_attr_setstackaddr
382bf45345Sjruoho.Nd get and set thread stack attributes
392bf45345Sjruoho.Sh LIBRARY
402bf45345Sjruoho.Lb libpthread
412bf45345Sjruoho.Sh SYNOPSIS
422bf45345Sjruoho.In pthread.h
432bf45345Sjruoho.Ft int
442bf45345Sjruoho.Fn pthread_attr_getstack \
452bf45345Sjruoho"const pthread_attr_t * restrict attr" \
46b9737c74Swiz"void ** restrict stackaddr" "size_t * restrict stacksize"
472bf45345Sjruoho.Ft int
482bf45345Sjruoho.Fn pthread_attr_setstack \
49b9737c74Swiz"pthread_attr_t * restrict attr" "void *stackaddr" "size_t stacksize"
502bf45345Sjruoho.Ft int
512bf45345Sjruoho.Fn pthread_attr_getstacksize \
522bf45345Sjruoho"const pthread_attr_t * restrict attr" "size_t * restrict stacksize"
532bf45345Sjruoho.Ft int
542bf45345Sjruoho.Fn pthread_attr_setstacksize \
552bf45345Sjruoho"pthread_attr_t *attr" "size_t stacksize"
562bf45345Sjruoho.Ft int
572bf45345Sjruoho.Fn pthread_attr_getstackaddr \
582bf45345Sjruoho"const pthread_attr_t * restrict attr" "void ** restrict stackaddr"
592bf45345Sjruoho.Ft int
602bf45345Sjruoho.Fn pthread_attr_setstackaddr \
612bf45345Sjruoho"pthread_attr_t *attr" "void *stackaddr"
622bf45345Sjruoho.Sh DESCRIPTION
632bf45345SjruohoThe
642bf45345Sjruoho.Fn pthread_attr_getstack
652bf45345Sjruohoand
662bf45345Sjruoho.Fn pthread_attr_setstack
672bf45345Sjruohofunctions get and set, respectively, the thread stack attributes
682bf45345Sjruoho.Fa stackaddr
692bf45345Sjruohoand
702bf45345Sjruoho.Fa stacksize
712bf45345Sjruohoin the
722bf45345Sjruoho.Fa attr
732bf45345Sjruohoobject.
742bf45345SjruohoThe remaining four functions behave similarly,
752bf45345Sjruohobut instead of getting or setting both
762bf45345Sjruoho.Fa stackaddr
772bf45345Sjruohoand
782bf45345Sjruoho.Fa stacksize ,
792bf45345Sjruohothese get and set the values individually.
802bf45345Sjruoho.Pp
812bf45345SjruohoThe
822bf45345Sjruoho.Fa stacksize
832bf45345Sjruohoparameter is defined to be the minimum stack size (in bytes)
842bf45345Sjruohoallocated for the thread's stack during the creation of the thread.
852bf45345SjruohoThe
862bf45345Sjruoho.Fa stackaddr
872bf45345Sjruohoattribute specifies the location of storage to be used for the thread's stack.
882bf45345SjruohoAll pages within the stack described by
892bf45345Sjruoho.Fa stackaddr
902bf45345Sjruohoand
912bf45345Sjruoho.Fa stacksize
9240267ee9Sjruohoshould be both readable and writable by the thread.
932bf45345Sjruoho.Pp
942bf45345SjruohoThe behavior is undefined in all functions if the
952bf45345Sjruoho.Fa attr
962bf45345Sjruohoparameter does not refer to an attribute object initialized by using
972bf45345Sjruoho.Xr pthread_attr_init 3
982bf45345Sjruohoprior to the call.
992bf45345SjruohoIn addition, undefined behavior may follow if the
1002bf45345Sjruoho.Fn pthread_attr_getstack
1012bf45345Sjruohofunction is called before the
1022bf45345Sjruoho.Fa stackaddr
1032bf45345Sjruohoattribute has been set.
1044ec5f028Sjruoho.Ss Rationale
1052bf45345SjruohoThe rationale behind these functions is to address cases where an application
1062bf45345Sjruohomay be used in an environment where the stack of a thread must be placed to
1072bf45345Sjruohosome particular region of memory.
1082bf45345SjruohoFor the majority of applications, this is seldom necessary,
1092bf45345Sjruohoand the use of these functions should be generally avoided.
1102bf45345SjruohoAt least few potential caveats can be mentioned.
1112bf45345Sjruoho.Bl -bullet -offset 2n
1122bf45345Sjruoho.It
113661e3b51SwizThere is a certain degree of ambiguity in the POSIX
1142bf45345Sjruohostandard with respect to thread stack.
1152bf45345Sjruoho.It
1162bf45345SjruohoThe exact behavior of the functions may vary
1172bf45345Sjruohoboth across machines and operating systems.
1182bf45345SjruohoIn particular, the address specified by
1192bf45345Sjruoho.Fa stackaddr
1202bf45345Sjruohoshould be suitably aligned.
1212bf45345SjruohoThe system page size, as specified by
1222bf45345Sjruoho.Xr sysconf 3 ,
1232bf45345Sjruohoand the use of
1242bf45345Sjruoho.Xr posix_memalign 3
1252bf45345Sjruohomay guarantee some degree of portability.
126e328c029SjruohoAlso
127e328c029Sjruoho.Xr mmap 2
128e328c029Sjruohoprovides means for alignment.
1292bf45345Sjruoho.It
1302bf45345SjruohoIf the application modifies the stack address, it claims also
1312bf45345Sjruohothe responsibility of allocating the stack area and guarding it against
1322bf45345Sjruohopossible stack overflow.
13340267ee9SjruohoNo default guard area will be allocated (see
13440267ee9Sjruoho.Xr pthread_attr_getguardsize 3 ) .
13540267ee9SjruohoIt may be necessary to manually use
13640267ee9Sjruoho.Xr mprotect 2
13740267ee9Sjruohoin order to define a guard area at the end of the allocated stack.
1382bf45345Sjruoho.It
13940267ee9SjruohoMoreover, if
1402bf45345Sjruoho.Fa attr
1412bf45345Sjruohois used to create multiple threads, the stack address must be changed
1422bf45345Sjruohoby the application between successive calls to
1432bf45345Sjruoho.Xr pthread_create 3 .
1442bf45345Sjruoho.El
1452bf45345Sjruoho.Sh RETURN VALUES
1462bf45345SjruohoIf successful, these functions return 0.
1472bf45345SjruohoOtherwise, an error number is returned to indicate the error.
1482bf45345Sjruoho.Sh ERRORS
1492bf45345SjruohoNo errors are defined for the three functions that obtain the stack values.
1502bf45345SjruohoThe three functions that set the stack values may fail if:
1512bf45345Sjruoho.Bl -tag -width Er
1522bf45345Sjruoho.It Bq Er ENOMEM
1532bf45345SjruohoThere was insufficient memory to complete the operation.
1542bf45345Sjruoho.El
1552bf45345Sjruoho.Pp
1562bf45345SjruohoThe
1572bf45345Sjruoho.Fn pthread_attr_setstacksize
1582bf45345Sjruohofunction may additionally fail if:
1592bf45345Sjruoho.Bl -tag -width Er
1602bf45345Sjruoho.It Bq Er EINVAL
1612bf45345SjruohoThe specified
1622bf45345Sjruoho.Fa stacksize
1632bf45345Sjruohois less than
1642bf45345Sjruoho.Dv PTHREAD_STACK_MIN
1652bf45345Sjruohoor exceeds some system-imposed limit.
1662bf45345Sjruoho.El
1672bf45345Sjruoho.Sh SEE ALSO
16840267ee9Sjruoho.Xr pthread_attr 3 ,
16940267ee9Sjruoho.Xr pthread_attr_setguardsize 3
1702bf45345Sjruoho.Sh STANDARDS
1719c4ae7f5SjruohoAll described functions conform to
1729c4ae7f5Sjruoho.St -p1003.1-2001 .
1739c4ae7f5SjruohoNote that
1742bf45345Sjruoho.Fn pthread_attr_getstackaddr
1752bf45345Sjruohoand
1762bf45345Sjruoho.Fn pthread_attr_setstackaddr
1779c4ae7f5Sjruohowere however removed from the specification in the
1789c4ae7f5Sjruoho.St -p1003.1-2008
1799c4ae7f5Sjruohorevision.
180*57b4a3d7Sriastradh.Sh BUGS
181*57b4a3d7SriastradhOlder versions of
182*57b4a3d7Sriastradh.Nx ,
183*57b4a3d7Sriastradhprior to 10.0, 9.4, and 8.3, incorrectly adjust the stack address by
184*57b4a3d7Sriastradhthe guard size in threads configured with
185*57b4a3d7Sriastradh.Fn pthread_attr_setstack ,
186*57b4a3d7Sriastradhinstead of ignoring the guard size in that case as
187*57b4a3d7Sriastradh.Tn POSIX
188*57b4a3d7Sriastradhprescribes
189*57b4a3d7Sriastradh.Po
190*57b4a3d7Sriastradhsee
191*57b4a3d7Sriastradh.Lk https://gnats.NetBSD.org/57721 "PR lib/57721"
192*57b4a3d7Sriastradh.Pc .
193*57b4a3d7Sriastradh.Pp
194*57b4a3d7SriastradhEven if you didn't set a nonzero guard size with
195*57b4a3d7Sriastradh.Xr pthread_attr_setguardsize 3 ,
196*57b4a3d7Sriastradhthe system will choose a nonzero default guard size.
197*57b4a3d7Sriastradh.Pp
198*57b4a3d7SriastradhTo work around this in applications that run on older and newer
199*57b4a3d7Sriastradhversions of
200*57b4a3d7Sriastradh.Nx ,
201*57b4a3d7Sriastradhas well as on other operating systems, you can safely set the guard
202*57b4a3d7Sriastradhsize to zero:
203*57b4a3d7Sriastradh.Dl "pthread_attr_setguardsize(&attr, 0);"
204