[newlib-cygwin] Cygwin: lock cygheap during fork
Jeremy Drake
jeremyd2019@sourceware.org
Sat Sep 20 18:35:53 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=8a5d39527f9a56d1a623e86d30af6b590fd1472d
commit 8a5d39527f9a56d1a623e86d30af6b590fd1472d
Author: Jeremy Drake <cygwin@jdrake.com>
Date: Fri Sep 19 13:36:30 2025 -0700
Cygwin: lock cygheap during fork
another thread may simultaneously be doing a cmalloc/cfree while the
cygheap is being copied to the child.
Addresses: https://cygwin.com/pipermail/cygwin/2025-September/258801.html
Signed-off-by: Jeremy Drake <cygwin@jdrake.com>
Diff:
---
winsup/cygwin/fork.cc | 7 ++++++-
winsup/cygwin/release/3.6.5 | 4 ++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index 4abc52598..7156fc31d 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -329,6 +329,7 @@ frok::parent (volatile char * volatile stack_here)
/* NEVER, EVER, call a function which in turn calls malloc&friends while this
malloc lock is active! */
__malloc_lock ();
+ cygheap->lock ();
bool locked = true;
/* Remove impersonation */
@@ -483,6 +484,7 @@ frok::parent (volatile char * volatile stack_here)
impure, impure_beg, impure_end,
NULL);
+ cygheap->unlock ();
__malloc_unlock ();
locked = false;
if (!rc)
@@ -568,7 +570,10 @@ cleanup:
if (fix_impersonation)
cygheap->user.reimpersonate ();
if (locked)
- __malloc_unlock ();
+ {
+ cygheap->unlock ();
+ __malloc_unlock ();
+ }
/* Remember to de-allocate the fd table. */
if (hchild)
diff --git a/winsup/cygwin/release/3.6.5 b/winsup/cygwin/release/3.6.5
index 6a37b6a43..d7cf8381f 100644
--- a/winsup/cygwin/release/3.6.5
+++ b/winsup/cygwin/release/3.6.5
@@ -25,3 +25,7 @@ Fixes:
- Fix Ctrl-O (FLUSHO) handling.
Addresses: https://cygwin.com/pipermail/cygwin/2025-August/258717.html
+
+- Fix multi-thread safety of fork()/exec() by adding the same locking as was
+ done for spawn.
+ Addresses: https://cygwin.com/pipermail/cygwin/2025-September/258801.html
More information about the Cygwin-cvs
mailing list