4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-01-19 12:59:21 +08:00
Corinna Vinschen 717c36c0a4 Cygwin: fork: fix a potential hang in fork
while debugging a problem introduced in commit
63b503916d42 ("Cygwin: tls_pathbuf: Use Windows heap")
a hang in fork was encountered using the original implementation
of tls_pathbuf:

Using tmp_pathbuf inside the code block guarded by __malloc_trylock
may call malloc from tmp_pathbuf::w_get and thus trying to lock an
exclusive SRW lock recursively, which results in a deadlock.

Allocate a small SECURITY_ATTRIBUTES block on the stack rather than
allocating a 64K tmp_pathbuf.  This avoids the potential malloc call.

Drop the __malloc_trylock call entirely.  There must not be a malloc
call inside the frok::parent block guarded by __malloc_lock, and
just trying to lock is too dangerous inside fork while other threads
might actually chage the content of the heap.  Additionally, add a
comment frowning on malloc usage inside tis code block.

Fixes: 44a79a6eca3d ("Cygwin: convert malloc lock to SRWLOCK")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2022-08-29 12:25:24 +02:00

44 lines
1.0 KiB
C

/* cygmalloc.h: cygwin DLL malloc stuff
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#ifdef __cplusplus
extern "C" {
#endif
void dlfree (void *p);
void *dlmalloc (size_t size);
void *dlrealloc (void *p, size_t size);
void *dlcalloc (size_t nmemb, size_t size);
void *dlmemalign (size_t alignment, size_t bytes);
void *dlvalloc (size_t bytes);
size_t dlmalloc_usable_size (void *p);
int dlmalloc_trim (size_t);
int dlmallopt (int p, int v);
void dlmalloc_stats ();
#define MALLOC_ALIGNMENT ((size_t)16U)
#if defined (DLMALLOC_VERSION) /* Building malloc.cc */
extern "C" void __set_ENOMEM ();
# define MALLOC_FAILURE_ACTION __set_ENOMEM ()
# define USE_DL_PREFIX 1
#elif defined (__INSIDE_CYGWIN__)
# define __malloc_lock() AcquireSRWLockExclusive (&mallock)
# define __malloc_unlock() ReleaseSRWLockExclusive (&mallock)
extern SRWLOCK NO_COPY mallock;
void malloc_init ();
#endif
#ifdef __cplusplus
}
#endif