[PATCH 4/5] tests/test-elf_cntl_gelf_getshdr.c: Close fd unconditionally

2025-01-30 Thread Aaron Merey
test-elf_cntl_gelf_getshdr conditionally closes a file descriptor
depending on a command line argument. This causes an error when run
under valgrind --track-fds=yes.

Fix this by unconditionally closing the fd.

Signed-off-by: Aaron Merey 
---
 tests/test-elf_cntl_gelf_getshdr.c | 20 +---
 1 file changed, 5 insertions(+), 15 deletions(-)

diff --git a/tests/test-elf_cntl_gelf_getshdr.c 
b/tests/test-elf_cntl_gelf_getshdr.c
index 7371110c..9f78bec2 100644
--- a/tests/test-elf_cntl_gelf_getshdr.c
+++ b/tests/test-elf_cntl_gelf_getshdr.c
@@ -43,22 +43,12 @@ main (int argc, char *argv[])
 }
 
   bool do_mmap = false;
-  bool close_fd = false;
   if (strcmp (argv[1], "READ") == 0)
-{
-  do_mmap = false;
-  close_fd = false;
-}
+do_mmap = false;
   else if (strcmp (argv[1], "MMAP") == 0)
-{
-  do_mmap = true;
-  close_fd = false;
-}
+do_mmap = true;
   else if  (strcmp (argv[1], "FDREAD") == 0)
-{
-  do_mmap = false;
-  close_fd = true;
-}
+do_mmap = false;
   else
 {
   fprintf (stderr, "First arg needs to be 'READ', 'MMAP' or 'FDREAD'\n");
@@ -83,7 +73,7 @@ main (int argc, char *argv[])
   exit (2);
 }
 
-  if (! do_mmap && close_fd)
+  if (! do_mmap)
 {
   if (elf_cntl (elf, ELF_C_FDREAD) < 0)
{
@@ -91,7 +81,6 @@ main (int argc, char *argv[])
   elf_errmsg (-1));
  exit (1);
}
-  close (fd);
 }
 
   Elf_Scn *scn = NULL;
@@ -103,5 +92,6 @@ main (int argc, char *argv[])
 }
 
   elf_end (elf);
+  close (fd);
   exit (0);
 }
-- 
2.48.1



[PATCH 2/5] libdwfl/offline.c: Avoid closing invalid fd

2025-01-30 Thread Aaron Merey
process_archive may be called with an fd argument of -1, which
libelf interprets as "no file opened".  However when closing
the fd process_archive does not check whether the fd is valid
and may attempt to close an fd of -1.

Signed-off-by: Aaron Merey 
---
 libdwfl/offline.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libdwfl/offline.c b/libdwfl/offline.c
index 24e9e180..dc099d2b 100644
--- a/libdwfl/offline.c
+++ b/libdwfl/offline.c
@@ -271,7 +271,8 @@ process_archive (Dwfl *dwfl, const char *name, const char 
*file_name, int fd,
  zero, that module will close FD.  If no modules survived the predicate,
  we are all done with the file right here.  */
   if (mod != NULL  /* If no modules, caller will clean up.  */
-  && elf_end (archive) == 0)
+  && elf_end (archive) == 0
+  && fd >= 0)
 close (fd);
 
   return mod;
-- 
2.48.1



[PATCH 3/5] tests/backtrace-subr.sh: Avoid valgrind track-fds in check_native_core

2025-01-30 Thread Aaron Merey
valgrind --track-fds=yes might incorrectly report an error due to the use
of inherited file descriptors in check_native_core.

Prevent this false positive by temporarily removing "--track-fds=yes" from
$VALGRIND_CMD for the duration of the testrun in check_native_core.

Signed-off-by: Aaron Merey 
---
 tests/backtrace-subr.sh | 10 +-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tests/backtrace-subr.sh b/tests/backtrace-subr.sh
index b63e3814..0a5b38f8 100644
--- a/tests/backtrace-subr.sh
+++ b/tests/backtrace-subr.sh
@@ -187,7 +187,9 @@ check_native_core()
   fi
 
   if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then
