[13/15,v4] Add tests/run-eu-search-macros.sh

Message ID 20250120032041.280173-13-amerey@redhat.com
State Superseded
Headers
Series [01/15] Add eu_search_tree_init |

Commit Message

Aaron Merey Jan. 20, 2025, 3:20 a.m. UTC
  From: Heather McIntyre <hsm2@rice.edu>

	* tests/.gitignore: Add eu_search_macros.sh
	* tests/Makefile.am: Add eu_search_macros,
	run-eu-search-macros.sh.
	* tests/eu_search_macros.c: New file.
	* tests/run-eu-search-macros.sh: New file.

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Aaron Merey <amerey@redhat.com>
Signed-off-by: Mark Wielaard <mark@klomp.org>

---
v3: https://patchwork.sourceware.org/project/elfutils/patch/20240802233847.690564-9-amerey@redhat.com/

Changes in v4:
run-eu-search-tests.sh dropped and macros search_tree tests moved to
run-eu-search-macros.sh.
Formatting changes and improved lock contention in eu_search_macros.sh

 tests/.gitignore              |   1 +
 tests/Makefile.am             |   8 +-
 tests/eu_search_macros.c      | 174 ++++++++++++++++++++++++++++++++++
 tests/run-eu-search-macros.sh |  28 ++++++
 4 files changed, 208 insertions(+), 3 deletions(-)
 create mode 100644 tests/eu_search_macros.c
 create mode 100755 tests/run-eu-search-macros.sh
  

Comments

Mark Wielaard Jan. 24, 2025, 2:58 p.m. UTC | #1
Hi Aaron,

On Sun, 2025-01-19 at 22:20 -0500, Aaron Merey wrote:
> From: Heather McIntyre <hsm2@rice.edu>
> 
> 	* tests/.gitignore: Add eu_search_macros.sh
> 	* tests/Makefile.am: Add eu_search_macros,
> 	run-eu-search-macros.sh.
> 	* tests/eu_search_macros.c: New file.
> 	* tests/run-eu-search-macros.sh: New file.

Nice. Looks good. One nitpick and a question about the actual test
below.

> @@ -877,7 +877,9 @@ elf_print_reloc_syms_LDADD = $(libelf)
>  cu_dwp_section_info_LDADD = $(libdw)
>  declfiles_LDADD = $(libdw)
>  eu_search_cfi_LDFLAGS = $(if $(filter undefined,$(origin USE_LOCKS)),-pthread) $(AM_LDFLAGS)
> +eu_search_macros_LDFLAGS = $(if $(filter undefined,$(origin USE_LOCKS)),-pthread) $(AM_LDFLAGS)
>  eu_search_cfi_LDADD = $(libeu) $(libelf) $(libdw)
> +eu_search_macros_LDADD = $(libdw)

Same nitpick as for eu-search-cfi, you can probably just
unconditionally add -pthread. And I would keep the LDFLAGS and LDADD
for the same program together. But certainly not showstoppers.

> +static void *thread_work (void *arg __attribute__ ((unused)))
> +{
> +  Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
> +  for (ptrdiff_t off = DWARF_GETMACROS_START;
> +       (off = dwarf_getmacros (cudie, mac, dbg, off));)
> +    {
> +      if (off != 0)
> +	{
> +	  puts (dwarf_errmsg (dwarf_errno ()));
> +	  break;

Don't you want to exit/error here to indicate failure?

> +	}
> +    }
> +
> +  return NULL;
> +}
  

Patch

diff --git a/tests/.gitignore b/tests/.gitignore
index bf17d646..9fb56361 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -68,6 +68,7 @@ 
 /elf-print-reloc-syms
 /emptyfile
 /eu_search_cfi
+/eu_search_macros
 /fillfile
 /find-prologues
 /funcretval
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 0eb2048d..a1bb6d6a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -65,7 +65,7 @@  check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
 		  msg_tst system-elf-libelf-test system-elf-gelf-test \
 		  nvidia_extended_linemap_libdw elf-print-reloc-syms \
 		  cu-dwp-section-info declfiles \
