public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Sergei Trofimovich <slyich@gmail.com>
To: gcc-patches@gcc.gnu.org
Cc: Ian Lance Taylor <ian@airs.com>,
	Sergei Trofimovich <siarheit@google.com>
Subject: [PATCH] libbacktrace: fix fd leak tests on systems with extra descriptors
Date: Thu, 12 Aug 2021 23:33:36 +0100	[thread overview]
Message-ID: <20210812223336.3398471-1-slyich@gmail.com> (raw)

From: Sergei Trofimovich <siarheit@google.com>

I noticed test failures when ran gcc test suite from under mc shell.
mc opens fd=9 and exposes it to child processes. As a result a few
tests failes:
    FAIL: b2test_buildid
    FAIL: btest_gnudebuglink
    FAIL: btest
    FAIL: btest_lto
    FAIL: btest_alloc
    FAIL: ctestg
    FAIL: ctesta
    FAIL: ctestg_alloc
    FAIL: ctesta_alloc
    FAIL: dwarf5
    FAIL: dwarf5_alloc

Instead of trying to close file descripts in range test polls for
first available file descriptor by creating it via dup(1).

libbacktrace/

	* btest.c (check_open_files): Use last free file descriptor as a
	signal for flie descriptor leak.
---
 libbacktrace/btest.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/libbacktrace/btest.c b/libbacktrace/btest.c
index 9f9c03babf3..d5cf321640c 100644
--- a/libbacktrace/btest.c
+++ b/libbacktrace/btest.c
@@ -458,22 +458,32 @@ test5 (void)
   return failures;
 }
 
+/* Peek at first free flie descriptior.  */
+
+static int probe_first_dree_fd (void) {
+  int fd;
+
+  fd = dup(1);
+  close(fd);
+
+  return fd;
+}
+
 /* Check that are no files left open.  */
 
 static void
-check_open_files (void)
+check_open_files (int last_free_fd)
 {
-  int i;
+  int fd;
+
+  fd = probe_first_dree_fd();
 
-  for (i = 3; i < 10; i++)
+  if (fd != last_free_fd)
     {
-      if (close (i) == 0)
-	{
-	  fprintf (stderr,
-		   "ERROR: descriptor %d still open after tests complete\n",
-		   i);
-	  ++failures;
-	}
+      fprintf (stderr,
+	       "ERROR: descriptor %d still open after tests complete\n",
+	       last_free_fd);
+      ++failures;
     }
 }
 
@@ -482,8 +492,11 @@ check_open_files (void)
 int
 main (int argc ATTRIBUTE_UNUSED, char **argv)
 {
+  int first_free_fd;
+
   state = backtrace_create_state (argv[0], BACKTRACE_SUPPORTS_THREADS,
 				  error_callback_create, NULL);
+  first_free_fd = probe_first_dree_fd ();
 
 #if BACKTRACE_SUPPORTED
   test1 ();
@@ -495,7 +508,7 @@ main (int argc ATTRIBUTE_UNUSED, char **argv)
 #endif
 #endif
 
-  check_open_files ();
+  check_open_files (first_free_fd);
 
   exit (failures ? EXIT_FAILURE : EXIT_SUCCESS);
 }
-- 
2.32.0


             reply	other threads:[~2021-08-12 22:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-12 22:33 Sergei Trofimovich [this message]
2021-08-12 23:16 ` Ian Lance Taylor
2021-08-13  7:05   ` Sergei Trofimovich
2021-08-13 16:52     ` Ian Lance Taylor
2021-08-13 22:05       ` Sergei Trofimovich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210812223336.3398471-1-slyich@gmail.com \
    --to=slyich@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ian@airs.com \
    --cc=siarheit@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).