-VALGRIND_CMD="$SAVED_VALGRIND_CMD"
+# Restore $VALGRIND_CMD but disable --track-fds for the following testrun.
+# Valgrind --track-fds might complain about an inherited fd.
+VALGRIND_CMD=$(sed 's/--track-fds=yes//g' <<< "$SAVED_VALGRIND_CMD")
 export VALGRIND_CMD
   fi
 
@@ -195,6 +197,12 @@ check_native_core()
   # - see function check_err.
   tempfiles $core{,.{bt,err}}
   (set +ex; testrun ${abs_builddir}/backtrace -e ${abs_builddir}/$child 
--core=$core 1>$core.bt 2>$core.err; true)
+
+  if [ "x$SAVED_VALGRIND_CMD" != "x" ]; then
+VALGRIND_CMD="$SAVED_VALGRIND_CMD"
+export VALGRIND_CMD
+  fi
+
   cat $core.{bt,err}
   check_native_unsupported $core.err $child-$core
   check_all $core.{bt,err} $child-$core
-- 
2.48.1



[PATCH 5/5] tests: Avoid leaking file descriptors

2025-01-30 Thread Aaron Merey
Add calls to close for all test programs that leak file descriptors
in order to prevent test failures when run under valgrind
--track-fds=yes.

Signed-off-by: Aaron Merey 
---
 tests/all-dwarf-ranges.c| 2 ++
 tests/alldts.c  | 1 +
 tests/dwarf-getmacros.c | 3 ++-
 tests/dwarf-ranges.c| 3 ++-
 tests/dwarfcfi.c| 1 +
 tests/dwfl-core-noncontig.c | 2 ++
 tests/early-offscn.c| 1 +
 tests/ecp.c | 1 +
 tests/newfile.c | 1 +
 tests/rerequest_tag.c   | 2 ++
 tests/test-flag-nobits.c| 2 ++
 tests/update1.c | 1 +
 tests/update2.c | 1 +
 tests/update3.c | 1 +
 tests/update4.c | 1 +
 15 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/tests/all-dwarf-ranges.c b/tests/all-dwarf-ranges.c
index 4331a05b..5924c0ff 100644
--- a/tests/all-dwarf-ranges.c
+++ b/tests/all-dwarf-ranges.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 
 static void
 ranges_die (Dwarf_Die *die)
@@ -85,6 +86,7 @@ main (int argc, char *argv[])
   walk_tree (&die);
 }
   dwarf_end (dbg);
+  close (fd);
 
   return 0;
 }
diff --git a/tests/alldts.c b/tests/alldts.c
index d0fe4f24..cd12c14d 100644
--- a/tests/alldts.c
+++ b/tests/alldts.c
@@ -268,5 +268,6 @@ main (void)
   return 1;
 }
 
+  close (fd);
   return 0;
 }
diff --git a/tests/dwarf-getmacros.c b/tests/dwarf-getmacros.c
index 8381d42c..a9f90ee4 100644
--- a/tests/dwarf-getmacros.c
+++ b/tests/dwarf-getmacros.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 
 static void include (Dwarf *dbg, Dwarf_Off macoff, ptrdiff_t token);
 
@@ -174,6 +175,6 @@ main (int argc, char *argv[])
 }
 
   dwarf_end (dbg);
-
+  close (fd);
   return 0;
 }
diff --git a/tests/dwarf-ranges.c b/tests/dwarf-ranges.c
index 4bcf96ce..e111a608 100644
--- a/tests/dwarf-ranges.c
+++ b/tests/dwarf-ranges.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 
 int
 main (int argc, char *argv[])
@@ -52,6 +53,6 @@ main (int argc, char *argv[])
   start, end, base);
 
   dwarf_end (dbg);
-
+  close (fd);
   return 0;
 }
diff --git a/tests/dwarfcfi.c b/tests/dwarfcfi.c
index 29849e71..5f25228d 100644
--- a/tests/dwarfcfi.c
+++ b/tests/dwarfcfi.c
@@ -170,6 +170,7 @@ main (int argc, char *argv[])
 
   dwarf_end (dwarf);
   elf_end (elf);
+  close (fd);
 
   return result;
 }