-		  eu_search_cfi \
+		  eu_search_cfi eu_search_macros \
 		  $(asm_TESTS)
 
 asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
@@ -218,7 +218,7 @@  TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
 	run-readelf-dw-form-indirect.sh run-strip-largealign.sh \
 	run-readelf-Dd.sh run-dwfl-core-noncontig.sh run-cu-dwp-section-info.sh \
 	run-declfiles.sh \
-	run-sysroot.sh run-eu-search-cfi.sh
+	run-sysroot.sh run-eu-search-cfi.sh run-eu-search-macros.sh
 
 if !BIARCH
 export ELFUTILS_DISABLE_BIARCH = 1
@@ -690,7 +690,7 @@  EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 	     testfile-dwp-cu-index-overflow.source \
 	     testfile-define-file.bz2 \
 	     testfile-sysroot.tar.bz2 run-sysroot.sh run-debuginfod-seekable.sh \
-	     run-eu-search-cfi.sh \
+	     run-eu-search-cfi.sh run-eu-search-macros.sh \
 	     thread-safety-subr.sh
 
 
@@ -877,7 +877,9 @@  elf_print_reloc_syms_LDADD = $(libelf)
 cu_dwp_section_info_LDADD = $(libdw)
 declfiles_LDADD = $(libdw)
 eu_search_cfi_LDFLAGS = $(if $(filter undefined,$(origin USE_LOCKS)),-pthread) $(AM_LDFLAGS)
+eu_search_macros_LDFLAGS = $(if $(filter undefined,$(origin USE_LOCKS)),-pthread) $(AM_LDFLAGS)
 eu_search_cfi_LDADD = $(libeu) $(libelf) $(libdw)
+eu_search_macros_LDADD = $(libdw)
 
 # We want to test the libelf headers against the system elf.h header.
 # Don't include any -I CPPFLAGS. Except when we install our own elf.h.
