public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Use libnss_files.so for tests posix/bug-ga2 and resolv/tst-leaks2 [BZ #26821]
@ 2020-11-20 12:06 Stefan Liebler
  2020-11-20 13:40 ` H.J. Lu
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Liebler @ 2020-11-20 12:06 UTC (permalink / raw)
  To: libc-alpha; +Cc: Stefan Liebler

The tests posix/bug-ga2-mem and resolv/mtrace-tst-leaks2 are failing on
fedora 33 as mtrace reports memory leaks.

The /etc/nsswitch.conf differs between
Fedora 32: hosts:      files dns myhostname
Fedora 33: hosts:      files resolve [!UNAVAIL=return] myhostname dns

Therefore /lib64/libnss_resolve.so.2 (from systemd) and the dependencies
libgcc_s.so.1 and libpthread.so.0 are loaded.

Usually all malloc'ed resources from getaddrinfo / gethostbyname are freed
and the libraries are dlclose'd in nss/nsswitch.c:libc_freeres_fn (free_mem).
Unfortunately, /lib64/libnss_resolve.so.2 is marked with DF_1_NODELETE.
As this library is not unmapped, you'll see "Memory not freed".

Therefore those tests are now only relying on libnss_files.so by making
them test-container tests and providing the required configuration files.

By moving the tests to tests-container, those are now running with
"make check".  Therefore the mtrace part of the tests are also moved
from "make xcheck" to "make check".

bug-ga2.c is now using test-driver.c in order to support WAIT_FOR_DEBUGGER
environment variable.
---
 posix/Makefile                           |  6 +++---
 posix/bug-ga2.c                          | 13 +++++++------
 posix/bug-ga2.root/etc/hosts             |  1 +
 posix/bug-ga2.root/etc/nsswitch.conf     |  2 ++
 posix/bug-ga2.root/etc/services          |  1 +
 resolv/Makefile                          |  7 +++----
 resolv/tst-leaks2.c                      |  6 ++++--
 resolv/tst-leaks2.root/etc/hosts         |  1 +
 resolv/tst-leaks2.root/etc/nsswitch.conf |  1 +
 9 files changed, 23 insertions(+), 15 deletions(-)
 create mode 100644 posix/bug-ga2.root/etc/hosts
 create mode 100644 posix/bug-ga2.root/etc/nsswitch.conf
 create mode 100644 posix/bug-ga2.root/etc/services
 create mode 100644 resolv/tst-leaks2.root/etc/hosts
 create mode 100644 resolv/tst-leaks2.root/etc/nsswitch.conf

diff --git a/posix/Makefile b/posix/Makefile
index 693082ed28..fa2d0675cd 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -106,7 +106,8 @@ tests		:= test-errno tstgetopt testfnm runtests runptests \
 tests-internal	:= bug-regex5 bug-regex20 bug-regex33 \
 		   tst-rfc3484 tst-rfc3484-2 tst-rfc3484-3 \
 		   tst-glob_lstat_compat tst-spawn4-compat
-xtests		:= bug-ga2 tst-getaddrinfo4 tst-getaddrinfo5
+tests-container := bug-ga2
+xtests		:= tst-getaddrinfo4 tst-getaddrinfo5
 ifeq (yes,$(build-shared))
 test-srcs	:= globtest
 tests           += wordexp-test tst-exec tst-spawn tst-spawn2 tst-spawn3
@@ -153,8 +154,7 @@ tests-special += $(objpfx)bug-regex2-mem.out $(objpfx)bug-regex14-mem.out \
 		 $(objpfx)tst-boost-mem.out $(objpfx)tst-getconf.out \
 		 $(objpfx)bug-glob2-mem.out $(objpfx)tst-vfork3-mem.out \
 		 $(objpfx)tst-fnmatch-mem.out $(objpfx)bug-regex36-mem.out \
-		 $(objpfx)tst-glob-tilde-mem.out
-xtests-special += $(objpfx)bug-ga2-mem.out
+		 $(objpfx)tst-glob-tilde-mem.out $(objpfx)bug-ga2-mem.out
 endif
 
 include ../Rules
diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
index 5ea759b8ce..712e40de48 100644
--- a/posix/bug-ga2.c
+++ b/posix/bug-ga2.c
@@ -3,9 +3,10 @@
 #include <netdb.h>
 #include <stdio.h>
 #include <string.h>
+#include <support/check.h>
 
-int
-main (void)
+static int
+do_test (void)
 {
   struct addrinfo hints, *res;
   int i, ret;
@@ -20,11 +21,11 @@ main (void)
       ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
 
       if (ret)
-	{
-	  printf ("%s\n", gai_strerror (ret));
-	  return 1;
-	}
+	FAIL_EXIT1 ("%s\n", gai_strerror (ret));
+
       freeaddrinfo (res);
     }
   return 0;
 }
+
+#include <support/test-driver.c>
diff --git a/posix/bug-ga2.root/etc/hosts b/posix/bug-ga2.root/etc/hosts
new file mode 100644
index 0000000000..65151f27f5
--- /dev/null
+++ b/posix/bug-ga2.root/etc/hosts
@@ -0,0 +1 @@
+192.0.2.1 www.gnu.org
diff --git a/posix/bug-ga2.root/etc/nsswitch.conf b/posix/bug-ga2.root/etc/nsswitch.conf
new file mode 100644
index 0000000000..fd2edb44dc
--- /dev/null
+++ b/posix/bug-ga2.root/etc/nsswitch.conf
@@ -0,0 +1,2 @@
+services: files
+hosts:    files
diff --git a/posix/bug-ga2.root/etc/services b/posix/bug-ga2.root/etc/services
new file mode 100644
index 0000000000..94d4f07612
--- /dev/null
+++ b/posix/bug-ga2.root/etc/services
@@ -0,0 +1 @@
+http 80/tcp
diff --git a/resolv/Makefile b/resolv/Makefile
index dbd8f8bf4f..462c111e13 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -32,7 +32,7 @@ routines := herror inet_addr inet_ntop inet_pton nsap_addr res_init \
 	    resolv_context resolv_conf
 
 tests = tst-aton tst-leaks tst-inet_ntop
-xtests = tst-leaks2
+tests-container = tst-leaks2
 
 tests-internal += tst-inet_aton_exact
 
@@ -125,9 +125,8 @@ endif
 
 ifeq ($(run-built-tests),yes)
 ifneq (no,$(PERL))
-tests-special += $(objpfx)mtrace-tst-leaks.out
-xtests-special += $(objpfx)mtrace-tst-leaks2.out
-tests-special += $(objpfx)mtrace-tst-resolv-res_ninit.out
+tests-special += $(objpfx)mtrace-tst-leaks.out $(objpfx)mtrace-tst-leaks2.out \
+		 $(objpfx)mtrace-tst-resolv-res_ninit.out
 endif
 endif
 
diff --git a/resolv/tst-leaks2.c b/resolv/tst-leaks2.c
index a2f5db3adc..49a5d11d49 100644
--- a/resolv/tst-leaks2.c
+++ b/resolv/tst-leaks2.c
@@ -21,6 +21,7 @@
 #include <mcheck.h>
 #include <netdb.h>
 #include <resolv.h>
+#include <support/check.h>
 
 static int
 do_test (void)
@@ -28,8 +29,9 @@ do_test (void)
   mtrace ();
   for (int i = 0; i < 20; ++i)
     {
-      res_init ();
-      gethostbyname ("www.gnu.org");
+      TEST_VERIFY_EXIT (res_init () == 0);
+      if (gethostbyname ("www.gnu.org") == NULL)
+	FAIL_EXIT1 ("%s\n", hstrerror (h_errno));
     }
   return 0;
 }
diff --git a/resolv/tst-leaks2.root/etc/hosts b/resolv/tst-leaks2.root/etc/hosts
new file mode 100644
index 0000000000..65151f27f5
--- /dev/null
+++ b/resolv/tst-leaks2.root/etc/hosts
@@ -0,0 +1 @@
+192.0.2.1 www.gnu.org
diff --git a/resolv/tst-leaks2.root/etc/nsswitch.conf b/resolv/tst-leaks2.root/etc/nsswitch.conf
new file mode 100644
index 0000000000..5b0c6a4199
--- /dev/null
+++ b/resolv/tst-leaks2.root/etc/nsswitch.conf
@@ -0,0 +1 @@
+hosts: files
-- 
2.28.0


^ permalink raw reply	[flat|nested] 7+ messages in thread
* [PATCH] Use libnss_files.so for tests posix/bug-ga2 and resolv/tst-leaks2 [BZ #26821]
@ 2020-11-19 12:28 Stefan Liebler
  2020-11-19 15:11 ` H.J. Lu
  0 siblings, 1 reply; 7+ messages in thread
From: Stefan Liebler @ 2020-11-19 12:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: Stefan Liebler

The tests posix/bug-ga2-mem and resolv/mtrace-tst-leaks2 are failing on
fedora 33 as mtrace reports memory leaks.

The /etc/nsswitch.conf differs between
Fedora 32: hosts:      files dns myhostname
Fedora 33: hosts:      files resolve [!UNAVAIL=return] myhostname dns

Therefore /lib64/libnss_resolve.so.2 (from systemd) and the dependencies
libgcc_s.so.1 and libpthread.so.0 are loaded.

Usually all malloc'ed resources from getaddrinfo / gethostbyname are freed
and the libraries are dlclose'd in nss/nsswitch.c:libc_freeres_fn (free_mem).
Unfortunately, /lib64/libnss_resolve.so.2 is marked with DF_1_NODELETE.
As this library is not unmapped, you'll see "Memory not freed".

Therefore those tests are now only rely on libnss_files.so by preparing
a chroot with all required configuration files and executing the test in
a subprocess.

This patch also added support for /etc/services and /etc/nsswitch.conf
in struct support_chroot_configuration.

Note:
We can't use __nss_configure_lookup as it would lead to not setting up
nsswitch.c:service_table before calling __getservbyname_r / __gethostbyname_r.
nsswitch.c:nss_load_library would then add resources to default_table which
are not freed by nsswitch.c:libc_freeres_fn (free_mem)
---
 posix/Makefile           |  1 +
 posix/bug-ga2.c          | 68 +++++++++++++++++++++++++++++++++++-----
 resolv/Makefile          |  1 +
 resolv/tst-leaks2.c      | 63 +++++++++++++++++++++++++++++++++----
 support/namespace.h      |  4 +++
 support/support_chroot.c |  9 +++++-
 6 files changed, 132 insertions(+), 14 deletions(-)

diff --git a/posix/Makefile b/posix/Makefile
index 693082ed28..0ef4e7f4be 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -361,6 +361,7 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out
 	$(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace > $@; \
 	$(evaluate-test)
 
+$(objpfx)bug-ga2: $(libdl)
 bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace
 
 bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace
diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
index 5ea759b8ce..889f100358 100644
--- a/posix/bug-ga2.c
+++ b/posix/bug-ga2.c
@@ -1,16 +1,46 @@
 /* Test case by Sam Varshavchik <mrsam@courier-mta.com>.  */
 #include <mcheck.h>
 #include <netdb.h>
-#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
 
-int
-main (void)
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  /* Generate a /etc/nsswitch.conf instead of using __nss_configure_lookup
+     as it would lead to not setting up nsswitch.c:service_table before calling
+     __getservbyname_r.  nss_load_library would then add resources to
+     default_table which are not freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .nsswitch_conf =
+	"services: files\n"
+	"hosts: files\n",
+       .host_conf = "multi on\n",
+       .hosts = "192.0.2.1 www.gnu.org\n",
+       .services = "http 80/tcp\n"
+     });
+}
+
+static void
+subprocess_test (void *closure)
 {
   struct addrinfo hints, *res;
   int i, ret;
 
   mtrace ();
+  xchroot (chroot_env->path_chroot);
+
   for (i = 0; i < 100; i++)
     {
       memset (&hints, 0, sizeof (hints));
@@ -20,11 +50,35 @@ main (void)
       ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
 
       if (ret)
-	{
-	  printf ("%s\n", gai_strerror (ret));
-	  return 1;
-	}
+	FAIL_EXIT1 ("%s\n", gai_strerror (ret));
+
       freeaddrinfo (res);
     }
+
+  /* Ensure that the resources malloc'ed by getaddrinfo are freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  exit (EXIT_SUCCESS);
+}
+
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  /* Load the libraries now as those are not available in chroot.  */
+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
+
+  /* Run the chroot'ing test in a subprocess in order to be able to delete the
+     temporary files.  */
+  support_isolate_in_subprocess (subprocess_test, NULL);
+
+  support_chroot_free (chroot_env);
   return 0;
 }
+
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/resolv/Makefile b/resolv/Makefile
index dbd8f8bf4f..533d36eebf 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -157,6 +157,7 @@ $(objpfx)mtrace-tst-leaks.out: $(objpfx)tst-leaks.out
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks.mtrace > $@; \
 	$(evaluate-test)
 
+$(objpfx)tst-leaks2: $(libdl)
 tst-leaks2-ENV = MALLOC_TRACE=$(objpfx)tst-leaks2.mtrace
 $(objpfx)mtrace-tst-leaks2.out: $(objpfx)tst-leaks2.out
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks2.mtrace > $@; \
diff --git a/resolv/tst-leaks2.c b/resolv/tst-leaks2.c
index a2f5db3adc..8f7a807572 100644
--- a/resolv/tst-leaks2.c
+++ b/resolv/tst-leaks2.c
@@ -21,20 +21,71 @@
 #include <mcheck.h>
 #include <netdb.h>
 #include <resolv.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
 
-static int
-do_test (void)
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  /* Generate a /etc/nsswitch.conf file instead of using __nss_configure_lookup
+     as it would lead to not setting up nsswitch.c:service_table before calling
+     __gethostbyname_r. nss_load_library would then add resources to
+     default_table which are not freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem)  */
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .nsswitch_conf = "hosts: files\n",
+       .host_conf = "multi on\n",
+       .hosts = "192.0.2.1 www.gnu.org\n",
+     });
+}
+
+static void
+subprocess_test (void *closure)
 {
   mtrace ();
+  xchroot (chroot_env->path_chroot);
+
   for (int i = 0; i < 20; ++i)
     {
       res_init ();
-      gethostbyname ("www.gnu.org");
+      if (gethostbyname ("www.gnu.org") == NULL)
+	FAIL_EXIT1 ("%s\n", hstrerror (h_errno));
     }
+
+  /* Ensure that the resources malloc'ed by gethostbyname are freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  exit (EXIT_SUCCESS);
+}
+
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  /* Load the libraries now as those are not available in chroot.  */
+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
+
+  /* Run the chroot'ing test in a subprocess in order to be able to delete the
+     temporary files.  */
+  support_isolate_in_subprocess (subprocess_test, NULL);
+
+  support_chroot_free (chroot_env);
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
 #define TIMEOUT 30
-/* This defines the `main' function and some more.  */
-#include <test-skeleton.c>
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/support/namespace.h b/support/namespace.h
index 5210343877..1c74dcaa4b 100644
--- a/support/namespace.h
+++ b/support/namespace.h
@@ -75,6 +75,8 @@ struct support_chroot_configuration
   const char *hosts;            /* /etc/hosts.  */
   const char *host_conf;        /* /etc/host.conf.  */
   const char *aliases;          /* /etc/aliases.  */
+  const char *services;         /* /etc/services.  */
+  const char *nsswitch_conf;    /* /etc/nsswitch.conf.  */
 };
 
 /* The result of the creation of a chroot.  */
