diff options
-rw-r--r-- | lib/libpthread/include/pthread.h | 4 | ||||
-rw-r--r-- | lib/libpthread/man/Makefile.inc | 4 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_attr_init.3 | 5 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_attr_setdetachstate.3 | 5 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_attr_setguardsize.3 | 89 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_attr_setstack.3 | 5 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_attr_setstackaddr.3 | 5 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_attr_setstacksize.3 | 5 | ||||
-rw-r--r-- | lib/libpthread/man/pthread_create.3 | 5 | ||||
-rw-r--r-- | lib/libpthread/man/pthreads.3 | 9 | ||||
-rw-r--r-- | lib/libpthread/shlib_version | 2 | ||||
-rw-r--r-- | lib/libpthread/uthread/Makefile.inc | 4 | ||||
-rw-r--r-- | lib/libpthread/uthread/pthread_private.h | 7 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_attr_getguardsize.c | 55 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_attr_setguardsize.c | 55 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_create.c | 4 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_init.c | 5 | ||||
-rw-r--r-- | lib/libpthread/uthread/uthread_stack.c | 40 |
18 files changed, 269 insertions, 39 deletions
diff --git a/lib/libpthread/include/pthread.h b/lib/libpthread/include/pthread.h index ca837eb11f1..e572fae8f73 100644 --- a/lib/libpthread/include/pthread.h +++ b/lib/libpthread/include/pthread.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pthread.h,v 1.28 2008/06/25 14:58:54 millert Exp $ */ +/* $OpenBSD: pthread.h,v 1.29 2008/12/18 09:30:32 guenther Exp $ */ /* * Copyright (c) 1993, 1994 by Chris Provenzano, proven@mit.edu @@ -203,11 +203,13 @@ int pthread_attr_getstack(const pthread_attr_t *, void **, size_t *); int pthread_attr_getstacksize(const pthread_attr_t *, size_t *); int pthread_attr_getstackaddr(const pthread_attr_t *, void **); +int pthread_attr_getguardsize(const pthread_attr_t *, size_t *); int pthread_attr_getdetachstate(const pthread_attr_t *, int *); int pthread_attr_init(pthread_attr_t *); int pthread_attr_setstacksize(pthread_attr_t *, size_t); int pthread_attr_setstack(pthread_attr_t *, void *, size_t); int pthread_attr_setstackaddr(pthread_attr_t *, void *); +int pthread_attr_setguardsize(pthread_attr_t *, size_t); int pthread_attr_setdetachstate(pthread_attr_t *, int); void pthread_cleanup_pop(int); void pthread_cleanup_push(void (*) (void *), void *routine_arg); diff --git a/lib/libpthread/man/Makefile.inc b/lib/libpthread/man/Makefile.inc index bfa2dbda449..327457260ac 100644 --- a/lib/libpthread/man/Makefile.inc +++ b/lib/libpthread/man/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.22 2008/07/05 08:03:57 guenther Exp $ +# $OpenBSD: Makefile.inc,v 1.23 2008/12/18 09:30:32 guenther Exp $ # $FreeBSD: Makefile.inc,v 1.6 1999/08/28 00:03:02 peter Exp $ # POSIX thread man files @@ -11,6 +11,7 @@ MAN+= \ pthread_attr_setstack.3 \ pthread_attr_setstackaddr.3 \ pthread_attr_setstacksize.3 \ + pthread_attr_setguardsize.3 \ pthread_attr_setdetachstate.3 \ pthread_cleanup_pop.3 \ pthread_cleanup_push.3 \ @@ -85,6 +86,7 @@ MLINKS+=flockfile.3 funlockfile.3 \ pthread_attr_setstack.3 pthread_attr_getstack.3 \ pthread_attr_setstackaddr.3 pthread_attr_getstackaddr.3 \ pthread_attr_setstacksize.3 pthread_attr_getstacksize.3 \ + pthread_attr_setguardsize.3 pthread_attr_getguardsize.3 \ pthread_attr_setdetachstate.3 pthread_attr_getdetachstate.3 \ pthread_mutexattr.3 pthread_mutexattr_init.3 \ pthread_mutexattr.3 pthread_mutexattr_destroy.3 \ diff --git a/lib/libpthread/man/pthread_attr_init.3 b/lib/libpthread/man/pthread_attr_init.3 index 10351aabf1f..7dff56d4b1e 100644 --- a/lib/libpthread/man/pthread_attr_init.3 +++ b/lib/libpthread/man/pthread_attr_init.3 @@ -1,9 +1,9 @@ -.\" $OpenBSD: pthread_attr_init.3,v 1.9 2008/07/05 10:43:07 jmc Exp $ +.\" $OpenBSD: pthread_attr_init.3,v 1.10 2008/12/18 09:30:32 guenther Exp $ .\" Manual page derived from TOG's UNIX98 documentation. .\" .\" David Leonard, 2000. Public Domain. .\" -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREAD_ATTR_INIT 3 .Os .Sh NAME @@ -65,6 +65,7 @@ These functions will not return an error code of .Bq Er EINTR . .Sh SEE ALSO .Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstack 3 , .Xr pthread_attr_setstackaddr 3 , .Xr pthread_attr_setstacksize 3 , diff --git a/lib/libpthread/man/pthread_attr_setdetachstate.3 b/lib/libpthread/man/pthread_attr_setdetachstate.3 index 6fe3a88229d..fa27a96e7c5 100644 --- a/lib/libpthread/man/pthread_attr_setdetachstate.3 +++ b/lib/libpthread/man/pthread_attr_setdetachstate.3 @@ -1,9 +1,9 @@ -.\" $OpenBSD: pthread_attr_setdetachstate.3,v 1.8 2008/07/05 08:03:57 guenther Exp $ +.\" $OpenBSD: pthread_attr_setdetachstate.3,v 1.9 2008/12/18 09:30:32 guenther Exp $ .\" Manual page derived from TOG's UNIX98 documentation. .\" .\" David Leonard, 2000. Public Domain. .\" -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREAD_ATTR_SETDETACHSTATE 3 .Os .Sh NAME @@ -90,6 +90,7 @@ These functions will not return an error code of .Bq Er EINTR . .Sh SEE ALSO .Xr pthread_attr_init 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstack 3 , .Xr pthread_attr_setstackaddr 3 , .Xr pthread_attr_setstacksize 3 , diff --git a/lib/libpthread/man/pthread_attr_setguardsize.3 b/lib/libpthread/man/pthread_attr_setguardsize.3 new file mode 100644 index 00000000000..471def58d61 --- /dev/null +++ b/lib/libpthread/man/pthread_attr_setguardsize.3 @@ -0,0 +1,89 @@ +.\" $OpenBSD: pthread_attr_setguardsize.3,v 1.1 2008/12/18 09:30:32 guenther Exp $ +.\" Manual page derived from TOG's XPG6 documentation. +.\" +.\" David Leonard, 2000. Public Domain. +.\" +.Dd $Mdocdate: December 18 2008 $ +.Dt PTHREAD_ATTR_SETGUARDSIZE 3 +.Os +.Sh NAME +.Nm pthread_attr_setguardsize , +.Nm pthread_attr_getguardsize +.Nd set and get guardsize attribute +.Sh SYNOPSIS +.Fd #include <pthread.h> +.Ft int +.Fn pthread_attr_setguardsize "pthread_attr_t *attr" "size_t guardsize" +.Ft int +.Fn pthread_attr_getguardsize "const pthread_attr_t *attr" "size_t *guardsize" +.Sh DESCRIPTION +The functions +.Fn pthread_attr_setguardsize +and +.Fn pthread_attr_getguardsize , +respectively, set and get the thread +creation +.Va guardsize +attribute in the +.Fa attr +object. +If +.Va guardsize +is zero, +a guard area shall not be provided for threads created with +.Fa attr . +If +.Va guardsize +is greater than zero, +a guard area of at least size +.Va guardsize +bytes shall be provided for each thread created with +.Fa attr . +.Pp +The +.Va guardsize +attribute controls the size of the guard area for the created +thread's stack. +The +.Va guardsize +attribute provides protection against overflow of the stack pointer. +If a thread's stack is created with guard protection, +the implementation allocates extra memory at the overflow end of +the stack as a buffer against stack overflow of the stack pointer. +If an application overflows into this buffer an error shall result +(possibly in a SIGSEGV signal being delivered to the thread). +.Sh RETURN VALUES +Upon successful completion, +.Fn pthread_attr_setguardsize +and +.Fn pthread_attr_getguardsize +return a value of 0. +Otherwise, an error number is returned to indicate the error. +.Pp +The +.Fn pthread_attr_getguardsize +function stores the +.Va guardsize +attribute value in +.Fa guardsize +if successful. +.Sh ERRORS +No errors are defined. +.Pp +These functions will not return an error code of +.Bq Er EINTR . +.Sh SEE ALSO +.Xr pthread_attr_init 3 , +.Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setstack 3 , +.Xr pthread_attr_setstackaddr 3 , +.Xr pthread_attr_setstacksize 3 , +.Xr pthread_create 3 , +.Xr pthreads 3 +.Sh STANDARDS +.Fn pthread_attr_setguardsize +and +.Fn pthread_attr_getguardsize +conform to ISO/IEC 9945-1 ANSI/IEEE +.Pq Dq Tn POSIX +Std 1003.1, 2004 Edition. diff --git a/lib/libpthread/man/pthread_attr_setstack.3 b/lib/libpthread/man/pthread_attr_setstack.3 index c2cd372c8e7..ed4250eedbe 100644 --- a/lib/libpthread/man/pthread_attr_setstack.3 +++ b/lib/libpthread/man/pthread_attr_setstack.3 @@ -1,9 +1,9 @@ -.\" $OpenBSD: pthread_attr_setstack.3,v 1.2 2008/07/05 10:43:07 jmc Exp $ +.\" $OpenBSD: pthread_attr_setstack.3,v 1.3 2008/12/18 09:30:32 guenther Exp $ .\" Manual page derived from TOG's UNIX98 documentation. .\" .\" David Leonard, 2000. Public Domain. .\" -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREAD_ATTR_SETSTACK 3 .Os .Sh NAME @@ -79,6 +79,7 @@ These functions will not return an error code of .Sh SEE ALSO .Xr pthread_attr_init 3 , .Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstackaddr 3 , .Xr pthread_attr_setstacksize 3 , .Xr pthread_create 3 , diff --git a/lib/libpthread/man/pthread_attr_setstackaddr.3 b/lib/libpthread/man/pthread_attr_setstackaddr.3 index efb3eea54b4..0de282cd15b 100644 --- a/lib/libpthread/man/pthread_attr_setstackaddr.3 +++ b/lib/libpthread/man/pthread_attr_setstackaddr.3 @@ -1,9 +1,9 @@ -.\" $OpenBSD: pthread_attr_setstackaddr.3,v 1.9 2008/07/05 08:03:57 guenther Exp $ +.\" $OpenBSD: pthread_attr_setstackaddr.3,v 1.10 2008/12/18 09:30:32 guenther Exp $ .\" Manual page derived from TOG's UNIX98 documentation. .\" .\" David Leonard, 2000. Public Domain. .\" -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREAD_ATTR_SETSTACKADDR 3 .Os .Sh NAME @@ -57,6 +57,7 @@ These functions will not return an error code of .Sh SEE ALSO .Xr pthread_attr_init 3 , .Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstack 3 , .Xr pthread_attr_setstacksize 3 , .Xr pthread_create 3 , diff --git a/lib/libpthread/man/pthread_attr_setstacksize.3 b/lib/libpthread/man/pthread_attr_setstacksize.3 index b26f31be486..2189e0b1eb1 100644 --- a/lib/libpthread/man/pthread_attr_setstacksize.3 +++ b/lib/libpthread/man/pthread_attr_setstacksize.3 @@ -1,9 +1,9 @@ -.\" $OpenBSD: pthread_attr_setstacksize.3,v 1.8 2008/07/05 08:03:57 guenther Exp $ +.\" $OpenBSD: pthread_attr_setstacksize.3,v 1.9 2008/12/18 09:30:32 guenther Exp $ .\" Manual page derived from TOG's UNIX98 documentation. .\" .\" David Leonard, 2000. Public Domain. .\" -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREAD_ATTR_SETSTACKSIZE 3 .Os .Sh NAME @@ -65,6 +65,7 @@ These functions will not return an error code of .Sh SEE ALSO .Xr pthread_attr_init 3 , .Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstack 3 , .Xr pthread_attr_setstackaddr 3 , .Xr pthread_create 3 , diff --git a/lib/libpthread/man/pthread_create.3 b/lib/libpthread/man/pthread_create.3 index 87833f5c736..8e9be64e3c6 100644 --- a/lib/libpthread/man/pthread_create.3 +++ b/lib/libpthread/man/pthread_create.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: pthread_create.3,v 1.12 2008/07/05 10:43:07 jmc Exp $ +.\" $OpenBSD: pthread_create.3,v 1.13 2008/12/18 09:30:32 guenther Exp $ .\" .\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>. .\" All rights reserved. @@ -32,7 +32,7 @@ .\" .\" $FreeBSD: pthread_create.3,v 1.8 1999/08/28 00:03:04 peter Exp $ .\" -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREAD_CREATE 3 .Os .Sh NAME @@ -111,6 +111,7 @@ is invalid. .Xr fork 2 , .Xr pthread_attr_init 3 , .Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstack 3 , .Xr pthread_attr_setstackaddr 3 , .Xr pthread_attr_setstacksize 3 , diff --git a/lib/libpthread/man/pthreads.3 b/lib/libpthread/man/pthreads.3 index d760eaaa835..462959cb381 100644 --- a/lib/libpthread/man/pthreads.3 +++ b/lib/libpthread/man/pthreads.3 @@ -1,6 +1,6 @@ -.\" $OpenBSD: pthreads.3,v 1.28 2008/07/05 08:03:57 guenther Exp $ +.\" $OpenBSD: pthreads.3,v 1.29 2008/12/18 09:30:32 guenther Exp $ .\" David Leonard <d@openbsd.org>, 1998. Public domain. -.Dd $Mdocdate: July 5 2008 $ +.Dd $Mdocdate: December 18 2008 $ .Dt PTHREADS 3 .Os .Sh NAME @@ -84,6 +84,10 @@ Set stackaddr attribute. Get stacksize attribute. .It Fn pthread_attr_setstacksize Set stacksize attribute. +.It Fn pthread_attr_getguardsize +Get guardsize attribute. +.It Fn pthread_attr_setguardsize +Set guardsize attribute. .El .Ss Cleanup Routines The functions available are as follows: @@ -491,6 +495,7 @@ environment variable. .Xr pthread_atfork 3 , .Xr pthread_attr_init 3 , .Xr pthread_attr_setdetachstate 3 , +.Xr pthread_attr_setguardsize 3 , .Xr pthread_attr_setstack 3 , .Xr pthread_attr_setstackaddr 3 , .Xr pthread_attr_setstacksize 3 , diff --git a/lib/libpthread/shlib_version b/lib/libpthread/shlib_version index f461c533903..ba5b9bf5d82 100644 --- a/lib/libpthread/shlib_version +++ b/lib/libpthread/shlib_version @@ -1,2 +1,2 @@ major=11 -minor=0 +minor=1 diff --git a/lib/libpthread/uthread/Makefile.inc b/lib/libpthread/uthread/Makefile.inc index 3e814dd0500..23d7f229128 100644 --- a/lib/libpthread/uthread/Makefile.inc +++ b/lib/libpthread/uthread/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.20 2007/06/05 18:11:48 kurt Exp $ +# $OpenBSD: Makefile.inc,v 1.21 2008/12/18 09:30:32 guenther Exp $ # $FreeBSD: Makefile.inc,v 1.19 1999/08/28 00:03:19 peter Exp $ # uthread sources @@ -12,6 +12,7 @@ SRCS+= \ uthread_attr_destroy.c \ uthread_attr_init.c \ uthread_attr_getdetachstate.c \ + uthread_attr_getguardsize.c \ uthread_attr_getinheritsched.c \ uthread_attr_getschedparam.c \ uthread_attr_getschedpolicy.c \ @@ -21,6 +22,7 @@ SRCS+= \ uthread_attr_getstacksize.c \ uthread_attr_setcreatesuspend_np.c \ uthread_attr_setdetachstate.c \ + uthread_attr_setguardsize.c \ uthread_attr_setinheritsched.c \ uthread_attr_setschedparam.c \ uthread_attr_setschedpolicy.c \ diff --git a/lib/libpthread/uthread/pthread_private.h b/lib/libpthread/uthread/pthread_private.h index bcd64def492..5ed8fab9c08 100644 --- a/lib/libpthread/uthread/pthread_private.h +++ b/lib/libpthread/uthread/pthread_private.h @@ -1,4 +1,4 @@ -/* $OpenBSD: pthread_private.h,v 1.69 2007/11/20 19:35:36 deraadt Exp $ */ +/* $OpenBSD: pthread_private.h,v 1.70 2008/12/18 09:30:32 guenther Exp $ */ /* * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>. * All rights reserved. @@ -581,6 +581,7 @@ struct stack { SLIST_ENTRY(stack) qe; /* Queue entry for this stack. */ void *base; /* Bottom of useful stack */ size_t size; /* Size of useful stack */ + size_t guardsize; /* Size of red zone */ void *redzone; /* Red zone location */ void *storage; /* allocated storage */ }; @@ -957,7 +958,7 @@ SCLASS struct pthread_attr pthread_attr_default #ifdef GLOBAL_PTHREAD_PRIVATE = { SCHED_RR, 0, TIMESLICE_USEC, PTHREAD_DEFAULT_PRIORITY, PTHREAD_CREATE_RUNNING, PTHREAD_CREATE_JOINABLE, NULL, NULL, NULL, - PTHREAD_STACK_DEFAULT }; + PTHREAD_STACK_DEFAULT, 0 /* set in _thread_init() */ }; #else ; #endif @@ -1184,7 +1185,7 @@ void _thread_leave_cancellation_point(void); void _thread_cancellation_point(void); int _thread_slow_atomic_lock(volatile _spinlock_lock_t *); int _thread_slow_atomic_is_locked(volatile _spinlock_lock_t *); -struct stack * _thread_stack_alloc(void *, size_t); +struct stack * _thread_stack_alloc(void *, size_t, size_t); void _thread_stack_free(struct stack *); diff --git a/lib/libpthread/uthread/uthread_attr_getguardsize.c b/lib/libpthread/uthread/uthread_attr_getguardsize.c new file mode 100644 index 00000000000..b956ed6ecc2 --- /dev/null +++ b/lib/libpthread/uthread/uthread_attr_getguardsize.c @@ -0,0 +1,55 @@ +/* $OpenBSD: uthread_attr_getguardsize.c,v 1.1 2008/12/18 09:30:32 guenther Exp $ */ +/* + * Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by John Birrell. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD: uthread_attr_getstacksize.c,v 1.3 1999/08/28 00:03:22 peter Exp $ + */ +#include <errno.h> +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" + +int +pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize) +{ + int ret; + + /* Check for invalid arguments: */ + if (attr == NULL || *attr == NULL || guardsize == NULL) + ret = EINVAL; + else { + /* Return the guard size: */ + *guardsize = (*attr)->guardsize_attr; + ret = 0; + } + return(ret); +} +#endif diff --git a/lib/libpthread/uthread/uthread_attr_setguardsize.c b/lib/libpthread/uthread/uthread_attr_setguardsize.c new file mode 100644 index 00000000000..db9acad2fd7 --- /dev/null +++ b/lib/libpthread/uthread/uthread_attr_setguardsize.c @@ -0,0 +1,55 @@ +/* $OpenBSD: uthread_attr_setguardsize.c,v 1.1 2008/12/18 09:30:32 guenther Exp $ */ +/* + * Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by John Birrell. + * 4. Neither the name of the author nor the names of any co-contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD: uthread_attr_setstacksize.c,v 1.4 1999/08/28 00:03:26 peter Exp $ + */ +#include <errno.h> +#ifdef _THREAD_SAFE +#include <pthread.h> +#include "pthread_private.h" + +int +pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) +{ + int ret; + + /* Check for invalid arguments: */ + if (attr == NULL || *attr == NULL) + ret = EINVAL; + else { + /* Save the guard size: */ + (*attr)->guardsize_attr = guardsize; + ret = 0; + } + return(ret); +} +#endif diff --git a/lib/libpthread/uthread/uthread_create.c b/lib/libpthread/uthread/uthread_create.c index 3a5f7a626e4..6b5633f9f06 100644 --- a/lib/libpthread/uthread/uthread_create.c +++ b/lib/libpthread/uthread/uthread_create.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_create.c,v 1.22 2008/07/07 04:59:22 guenther Exp $ */ +/* $OpenBSD: uthread_create.c,v 1.23 2008/12/18 09:30:32 guenther Exp $ */ /* * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> * All rights reserved. @@ -87,7 +87,7 @@ pthread_create(pthread_t *thread, const pthread_attr_t *attr, /* Allocate a stack: */ stack = _thread_stack_alloc(pattr->stackaddr_attr, - pattr->stacksize_attr); + pattr->stacksize_attr, pattr->guardsize_attr); if (stack == NULL) { ret = EAGAIN; free(new_thread); diff --git a/lib/libpthread/uthread/uthread_init.c b/lib/libpthread/uthread/uthread_init.c index 19421a04d55..c9b20880ad4 100644 --- a/lib/libpthread/uthread/uthread_init.c +++ b/lib/libpthread/uthread/uthread_init.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_init.c,v 1.42 2008/06/14 15:40:16 fgsch Exp $ */ +/* $OpenBSD: uthread_init.c,v 1.43 2008/12/18 09:30:32 guenther Exp $ */ /* * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au> * All rights reserved. @@ -358,6 +358,9 @@ _thread_init(void) _thread_max_fdtsize = (int)rl.rlim_max; _thread_max_pfdtsize = (nfds_t)rl.rlim_max; + /* Get the default guardsize */ + pthread_attr_default.guardsize_attr = (size_t)sysconf(_SC_PAGESIZE); + /* Allocate memory for the file descriptor table: */ _thread_fd_table = calloc((size_t)_thread_max_fdtsize, sizeof(struct fd_table_entry *)); diff --git a/lib/libpthread/uthread/uthread_stack.c b/lib/libpthread/uthread/uthread_stack.c index bf3e3976444..2212a0a87d2 100644 --- a/lib/libpthread/uthread/uthread_stack.c +++ b/lib/libpthread/uthread/uthread_stack.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uthread_stack.c,v 1.10 2007/05/18 19:28:50 kurt Exp $ */ +/* $OpenBSD: uthread_stack.c,v 1.11 2008/12/18 09:30:32 guenther Exp $ */ /* * Copyright 1999, David Leonard. All rights reserved. * <insert BSD-style license&disclaimer> @@ -25,15 +25,14 @@ #include "pthread_private.h" struct stack * -_thread_stack_alloc(base, size) - void *base; - size_t size; +_thread_stack_alloc(void *base, size_t size, size_t guardsize) { struct stack *stack; size_t nbpg = (size_t)getpagesize(); /* Maintain a stack of default-sized stacks that we can re-use. */ - if (base == NULL && size == PTHREAD_STACK_DEFAULT) { + if (base == NULL && size == PTHREAD_STACK_DEFAULT + && guardsize == pthread_attr_default.guardsize_attr) { if (pthread_mutex_lock(&_gc_mutex) != 0) PANIC("Cannot lock gc mutex"); @@ -56,16 +55,24 @@ _thread_stack_alloc(base, size) /* Use the user's storage */ stack->base = base; stack->size = size; + stack->guardsize = 0; stack->redzone = NULL; stack->storage = NULL; return stack; } - /* Round size up to closest page boundry */ + /* Round sizes up to closest page boundry */ size = ((size + (nbpg - 1)) / nbpg) * nbpg; + guardsize = ((guardsize + (nbpg - 1)) / nbpg) * nbpg; - /* mmap storage for the stack, with one extra page for redzone */ - stack->storage = mmap(NULL, size + nbpg, PROT_READ|PROT_WRITE, + /* overflow? */ + if (SIZE_MAX - size < guardsize) { + free(stack); + return NULL; + } + + /* mmap storage for the stack, possibly with page(s) for redzone */ + stack->storage = mmap(NULL, size + guardsize, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); if (stack->storage == MAP_FAILED) { free(stack); @@ -77,16 +84,20 @@ _thread_stack_alloc(base, size) */ #if defined(MACHINE_STACK_GROWS_UP) /* Red zone is the last page of the storage: */ - stack->redzone = (void *)((caddr_t)stack->storage + (ptrdiff_t)size); + stack->redzone = (caddr_t)stack->storage + (ptrdiff_t)size; stack->base = stack->storage; stack->size = size; + stack->guardsize = guardsize; #else /* Red zone is the first page of the storage: */ stack->redzone = stack->storage; - stack->base = (caddr_t)stack->redzone + (ptrdiff_t)nbpg; + stack->base = (caddr_t)stack->redzone + (ptrdiff_t)guardsize; stack->size = size; + stack->guardsize = guardsize; #endif - if (mprotect(stack->redzone, nbpg, PROT_NONE) == -1) + if (!guardsize) + stack->redzone = NULL; + else if (mprotect(stack->redzone, guardsize, PROT_NONE) == -1) PANIC("Cannot protect stack red zone"); return stack; @@ -96,15 +107,14 @@ void _thread_stack_free(stack) struct stack *stack; { - size_t nbpg = (size_t)getpagesize(); - /* Cache allocated stacks of default size: */ - if (stack->storage != NULL && stack->size == PTHREAD_STACK_DEFAULT) + if (stack->storage != NULL && stack->size == PTHREAD_STACK_DEFAULT + && stack->guardsize == pthread_attr_default.guardsize_attr) SLIST_INSERT_HEAD(&_stackq, stack, qe); else { /* unmap storage: */ if (stack->storage) - munmap(stack->storage, stack->size + nbpg); + munmap(stack->storage, stack->size + stack->guardsize); /* Free stack information storage: */ free(stack); |