diff --git a/tests/dwfl-core-noncontig.c b/tests/dwfl-core-noncontig.c
index 04558e28..f4170206 100644
--- a/tests/dwfl-core-noncontig.c
+++ b/tests/dwfl-core-noncontig.c
@@ -21,6 +21,7 @@
 #include 
 #include ELFUTILS_HEADER(dwfl)
 #include ELFUTILS_HEADER(elf)
+#include 
 
 static const Dwfl_Callbacks cb =
 {
@@ -77,6 +78,7 @@ main (int argc, char **argv)
 
   dwfl_end (dwfl);
   elf_end (elf);
+  close (fd);
 
   return 0;
 }
diff --git a/tests/early-offscn.c b/tests/early-offscn.c
index af29da5a..9ebba29c 100644
--- a/tests/early-offscn.c
+++ b/tests/early-offscn.c
@@ -48,5 +48,6 @@ main (int argc, char *argv[])
 error (3, 0, "gelf_offscn: %s", elf_errmsg (-1));
 
   elf_end (elf);
+  close (fd);
   return 0;
 }
diff --git a/tests/ecp.c b/tests/ecp.c
index 44a7bda2..eb16eb4a 100644
--- a/tests/ecp.c
+++ b/tests/ecp.c
@@ -94,6 +94,7 @@ main (int argc, char *argv[])
   close (outfd);
 
   elf_end (inelf);
+  close (infd);
 
   return 0;
 }
diff --git a/tests/newfile.c b/tests/newfile.c
index 5eabdcb7..be3bd42a 100644
--- a/tests/newfile.c
+++ b/tests/newfile.c
@@ -166,5 +166,6 @@ main (int argc, char *argv[] __attribute__ ((unused)))
   (void) elf_end (elf);
 }
 
+  close (fd);
   return result;
 }
diff --git a/tests/rerequest_tag.c b/tests/rerequest_tag.c
index b4d46271..058b8c49 100644
--- a/tests/rerequest_tag.c
+++ b/tests/rerequest_tag.c
@@ -21,6 +21,7 @@
 #include 
 #include 
 #include 
+#include 
 
 int
 main (int argc, char **argv)
@@ -43,5 +44,6 @@ main (int argc, char **argv)
   assert (dwarf_tag (die) == 0);
 
   dwarf_end (dw);
+  close (i);
   return 0;
 }
diff --git a/tests/test-flag-nobits.c b/tests/test-flag-nobits.c
index 15d44ea8..c6658d9f 100644
--- a/tests/test-flag-nobits.c
+++ b/tests/test-flag-nobits.c
@@ -21,6 +21,7 @@
 #include 
 #include 
 #include 
+#include 
 
 int
 main (int argc, char **argv)
@@ -38,5 +39,6 @@ main (int argc, char **argv)
 elf_flagdata (elf_getdata (scn, NULL), ELF_C_SET, ELF_F_DIRTY);
 
   elf_end (stripped);
+  close (fd);
   return 0;
 }
diff --git a/tests/update1.c b/tests/update1.c
index b7be4e5f..4d436126 100644
--- a/tests/update1.c
+++ b/tests/update1.c
@@ -123,6 +123,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
 }
 
   unlink (fname);
+  close (fd);
 
   return 0;
 }
diff --git a/tests/update2.c b/tests/update2.c
index 71455633..f5d7230f 100644
--- a/tests/update2.c
+++ b/tests/update2.c
@@ -146,6 +146,7 @@ main (int argc, char *argv[] __attribute__ ((unused)))
 }
 
   unlink (fname);
+  close (fd);
 
   return 0;
 }
diff --git a/tests/update3.c b/te

[PATCH 1/5] tests/Makefile.am: Add --track-fds=yes to valgrind_cmd

2025-01-30 Thread Aaron Merey
`valgrind --track-fds=yes` will report errors for file descriptor leaks
and attempts at closing invalid file descriptors.

Signed-off-by: Aaron Merey 
---
 tests/Makefile.am | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8f087798..625a014f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -692,7 +692,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 
 
 if USE_VALGRIND
-valgrind_cmd=valgrind -q --leak-check=full --error-exitcode=1
+valgrind_cmd=valgrind -q --leak-check=full --error-exitcode=1 --track-fds=yes
 endif
 
 
-- 
2.48.1



Re: [PATCH] stacktrace: Add missing locale.h