@@ -92,6 +94,8 @@ struct support_chroot
   char *path_hosts;             /* /etc/hosts.  */
   char *path_host_conf;         /* /etc/host.conf.  */
   char *path_aliases;           /* /etc/aliases.  */
+  char *path_services;          /* /etc/services.  */
+  char *path_nsswitch_conf;     /* /etc/nsswitch.conf.  */
 };
 
 /* Create a chroot environment.  The returned data should be freed
diff --git a/support/support_chroot.c b/support/support_chroot.c
index 4f435c1ac1..e6cabd30c5 100644
--- a/support/support_chroot.c
+++ b/support/support_chroot.c
@@ -24,6 +24,8 @@
 #include <support/test-driver.h>
 #include <support/xunistd.h>
 
+extern const char *__progname;
+
 /* If CONTENTS is not NULL, write it to the file at DIRECTORY/RELPATH,
    and store the name in *ABSPATH.  If CONTENTS is NULL, store NULL in
    *ABSPATH.  */
@@ -45,7 +47,7 @@ struct support_chroot *
 support_chroot_create (struct support_chroot_configuration conf)
 {
   struct support_chroot *chroot = xmalloc (sizeof (*chroot));
-  chroot->path_chroot = support_create_temp_directory ("tst-resolv-res_init-");
+  chroot->path_chroot = support_create_temp_directory (__progname);
 
   /* Create the /etc directory in the chroot environment.  */
   char *path_etc = xasprintf ("%s/etc", chroot->path_chroot);
@@ -57,6 +59,9 @@ support_chroot_create (struct support_chroot_configuration conf)
   write_file (path_etc, "hosts", conf.hosts, &chroot->path_hosts);
   write_file (path_etc, "host.conf", conf.host_conf, &chroot->path_host_conf);
   write_file (path_etc, "aliases", conf.aliases, &chroot->path_aliases);
+  write_file (path_etc, "services", conf.services, &chroot->path_services);
+  write_file (path_etc, "nsswitch.conf", conf.nsswitch_conf,
+	      &chroot->path_nsswitch_conf);
 
   free (path_etc);
 
@@ -79,5 +84,7 @@ support_chroot_free (struct support_chroot *chroot)
   free (chroot->path_hosts);
   free (chroot->path_host_conf);
   free (chroot->path_aliases);
+  free (chroot->path_services);
+  free (chroot->path_nsswitch_conf);
   free (chroot);
 }
-- 
2.28.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-11-23  9:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-20 12:06 [PATCH] Use libnss_files.so for tests posix/bug-ga2 and resolv/tst-leaks2 [BZ #26821] Stefan Liebler
2020-11-20 13:40 ` H.J. Lu
2020-11-23  9:56   ` Stefan Liebler
  -- strict thread matches above, loose matches on Subject: below --
2020-11-19 12:28 Stefan Liebler
2020-11-19 15:11 ` H.J. Lu
2020-11-19 15:34   ` Carlos O'Donell
2020-11-20 12:10     ` Stefan Liebler

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).