diff --git a/tests/eu_search_macros.c b/tests/eu_search_macros.c
new file mode 100644
index 00000000..24c570d4
--- /dev/null
+++ b/tests/eu_search_macros.c
@@ -0,0 +1,174 @@ 
+/* Test program for eu_search_macros
+   Copyright (C) 2023 Red Hat, Inc.
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see<http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+#include ELFUTILS_HEADER(dw)
+#include <dwarf.h>
+#include <assert.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <stdatomic.h>
+#include <pthread.h>
+#include <unistd.h>
+
+static void *thread_work (void * arg);
+static int mac (Dwarf_Macro *macro, void *arg);
+static int include (Dwarf_Off macoff, ptrdiff_t token);
+
+static Dwarf *dbg;
+static ptrdiff_t cuoff;
+
+static void *thread_work (void *arg __attribute__ ((unused)))
+{
+  Dwarf_Die cudie_mem, *cudie = dwarf_offdie (dbg, cuoff, &cudie_mem);
+  for (ptrdiff_t off = DWARF_GETMACROS_START;
+       (off = dwarf_getmacros (cudie, mac, dbg, off));)
+    {
+      if (off != 0)
+	{
+	  puts (dwarf_errmsg (dwarf_errno ()));
+	  break;
+	}
+    }
+
+  return NULL;
+}
+
+static int include (Dwarf_Off macoff, ptrdiff_t token)
+{
+  while ((token = dwarf_getmacros_off (dbg, macoff, mac, dbg, token)) != 0)
+    {
+      if (token == -1)
+	{
+	  puts (dwarf_errmsg (dwarf_errno ()));
+	  return 1;
+	}
+    }
+  return 0;
+}
+
+static int
+mac (Dwarf_Macro *macro, void *arg __attribute__ ((unused)))
+{
+  unsigned int opcode;
+  dwarf_macro_opcode (macro, &opcode);
+  switch (opcode)
+    {
+    case DW_MACRO_import:
+      {
+	Dwarf_Attribute at;
+	if (dwarf_macro_param (macro, 0, &at) != 0)
+	  return DWARF_CB_ABORT;
+
+	Dwarf_Word w;
+	if (dwarf_formudata (&at, &w) != 0)
+	  return DWARF_CB_ABORT;
+	if (include (w, DWARF_GETMACROS_START) != 0)
+	  return DWARF_CB_ABORT;
+	break;
+      }
+
+    case DW_MACRO_start_file:
+      {
+	Dwarf_Files *files;
+	size_t nfiles;
+	if (dwarf_macro_getsrcfiles (dbg, macro, &files, &nfiles) < 0)
+	  {
+	    printf ("dwarf_macro_getsrcfiles: %s\n",
+		   dwarf_errmsg (dwarf_errno ()));
+	    return DWARF_CB_ABORT;
+	  }
+
+	Dwarf_Word w = 0;
+	if (dwarf_macro_param2 (macro, &w, NULL) != 0)
+	  return DWARF_CB_ABORT;
+
+        if (dwarf_filesrc (files, (size_t) w, NULL, NULL) == NULL)
+	  return DWARF_CB_ABORT;
+	break;
+      }
+
+    case DW_MACINFO_define:
+    case DW_MACRO_define_strp:
+      {
+	const char *value;
+	if (dwarf_macro_param2(macro, NULL, &value) != 0)
+	  return DWARF_CB_ABORT;
+	break;
+      }
+
+    case DW_MACINFO_undef:
+    case DW_MACRO_undef_strp:
+      break;
+
+    default:
+      {
+	size_t paramcnt;
+	if (dwarf_macro_getparamcnt (macro, &paramcnt) != 0)
+	  return DWARF_CB_ABORT;
+	break;
+      }
+    }
+
+  return DWARF_CB_OK;
+}
+
+int main (int argc, char *argv[])
+{
+  assert (argc == 3);
+  const char *name = argv[1];
+
+  int fd = open (name, O_RDONLY);
+  dbg = dwarf_begin (fd, DWARF_C_READ);
+  cuoff = strtol (argv[2], NULL, 0);
+
+  int num_threads = 4;
+  pthread_t threads[num_threads];
+
+  for (int i = 0; i < num_threads; i++)
+    {
+      if (pthread_create (&threads[i], NULL, thread_work, NULL) != 0)
+	{
+	  perror ("Failed to create thread");
+
+	  for (int j = 0; j < i; j++)
+	    pthread_cancel (threads[j]);
+
+	  dwarf_end (dbg);
+	  close (fd);
+	  return 1;
+	}
+    }
+
+  for (int i = 0; i < num_threads; i++)
+    {
+      if (pthread_join (threads[i], NULL) != 0)
+	{
+	  perror ("Failed to join thread");
+	}
+    }
+
+  dwarf_end (dbg);
+  close (fd);
+
+  return 0;
+}
diff --git a/tests/run-eu-search-macros.sh b/tests/run-eu-search-macros.sh
new file mode 100755
index 00000000..3eb5a76f
--- /dev/null
+++ b/tests/run-eu-search-macros.sh
@@ -0,0 +1,28 @@ 
+#! /bin/sh
+# Data race test for parallelizd dwarf-getmacros
+# Copyright (C) 2015, 2018 Red Hat, Inc.
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+. $srcdir/thread-safety-subr.sh
+
+check_thread_safety_enabled
+
+testfiles testfile51 testfile-macros testfile-macros-0xff
+
+testrun ${abs_builddir}/eu_search_macros testfile51 0xb
+testrun ${abs_builddir}/eu_search_macros testfile51 0x84
+testrun ${abs_builddir}/eu_search_macros testfile-macros 0xb
+testrun ${abs_builddir}/eu_search_macros testfile-macros-0xff 0xb