When configuring with --enable-valgrind we were only running valgrind
on tests with a shell wrapper script. This patch makes sure to also run
valgrind on "pure" binary tests. This found one small issue in libasm
where we could be writing some uninitialized padding to an ELF file.
And there were a couple tests that didn't clean up all the resources
they used. Both issues are also fixed with this patch.

Signed-off-by: Mark Wielaard <m...@klomp.org>
---
 libasm/ChangeLog         |  5 +++++
 libasm/asm_align.c       |  4 ++--
 tests/ChangeLog          |  9 +++++++++
 tests/dwfl-bug-fd-leak.c |  9 ++++++++-
 tests/dwfl-proc-attach.c |  7 +++++++
 tests/test-wrapper.sh    | 10 +++++++++-
 tests/vdsosyms.c         |  2 ++
 7 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 83a65492..d7ab8c42 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,3 +1,8 @@
+2020-10-29  Mark Wielaard  <m...@klomp.org>
+
+       * asm_align.c (__libasm_ensure_section_space): Use calloc, not
+       malloc to allocate extra space.
+
 2020-07-19  Mark Wielaard  <m...@klomp.org>
 
        * libasmP.h: Include libebl.h after libasm.h.
diff --git a/libasm/asm_align.c b/libasm/asm_align.c
index e59a070e..c8c671b2 100644
--- a/libasm/asm_align.c
+++ b/libasm/asm_align.c
@@ -143,7 +143,7 @@ __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
       /* This is the first block.  */
       size = MAX (2 * len, 960);
 
-      asmscn->content = (struct AsmData *) malloc (sizeof (struct AsmData)
+      asmscn->content = (struct AsmData *) calloc (1, sizeof (struct AsmData)
                                                   + size);
       if (asmscn->content == NULL)
        return -1;
@@ -160,7 +160,7 @@ __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
 
       size = MAX (2 *len, MIN (32768, 2 * asmscn->offset));
 
-      newp = (struct AsmData *) malloc (sizeof (struct AsmData) + size);
+      newp = (struct AsmData *) calloc (1, sizeof (struct AsmData) + size);
       if (newp == NULL)
        return -1;
 
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 91aeadaf..95f09c38 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,12 @@
+2020-10-29  Mark Wielaard  <m...@klomp.org>
+
+       * test-wrapper.sh: Determine whether the test is a script or not
+       and run binaries directly under valgrind.
+       * dwfl-bug-fd-leak.c (main): Call getrlimit before calling setrlimit.
+       * dwfl-proc-attach.c (main): Call dwfl_end, pthread_cancel and
+       pthread_join.
+       * vdsosyms.c (main): Call dwfl_end.
+
 2020-10-28  Tom Tromey  <t...@tromey.com>
 
        PR26773
diff --git a/tests/dwfl-bug-fd-leak.c b/tests/dwfl-bug-fd-leak.c
index ee3a916b..b0913361 100644
--- a/tests/dwfl-bug-fd-leak.c
+++ b/tests/dwfl-bug-fd-leak.c
@@ -101,7 +101,14 @@ main (void)
   /* Set locale.  */
   (void) setlocale (LC_ALL, "");
 
-  struct rlimit fd_limit = { .rlim_cur = 32, .rlim_max = 32 };
+  /* Get both the soft and hard limits first. The soft limit is what
+     will be enforced against the process.  The hard limit is the max
+     that can be set for the soft limit.  We don't want to lower it
+     because we might not be able to (under valgrind).  */
+  struct rlimit fd_limit;
+  if (getrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
+    error (2, errno, "getrlimit");
+  fd_limit.rlim_cur = 32;
   if (setrlimit (RLIMIT_NOFILE, &fd_limit) < 0)
     error (2, errno, "setrlimit");
 
diff --git a/tests/dwfl-proc-attach.c b/tests/dwfl-proc-attach.c
index 102ba181..b72cc814 100644
--- a/tests/dwfl-proc-attach.c
+++ b/tests/dwfl-proc-attach.c
@@ -97,6 +97,13 @@ main (int argc __attribute__ ((unused)),
   if (dwfl_getthreads (dwfl, thread_callback, &threads) != DWARF_CB_OK)
     error (-1, 0, "dwfl_getthreads failed: %s", dwfl_errmsg (-1));
 
+  dwfl_end (dwfl);
+
+  pthread_cancel (thread1);
+  pthread_cancel (thread2);
+  pthread_join (thread1, NULL);
+  pthread_join (thread2, NULL);
+
   return (threads == 3) ? 0 : -1;
 }
 
diff --git a/tests/test-wrapper.sh b/tests/test-wrapper.sh
index 09b4d49f..db16c591 100755
--- a/tests/test-wrapper.sh
+++ b/tests/test-wrapper.sh
@@ -44,6 +44,7 @@ case "$1" in
 *.sh)
   export built_library_path program_transform_name elfutils_testrun
   export elfutils_tests_rpath
+  is_shell_script="yes"
   ;;
 *)
   if [ $elfutils_testrun = built ]; then
@@ -55,6 +56,7 @@ case "$1" in
     LD_LIBRARY_PATH="${libdir}:${libdir}/elfutils$old_path"
   fi
   export LD_LIBRARY_PATH
+  is_shell_script="no"
   ;;
 esac
 
@@ -62,4 +64,10 @@ if [ "x$VALGRIND_CMD" != "x" ]; then
   export VALGRIND_CMD
 fi
 
-exec "$@"
+# When it is a run-*.sh script the VALGRIND_CMD will be passed on
+# otherwise we'll need to run the binary explicitly under valgrind.
+if [ "x$is_shell_script" == "xyes" ]; then
+  exec "$@"
+else
+  exec $VALGRIND_CMD "$@"
+fi
diff --git a/tests/vdsosyms.c b/tests/vdsosyms.c
index 7bfa7381..83ab034f 100644
--- a/tests/vdsosyms.c
+++ b/tests/vdsosyms.c
@@ -103,6 +103,8 @@ main (int argc __attribute__ ((unused)), char **argv 
__attribute__ ((unused)))
   if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
     error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
 
+  dwfl_end (dwfl);
+
   /* No symbols is ok, then we haven't seen the vdso at all on this arch.  */
   return vdso_syms >= 0 ? 0 : -1;
 }
-- 
2.18.4

Reply via email to