[newlib-cygwin] Simplify fork code setting up child stack info

Corinna Vinschen corinna@sourceware.org
Tue Jul 7 15:05:00 GMT 2015


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

commit 606013bcf108716f6a84b574864c26cff74f8a37
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Jul 7 17:05:20 2015 +0200

    Simplify fork code setting up child stack info
    
            * fork.cc (frok::parent): Simplify code propagating stack setup to
            child process.  Tweak comments.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/ChangeLog |  5 +++++
 winsup/cygwin/fork.cc   | 49 ++++++++++++++++++++-----------------------------
 2 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 13d6fe7..141307e 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,8 @@
+2015-07-07  Corinna Vinschen  <corinna@vinschen.de>
+
+	* fork.cc (frok::parent): Simplify code propagating stack setup to
+	child process.  Tweak comments.
+
 2015-07-06  Yaakov Selkowitz  <yselkowi@redhat.com>
 
 	* path.cc: Rework basename redefinition handling.  Explain why.
diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index 951c7fd..317aec7 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -307,39 +307,30 @@ frok::parent (volatile char * volatile stack_here)
 
   ch.forker_finished = forker_finished;
 
-  PTEB teb = NtCurrentTeb ();
-  ch.stackaddr = teb->DeallocationStack;
   ch.stackbottom = _tlsbase;
-  /* If DeallocationStack is NULL, we're running on an application-provided
-     stack.  If so, the entire stack is committed anyway and StackLimit points
-     to the allocation address of the stack.  Otherwise we're running on a
-     system-allocated stack and using StackLimit is dangerous, in case the
-     application encountered a stack overflow and recovered from it via
-     a signal handler running on an alternate stack.  Since stack_here is
-     the address of the stack pointer we start the child with anyway, we
-     can set ch.stacktop to this value rounded down to page size.  The
-     child will not need the rest of the stack anyway. */
+  ch.stackaddr = NtCurrentTeb ()->DeallocationStack;
   if (!ch.stackaddr)
-    ch.stacktop = _tlstop;
+    {
+      /* If DeallocationStack is NULL, we're running on an application-provided
+	 stack.  If so, the entire stack is committed anyway and StackLimit
+	 points to the allocation address of the stack.  Mark in guardsize that
+	 we must not set up guard pages. */
+      ch.stackaddr = ch.stacktop = _tlstop;
+      ch.guardsize = (size_t) -1;
+    }
   else
-    ch.stacktop = (void *) ((uintptr_t) stack_here & ~wincap.page_size ());
-  ch.guardsize = 0;
-  if (&_my_tls != _main_tls)
     {
-      /* We have not been started from the main thread.  Fetch the
-	 information required to set up the thread stack identically
-	 in the child. */
-      if (!ch.stackaddr)
-	{
-	  /* Pthread with application-provided stack.  Don't set up a
-	     PAGE_GUARD page.  guardsize == -1 is used in alloc_stack_hard_way
-	     to recognize this type of stack. */
-	  ch.stackaddr = _my_tls.tid->attr.stackaddr;
-	  ch.guardsize = (size_t) -1;
-	}
-      else if (_my_tls.tid)
-	/* If it's a pthread, fetch guardsize from thread attributes. */
-	ch.guardsize = _my_tls.tid->attr.guardsize;
+      /* Otherwise we're running on a system-allocated stack.  Since stack_here
+	 is the address of the stack pointer we start the child with anyway, we
+	 can set ch.stacktop to this value rounded down to page size.  The
+	 child will not need the rest of the stack anyway.  Guardsize depends
+	 on whether we're running on a pthread or not.  If pthread, we fetch
+	 the guardpage size from the pthread attribs, otherwise we use the
+	 system default. */
+      ch.stacktop = (void *) ((uintptr_t) stack_here & ~wincap.page_size ());
+      ch.guardsize = (&_my_tls != _main_tls && _my_tls.tid)
+		     ? _my_tls.tid->attr.guardsize
+		     : wincap.def_guard_page_size ();
     }
   debug_printf ("stack - bottom %p, top %p, addr %p, guardsize %ly",
 		ch.stackbottom, ch.stacktop, ch.stackaddr, ch.guardsize);



More information about the Cygwin-cvs mailing list