[newlib-cygwin] Align hard stack allocation to pthread stack setup changes

Corinna Vinschen corinna@sourceware.org
Sun Jul 5 15:46:00 GMT 2015


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=14e3fd1b221f59f7f8bc10f328c63e45d34019f8

commit 14e3fd1b221f59f7f8bc10f328c63e45d34019f8
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Sun Jul 5 17:45:06 2015 +0200

    Align hard stack allocation to pthread stack setup changes
    
    	* dcrt0.cc (CYGWIN_GUARD): Remove.
    	(child_info_fork::alloc_stack_hard_way): Align stack commit to changes
    	of thread stack setup in CygwinCreateThread.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/ChangeLog |  6 ++++++
 winsup/cygwin/dcrt0.cc  | 28 +++++++++++++++++-----------
 2 files changed, 23 insertions(+), 11 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 82a8a58..58b75e2 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,5 +1,11 @@
 2015-07-05  Corinna Vinschen  <corinna@vinschen.de>
 
+	* dcrt0.cc (CYGWIN_GUARD): Remove.
+	(child_info_fork::alloc_stack_hard_way): Align stack commit to changes
+	of thread stack setup in CygwinCreateThread.
+
+2015-07-05  Corinna Vinschen  <corinna@vinschen.de>
+
 	* miscfuncs.cc (struct pthread_wrapper_arg): Add member guardsize.
 	(pthread_wrapper): Set thread stack guarantee according to guardsize.
 	Tweak assembler code so that $rax/$eax is not required by GCC to
diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc
index 738860d..48301ea 100644
--- a/winsup/cygwin/dcrt0.cc
+++ b/winsup/cygwin/dcrt0.cc
@@ -16,6 +16,7 @@ details. */
 #include "glob.h"
 #include <ctype.h>
 #include <locale.h>
+#include <sys/param.h>
 #include "environ.h"
 #include "sigproc.h"
 #include "pinfo.h"
@@ -404,8 +405,6 @@ check_sanity_and_sync (per_process *p)
 
 child_info NO_COPY *child_proc_info;
 
-#define CYGWIN_GUARD (PAGE_READWRITE | PAGE_GUARD)
-
 void
 child_info_fork::alloc_stack_hard_way (volatile char *b)
 {
@@ -421,7 +420,7 @@ child_info_fork::alloc_stack_hard_way (volatile char *b)
       || is_mmapped_region ((caddr_t) stacktop, (caddr_t) stackbottom))
     return;
   /* First, try to reserve the entire stack. */
-  stacksize = (SIZE_T) stackbottom - (SIZE_T) stackaddr;
+  stacksize = (PBYTE) stackbottom - (PBYTE) stackaddr;
   if (!VirtualAlloc (stackaddr, stacksize, MEM_RESERVE, PAGE_NOACCESS))
     {
       PTEB teb = NtCurrentTeb ();
@@ -429,25 +428,32 @@ child_info_fork::alloc_stack_hard_way (volatile char *b)
 		 "%p - %p, (child has %p - %p), %E",
 		 stackaddr, stackbottom, teb->DeallocationStack, _tlsbase);
     }
-  stacksize = (SIZE_T) stackbottom - (SIZE_T) stacktop;
+  stacksize = (PBYTE) stackbottom - (PBYTE) stacktop;
   stack_ptr = VirtualAlloc (stacktop, stacksize, MEM_COMMIT, PAGE_READWRITE);
   if (!stack_ptr)
     abort ("can't commit memory for stack %p(%ly), %E", stacktop, stacksize);
   if (guardsize != (size_t) -1)
     {
+      ULONG real_guardsize = guardsize
+			     ? roundup2 (guardsize, wincap.page_size ())
+			     : wincap.def_guard_page_size ();
       /* Allocate PAGE_GUARD page if it still fits. */
       if (stack_ptr > stackaddr)
 	{
-	  stack_ptr = (void *) ((LPBYTE) stack_ptr
-					- wincap.page_size ());
-	  if (!VirtualAlloc (stack_ptr, wincap.page_size (), MEM_COMMIT,
-			     CYGWIN_GUARD))
+	  stack_ptr = (void *) ((PBYTE) stack_ptr - real_guardsize);
+	  if (!VirtualAlloc (stack_ptr, real_guardsize, MEM_COMMIT,
+			     PAGE_READWRITE | PAGE_GUARD))
 	    api_fatal ("fork: couldn't allocate new stack guard page %p, %E",
 		       stack_ptr);
 	}
-      /* Allocate POSIX guard pages. */
-      if (guardsize > 0)
-	VirtualAlloc (stackaddr, guardsize, MEM_COMMIT, PAGE_NOACCESS);
+      /* On post-XP systems, set thread stack guarantee matching the guardsize.
+	 Note that the guardsize is one page bigger than the guarantee. */
+      if (wincap.has_set_thread_stack_guarantee ()
+	  && real_guardsize > wincap.def_guard_page_size ())
+	{
+	  real_guardsize -= wincap.page_size ();
+	  SetThreadStackGuarantee (&real_guardsize);
+	}
     }
   b[0] = '\0';
 }



More information about the Cygwin-cvs mailing list