From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 3949C3857C6F; Thu, 28 Oct 2021 20:36:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3949C3857C6F Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: split malloc_init X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/cygwin-3_3-branch X-Git-Oldrev: 462ca33745cfa45302eeeb1d6e6bd82cc64c4027 X-Git-Newrev: e528cfe919d757ba9bc07db1798cb17560bba457 Message-Id: <20211028203627.3949C3857C6F@sourceware.org> Date: Thu, 28 Oct 2021 20:36:27 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Oct 2021 20:36:27 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=e528cfe919d757ba9bc07db1798cb17560bba457 commit e528cfe919d757ba9bc07db1798cb17560bba457 Author: Corinna Vinschen Date: Tue Oct 26 18:03:28 2021 +0200 Cygwin: split malloc_init Cygwin 3.3 only: Replace SRWLOCK usage for malloc synchronization with the first incarnation of the patch splitting malloc_init into two parts. The SRWLOCK usage requires TryAcquireSRWLockExclusive, which isn't available on Vista / Server 2008, unfortunately. Per https://cygwin.com/pipermail/cygwin-developers/2021-October/012429.html, we may encounter a crash when starting multiple threads during process startup (here: fhandler_fifo::fixup_after_{fork,exec}) which in turn allocate memory via malloc. The problem is concurrent usage of malloc before the malloc muto has been initialized. To fix this issue, split malloc_init into malloc_init_0, called from dll_crt0_0, and malloc_init_1, called from dll_crt_0_1. malloc_init_0 just initializes the muto, malloc_init_1 checks for user space provided malloc. Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/cygmalloc.h | 2 ++ winsup/cygwin/dcrt0.cc | 5 ++++- winsup/cygwin/heap.cc | 1 - winsup/cygwin/heap.h | 1 - winsup/cygwin/malloc_wrapper.cc | 4 +--- winsup/cygwin/release/3.3.1 | 6 ++++++ 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/winsup/cygwin/cygmalloc.h b/winsup/cygwin/cygmalloc.h index 84bad824c..784b6c1a7 100644 --- a/winsup/cygwin/cygmalloc.h +++ b/winsup/cygwin/cygmalloc.h @@ -39,6 +39,8 @@ void *mmap64 (void *, size_t, int, int, int, off_t); # define __malloc_lock() mallock.acquire () # define __malloc_unlock() mallock.release () extern muto mallock; +inline void malloc_init_0 () { mallock.init ("mallock"); } +extern void malloc_init_1 (); #endif diff --git a/winsup/cygwin/dcrt0.cc b/winsup/cygwin/dcrt0.cc index 6f4723bb0..31dfd6f03 100644 --- a/winsup/cygwin/dcrt0.cc +++ b/winsup/cygwin/dcrt0.cc @@ -29,6 +29,7 @@ details. */ #include "shared_info.h" #include "cygwin_version.h" #include "dll_init.h" +#include "cygmalloc.h" #include "heap.h" #include "tls_pbuf.h" #include "exception.h" @@ -769,6 +770,8 @@ dll_crt0_0 () NtOpenProcessToken (NtCurrentProcess (), MAXIMUM_ALLOWED, &hProcToken); set_cygwin_privileges (hProcToken); + malloc_init_0 (); + device::init (); do_global_ctors (&__CTOR_LIST__, 1); cygthread::init (); @@ -857,7 +860,7 @@ dll_crt0_1 (void *) on a functioning malloc and it's possible that the user's program may have overridden malloc. We only know about that at this stage, unfortunately. */ - malloc_init (); + malloc_init_1 (); user_shared->initialize (); #ifdef CYGHEAP_DEBUG diff --git a/winsup/cygwin/heap.cc b/winsup/cygwin/heap.cc index b839c8cd4..f27f81bc4 100644 --- a/winsup/cygwin/heap.cc +++ b/winsup/cygwin/heap.cc @@ -230,7 +230,6 @@ user_heap_info::init () debug_printf ("heap base %p, heap top %p, heap size %ly (%lu)", base, top, chunk, chunk); page_const--; - // malloc_init (); } #define pround(n) (((size_t)(n) + page_const) & ~page_const) diff --git a/winsup/cygwin/heap.h b/winsup/cygwin/heap.h index 565758e48..25200f286 100644 --- a/winsup/cygwin/heap.h +++ b/winsup/cygwin/heap.h @@ -10,7 +10,6 @@ details. */ /* Heap management. */ void heap_init (); -void malloc_init (); #define inheap(s) \ (cygheap->user_heap.ptr && s \ diff --git a/winsup/cygwin/malloc_wrapper.cc b/winsup/cygwin/malloc_wrapper.cc index 3b245800a..3fe961e64 100644 --- a/winsup/cygwin/malloc_wrapper.cc +++ b/winsup/cygwin/malloc_wrapper.cc @@ -272,10 +272,8 @@ strdup (const char *s) muto NO_COPY mallock; void -malloc_init () +malloc_init_1 () { - mallock.init ("mallock"); - /* Check if malloc is provided by application. If so, redirect all calls to malloc/free/realloc to application provided. This may happen if some other dll calls cygwin's malloc, but main code provides diff --git a/winsup/cygwin/release/3.3.1 b/winsup/cygwin/release/3.3.1 new file mode 100644 index 000000000..8af5f9139 --- /dev/null +++ b/winsup/cygwin/release/3.3.1 @@ -0,0 +1,6 @@ +Bug Fixes +--------- + +- Fix a fix in 3.3.0 which broke Vista / Server 2008 by using a Windows + function introduced with Windows 7 only, namely TryAcquireSRWLockExclusive. + Addresses: https://cygwin.com/pipermail/cygwin/2021-October/249732.html