2025-01-30 Thread parona
Hello,

On Thursday, January 30th, 2025 at 12:59 PM, Mark Wielaard  
wrote:

> Hi,
> 
> On Thu, Jan 30, 2025 at 12:05:59AM +, parona wrote:
> 
> > While building elfutils on musl with different features enabled I
> > hit this.
> > 
> > Curiously this also affects glibc if you explicitly build without
> > optimizations.
> 
> 
> Could you show the configure/build flags and the error the compiler
> gives? I think the patch is correct, but I have trouble replicating
> the issue.

export CFLAGS="-O0 -pipe"
autoreconf -fi
./configure --enable-stacktrace --enable-maintainer-mode
make

stacktrace.c: In function ‘main’:
stacktrace.c:1410:10: error: implicit declaration of function ‘setlocale’ 
[-Wimplicit-function-declaration]
 1410 |   (void) setlocale (LC_ALL, "");
  |  ^
  GEN  libar.manifest
stacktrace.c:1410:21: error: ‘LC_ALL’ undeclared (first use in this function)
 1410 |   (void) setlocale (LC_ALL, "");
  | ^~
stacktrace.c:1410:21: note: each undeclared identifier is reported only once 
for each function it appears in

This occurs on musl without any special CFLAGS.


Re: [PATCH] stacktrace: Add missing locale.h

2025-01-30 Thread Mark Wielaard
On Thu, Jan 30, 2025 at 11:05:20AM +, parona wrote:
> > Could you show the configure/build flags and the error the compiler
> > gives? I think the patch is correct, but I have trouble replicating
> > the issue.
> 
> export CFLAGS="-O0 -pipe"
> autoreconf -fi
> ./configure --enable-stacktrace --enable-maintainer-mode
> make
> 
> stacktrace.c: In function ‘main’:
> stacktrace.c:1410:10: error: implicit declaration of function ‘setlocale’ 
> [-Wimplicit-function-declaration]
>  1410 |   (void) setlocale (LC_ALL, "");
>   |  ^
>   GEN  libar.manifest
> stacktrace.c:1410:21: error: ‘LC_ALL’ undeclared (first use in this function)
>  1410 |   (void) setlocale (LC_ALL, "");
>   | ^~
> stacktrace.c:1410:21: note: each undeclared identifier is reported only once 
> for each function it appears in
> 
> This occurs on musl without any special CFLAGS.

Ah, how embarrassing I forgot that stacktrace is still experimental
and needs a configure flag to enable it. Doh.

Yes, this is obviously the correct thing to do. Pushed.

Thanks,

Mark


Re: [PATCH] stacktrace: Add missing locale.h

2025-01-30 Thread Mark Wielaard
Hi,

On Thu, Jan 30, 2025 at 12:05:59AM +, parona wrote:
> While building elfutils on musl with different features enabled I
> hit this.
>
> Curiously this also affects glibc if you explicitly build without
> optimizations.

Could you show the configure/build flags and the error the compiler
gives? I think the patch is correct, but I have trouble replicating
the issue.

Thanks,

Mark
 
> I didn't observe any explicit order with the headers, so I just added it 
> after the rest.

> From 4a11af08aea00cbe6362e82a14010ca3ea5fa9e7 Mon Sep 17 00:00:00 2001
> From: Alfred Wingate 
> Date: Wed, 29 Jan 2025 10:06:22 +0200
> Subject: [PATCH] stacktrace: Add missing locale.h
> 
> The missing header is only obvious on musl and on glibc when you don't
> have optimizations enabled.
> 
> Normally the header would transitively come from config.h -> ./lib/eu-config.h
> -> glibc libintl.h. with __OPTIMIZE__.
> 
> https://sourceware.org/git/?p=glibc.git;a=blob;f=intl/libintl.h;hb=HEAD#l103
> 
> Signed-off-by: Alfred Wingate 
> ---
>  src/stacktrace.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/src/stacktrace.c b/src/stacktrace.c
> index b912ca5d..d8699ce5 100644
> --- a/src/stacktrace.c
> +++ b/src/stacktrace.c
> @@ -77,6 +77,7 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  
>  #include 
>  
> -- 
> 2.48.1
>