From c6696a3426628761a201c12af68325b12ec6f0b8 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Thu, 23 May 2013 20:10:35 +0000 Subject: [PATCH] * dcrt0.cc (child_info_fork::alloc_stack_hard_way): Fix datatype of stacksize to SIZE_T. Cast to SIZE_T in pointer arithmetic. Slightly enhance output in case of a fatal error. * fork.cc (frok::parent): Always set ch.stackaddr to DeallocationStack value of current thread to help stack reservation in child_info_fork::alloc_stack_hard_way along. Simplify subsequent code storing stack values in ch. Print guardsize in hex, too. --- winsup/cygwin/ChangeLog | 10 ++++++++++ winsup/cygwin/dcrt0.cc | 17 ++++++++++------- winsup/cygwin/fork.cc | 18 +++++++----------- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index e91432d3e..89490e036 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,13 @@ +2013-05-23 Corinna Vinschen + + * dcrt0.cc (child_info_fork::alloc_stack_hard_way): Fix datatype of + stacksize to SIZE_T. Cast to SIZE_T in pointer arithmetic. Slightly + enhance output in case of a fatal error. + * fork.cc (frok::parent): Always set ch.stackaddr to DeallocationStack + value of current thread to help stack reservation in + child_info_fork::alloc_stack_hard_way along. Simplify subsequent code + storing stack values in ch. Print guardsize in hex, too. + 2013-05-23 Corinna Vinschen * environ.cc (set_winsymlinks): Handle "winsymlinks:nativestrict" diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 0208a9412..d435129d7 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -405,7 +405,7 @@ void child_info_fork::alloc_stack_hard_way (volatile char *b) { void *stack_ptr; - DWORD stacksize; + SIZE_T stacksize; /* First check if the requested stack area is part of the user heap or part of a mmapped region. If so, we have been started from a @@ -415,16 +415,19 @@ child_info_fork::alloc_stack_hard_way (volatile char *b) && stackbottom <= cygheap->user_heap.max) || is_mmapped_region ((caddr_t) stacktop, (caddr_t) stackbottom)) return; - /* First, try to reserve the entire stack. */ - stacksize = (char *) stackbottom - (char *) stackaddr; + stacksize = (SIZE_T) stackbottom - (SIZE_T) stackaddr; if (!VirtualAlloc (stackaddr, stacksize, MEM_RESERVE, PAGE_NOACCESS)) - api_fatal ("fork: can't reserve memory for stack %p - %p, %E", - stackaddr, stackbottom); - stacksize = (char *) stackbottom - (char *) stacktop; + { + PTEB teb = NtCurrentTeb (); + api_fatal ("fork: can't reserve memory for parent stack " + "%p - %p, (child has %p - %p), %E", + stackaddr, stackbottom, teb->DeallocationStack, _tlsbase); + } + stacksize = (SIZE_T) stackbottom - (SIZE_T) stacktop; stack_ptr = VirtualAlloc (stacktop, stacksize, MEM_COMMIT, PAGE_READWRITE); if (!stack_ptr) - abort ("can't commit memory for stack %p(%d), %E", stacktop, stacksize); + abort ("can't commit memory for stack %p(%ly), %E", stacktop, stacksize); if (guardsize != (size_t) -1) { /* Allocate PAGE_GUARD page if it still fits. */ diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc index 634621351..7d5f7101c 100644 --- a/winsup/cygwin/fork.cc +++ b/winsup/cygwin/fork.cc @@ -302,17 +302,17 @@ frok::parent (volatile char * volatile stack_here) ch.forker_finished = forker_finished; + PTEB teb = NtCurrentTeb (); ch.stackbottom = _tlsbase; ch.stacktop = (void *) _tlstop; - ch.stackaddr = 0; + ch.stackaddr = teb->DeallocationStack; 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. */ - PTEB teb = NtCurrentTeb (); - if (!teb->DeallocationStack) + 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 @@ -320,15 +320,11 @@ frok::parent (volatile char * volatile stack_here) ch.stackaddr = _my_tls.tid->attr.stackaddr; ch.guardsize = (size_t) -1; } - else - { - ch.stackaddr = teb->DeallocationStack; - /* If it's a pthread, fetch guardsize from thread attributes. */ - if (_my_tls.tid) - ch.guardsize = _my_tls.tid->attr.guardsize; - } + else if (_my_tls.tid) + /* If it's a pthread, fetch guardsize from thread attributes. */ + ch.guardsize = _my_tls.tid->attr.guardsize; } - debug_printf ("stack - bottom %p, top %p, addr %p, guardsize %lu", + debug_printf ("stack - bottom %p, top %p, addr %p, guardsize %ly", ch.stackbottom, ch.stacktop, ch.stackaddr, ch.guardsize); PROCESS_INFORMATION pi; -- 2.43.5