From: Mark Wielaard Date: Sun, 16 Jun 2024 22:27:12 +0000 (+0200) Subject: Close both internal pipe fds after VG_(fork) in parent and child X-Git-Tag: VALGRIND_3_24_0~108 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1263471efdf8;p=thirdparty%2Fvalgrind.git Close both internal pipe fds after VG_(fork) in parent and child An VG_fork() creates a pipe between parent and child to syncronize the two processes. The parent wants to register the child pid before the child can run. This is done in register_sigchld_ignore. Make sure both the parent and the child close both the read and write file descriptors so none leak. https://bugs.kde.org/show_bug.cgi?id=479661 --- diff --git a/coregrind/m_libcproc.c b/coregrind/m_libcproc.c index 11dabe768..8422e9d11 100644 --- a/coregrind/m_libcproc.c +++ b/coregrind/m_libcproc.c @@ -905,6 +905,8 @@ static void register_sigchld_ignore ( Int pid, Int fds[2]) return; if (pid == 0) { + /* We are the child, close writing fd that we don't use. */ + VG_(close)(fds[1]); /* Before proceeding, ensure parent has recorded child PID in map of SIGCHLD to ignore */ while (child_wait == 1) @@ -916,6 +918,7 @@ static void register_sigchld_ignore ( Int pid, Int fds[2]) } } + /* Now close reading fd. */ VG_(close)(fds[0]); return; } @@ -926,11 +929,15 @@ static void register_sigchld_ignore ( Int pid, Int fds[2]) ht_sigchld_ignore = VG_(HT_construct)("ht.sigchld.ignore"); VG_(HT_add_node)(ht_sigchld_ignore, n); + /* We are the parent process, close read fd that we don't use. */ + VG_(close)(fds[0]); + child_wait = 0; if (VG_(write)(fds[1], &child_wait, sizeof(Int)) <= 0) VG_(message)(Vg_DebugMsg, "warning: Unable to record PID of internal process (write)\n"); + /* Now close writing fd. */ VG_(close)(fds[1]); } diff --git a/none/tests/Makefile.am b/none/tests/Makefile.am index 553a9c865..8c1bc014b 100644 --- a/none/tests/Makefile.am +++ b/none/tests/Makefile.am @@ -240,6 +240,7 @@ EXTRA_DIST = \ threadederrno.vgtest \ timestamp.stderr.exp timestamp.vgtest \ tls.vgtest tls.stderr.exp tls.stdout.exp \ + track-fds-exec-children.vgtest track-fds-exec-children.stderr.exp \ unit_debuglog.stderr.exp unit_debuglog.vgtest \ vgprintf.stderr.exp vgprintf.vgtest \ vgprintf_nvalgrind.stderr.exp vgprintf_nvalgrind.vgtest \ @@ -297,6 +298,7 @@ check_PROGRAMS = \ tls \ tls.so \ tls2.so \ + track-fds-exec-children \ unit_debuglog \ valgrind_cpp_test \ vgprintf \ @@ -435,6 +437,7 @@ if VGCONF_OS_IS_DARWIN else tls2_so_LDFLAGS = -shared endif +track_fds_exec_children_SOURCES = track-fds-exec-children.c vgprintf_nvalgrind_SOURCES = vgprintf.c vgprintf_nvalgrind_CFLAGS = ${AM_CFLAGS} -DNVALGRIND diff --git a/none/tests/track-fds-exec-children.c b/none/tests/track-fds-exec-children.c new file mode 100644 index 000000000..7209ee73d --- /dev/null +++ b/none/tests/track-fds-exec-children.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + pid_t pid = fork (); + if (pid == 0) + execlp("true", "true", NULL); + + // Wait till true succeeds + wait (NULL); + return 0; +} diff --git a/none/tests/track-fds-exec-children.stderr.exp b/none/tests/track-fds-exec-children.stderr.exp new file mode 100644 index 000000000..e69de29bb diff --git a/none/tests/track-fds-exec-children.vgtest b/none/tests/track-fds-exec-children.vgtest new file mode 100644 index 000000000..aa926a629 --- /dev/null +++ b/none/tests/track-fds-exec-children.vgtest @@ -0,0 +1,3 @@ +env: DEBUGINFOD_URLS=file:/dev/null +prog: track-fds-exec-children +vgopts: -q --track-fds=yes --trace-children=yes