fa3bfd
commit 630f4cc3aa019ede55976ea561f1a7af2f068639
fa3bfd
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
fa3bfd
Date:   Wed Dec 6 13:05:50 2017 +0000
fa3bfd
fa3bfd
    [BZ #22637] Fix stack guard size accounting
fa3bfd
    
fa3bfd
    Previously if user requested S stack and G guard when creating a
fa3bfd
    thread, the total mapping was S and the actual available stack was
fa3bfd
    S - G - static_tls, which is not what the user requested.
fa3bfd
    
fa3bfd
    This patch fixes the guard size accounting by pretending the user
fa3bfd
    requested S+G stack.  This way all later logic works out except
fa3bfd
    when reporting the user requested stack size (pthread_getattr_np)
fa3bfd
    or when computing the minimal stack size (__pthread_get_minstack).
fa3bfd
    
fa3bfd
    Normally this will increase thread stack allocations by one page.
fa3bfd
    TLS accounting is not affected, that will require a separate fix.
fa3bfd
    
fa3bfd
            [BZ #22637]
fa3bfd
            * nptl/descr.h (stackblock, stackblock_size): Update comments.
fa3bfd
            * nptl/allocatestack.c (allocate_stack): Add guardsize to stacksize.
fa3bfd
            * nptl/nptl-init.c (__pthread_get_minstack): Remove guardsize from
fa3bfd
            stacksize.
fa3bfd
            * nptl/pthread_getattr_np.c (pthread_getattr_np): Likewise.
fa3bfd
fa3bfd
Index: glibc-2.17-c758a686/nptl/allocatestack.c
fa3bfd
===================================================================
fa3bfd
--- glibc-2.17-c758a686.orig/nptl/allocatestack.c
fa3bfd
+++ glibc-2.17-c758a686/nptl/allocatestack.c
fa3bfd
@@ -470,6 +470,10 @@ allocate_stack (const struct pthread_att
fa3bfd
       /* Make sure the size of the stack is enough for the guard and
fa3bfd
 	 eventually the thread descriptor.  */
fa3bfd
       guardsize = (attr->guardsize + pagesize_m1) & ~pagesize_m1;
fa3bfd
+      if (guardsize < attr->guardsize || size + guardsize < guardsize)
fa3bfd
+	/* Arithmetic overflow.  */
fa3bfd
+	return EINVAL;
fa3bfd
+      size += guardsize;
fa3bfd
       if (__builtin_expect (size < ((guardsize + __static_tls_size
fa3bfd
 				     + MINIMAL_REST_STACK + pagesize_m1)
fa3bfd
 				    & ~pagesize_m1),
fa3bfd
Index: glibc-2.17-c758a686/nptl/descr.h
fa3bfd
===================================================================
fa3bfd
--- glibc-2.17-c758a686.orig/nptl/descr.h
fa3bfd
+++ glibc-2.17-c758a686/nptl/descr.h
fa3bfd
@@ -366,9 +366,9 @@ struct pthread
fa3bfd
   struct _Unwind_Exception exc;
fa3bfd
 #endif
fa3bfd
 
fa3bfd
-  /* If nonzero pointer to area allocated for the stack and its
fa3bfd
-     size.  */
fa3bfd
+  /* If nonzero, pointer to the area allocated for the stack and guard. */
fa3bfd
   void *stackblock;
fa3bfd
+  /* Size of the stackblock area including the guard.  */
fa3bfd
   size_t stackblock_size;
fa3bfd
   /* Size of the included guard area.  */
fa3bfd
   size_t guardsize;
fa3bfd
Index: glibc-2.17-c758a686/nptl/nptl-init.c
fa3bfd
===================================================================
fa3bfd
--- glibc-2.17-c758a686.orig/nptl/nptl-init.c
fa3bfd
+++ glibc-2.17-c758a686/nptl/nptl-init.c
fa3bfd
@@ -500,8 +500,5 @@ strong_alias (__pthread_initialize_minim
fa3bfd
 size_t
fa3bfd
 __pthread_get_minstack (const pthread_attr_t *attr)
fa3bfd
 {
fa3bfd
-  struct pthread_attr *iattr = (struct pthread_attr *) attr;
fa3bfd
-
fa3bfd
-  return (GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN
fa3bfd
-	  + iattr->guardsize);
fa3bfd
+  return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
fa3bfd
 }
fa3bfd
Index: glibc-2.17-c758a686/nptl/pthread_getattr_np.c
fa3bfd
===================================================================
fa3bfd
--- glibc-2.17-c758a686.orig/nptl/pthread_getattr_np.c
fa3bfd
+++ glibc-2.17-c758a686/nptl/pthread_getattr_np.c
fa3bfd
@@ -59,8 +59,11 @@ pthread_getattr_np (thread_id, attr)
fa3bfd
   /* The sizes are subject to alignment.  */
fa3bfd
   if (__builtin_expect (thread->stackblock != NULL, 1))
fa3bfd
     {
fa3bfd
-      iattr->stacksize = thread->stackblock_size;
fa3bfd
-      iattr->stackaddr = (char *) thread->stackblock + iattr->stacksize;
fa3bfd
+      /* The stack size reported to the user should not include the
fa3bfd
+	 guard size.  */
fa3bfd
+      iattr->stacksize = thread->stackblock_size - thread->guardsize;
fa3bfd
+      iattr->stackaddr = (char *) thread->stackblock
fa3bfd
+       			 + thread->stackblock_size;
fa3bfd
     }
fa3bfd
   else
fa3bfd
     {