limit the configurable default stack/guard size for threads
authorRich Felker <dalias@aerifal.cx>
Wed, 19 Sep 2018 03:06:50 +0000 (23:06 -0400)
committerRich Felker <dalias@aerifal.cx>
Wed, 19 Sep 2018 03:06:50 +0000 (23:06 -0400)
limit to 8MB/1MB, repectively. since the defaults cannot be reduced
once increased, excessively large settings would lead to an
unrecoverably broken state. this change is in preparation to allow
defaults to be increased via program headers at the linker level.

creation of threads that really need larger sizes needs to be done
with an explicit attribute.

src/internal/pthread_impl.h
src/thread/default_attr.c
src/thread/pthread_setattr_default_np.c

index 26e6e1d..e73a251 100644 (file)
@@ -182,12 +182,15 @@ hidden void __acquire_ptc(void);
 hidden void __release_ptc(void);
 hidden void __inhibit_ptc(void);
 
-extern hidden size_t __default_stacksize;
-extern hidden size_t __default_guardsize;
+extern hidden unsigned __default_stacksize;
+extern hidden unsigned __default_guardsize;
 
 #define DEFAULT_STACK_SIZE 81920
 #define DEFAULT_GUARD_SIZE 4096
 
+#define DEFAULT_STACK_MAX (8<<20)
+#define DEFAULT_GUARD_MAX (1<<20)
+
 #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1)
 
 #endif
index 46fe98e..dce9640 100644 (file)
@@ -1,4 +1,4 @@
 #include "pthread_impl.h"
 
-size_t __default_stacksize = DEFAULT_STACK_SIZE;
-size_t __default_guardsize = DEFAULT_GUARD_SIZE;
+unsigned __default_stacksize = DEFAULT_STACK_SIZE;
+unsigned __default_guardsize = DEFAULT_GUARD_SIZE;
index 256f068..5848622 100644 (file)
@@ -2,6 +2,9 @@
 #include "pthread_impl.h"
 #include <string.h>
 
+#define MIN(a,b) ((a)<(b) ? (a) : (b))
+#define MAX(a,b) ((a)>(b) ? (a) : (b))
+
 int pthread_setattr_default_np(const pthread_attr_t *attrp)
 {
        /* Reject anything in the attr object other than stack/guard size. */
@@ -11,11 +14,12 @@ int pthread_setattr_default_np(const pthread_attr_t *attrp)
        if (memcmp(&tmp, &zero, sizeof tmp))
                return EINVAL;
 
+       unsigned stack = MIN(attrp->_a_stacksize, DEFAULT_STACK_MAX);
+       unsigned guard = MIN(attrp->_a_guardsize, DEFAULT_GUARD_MAX);
+
        __inhibit_ptc();
-       if (attrp->_a_stacksize >= __default_stacksize)
-               __default_stacksize = attrp->_a_stacksize;
-       if (attrp->_a_guardsize >= __default_guardsize)
-               __default_guardsize = attrp->_a_guardsize;
+       __default_stacksize = MAX(__default_stacksize, stack);
+       __default_guardsize = MAX(__default_guardsize, guard);
        __release_ptc();
 
        return 0;