summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAleksei Vetrov <vvvvvv@google.com>2022-09-20 13:36:37 +0000
committerMark Wielaard <mark@klomp.org>2022-10-16 17:09:42 +0200
commit64ee2cb792e7b6ba6ad2a5759bff7ce8714e4668 (patch)
treeac911024367364c26a5a557b6aed95aaed3245e9 /tests
parent6284f4d12ccbc6405e986fd84ac6d4d72dc9c2a7 (diff)
downloadelfutils-64ee2cb792e7b6ba6ad2a5759bff7ce8714e4668.tar.gz
libdwfl: add dwfl_report_offline_memory
This method allows to read and report ELF from memory instead of opening a file. That way arbitrary memory can be worked with, e.g. when coming from a stream without the need to persist. Another useful application is for fuzzing, because fuzzers might be able to track accesses to the memory and change the fuzzer input to cover more edge cases through more targeted input. Hence, add a new function along with a test case. Signed-off-by: Aleksei Vetrov <vvvvvv@google.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/ChangeLog9
-rw-r--r--tests/Makefile.am4
-rw-r--r--tests/dwfl-report-offline-memory.c97
-rwxr-xr-xtests/run-dwfl-report-offline-memory.sh26
4 files changed, 136 insertions, 0 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog
index d10acfa0..6ac2c1e8 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,12 @@
+2022-09-13 Aleksei Vetrov <vvvvvv@google.com>
+
+ * Makefile.am (check_PROGRAMS): Add dwfl-report-offline-memory.
+ (TESTS): Add run-dwfl-report-offline-memory.sh.
+ (EXTRA_DIST): Likewise.
+ (dwfl_report_offline_memory_LDADD): New variable.
+ * dwfl-report-offline-memory.c: New file.
+ * run-dwfl-report-offline-memory.sh: Likewise.
+
2022-09-13 Khem Raj <raj.khem@gmail.com>
* Makefile.am (*_LDADD): Add libeu if needed for error.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 142b9c30..f680d3e1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -47,6 +47,7 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
alldts typeiter typeiter2 low_high_pc \
test-elf_cntl_gelf_getshdr dwflsyms dwfllines \
dwfl-report-elf-align dwfl-report-segment-contiguous \
+ dwfl-report-offline-memory \
varlocs backtrace backtrace-child \
backtrace-data backtrace-dwarf debuglink debugaltlink \
buildid deleted deleted-lib.so aggregate_size peel_type \
@@ -149,6 +150,7 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
run-readelf-mixed-corenote.sh run-dwfllines.sh \
run-readelf-variant.sh run-readelf-fat-lto.sh \
run-dwfl-report-elf-align.sh run-addr2line-test.sh \
+ run-dwfl-report-offline-memory.sh \
run-addr2line-i-test.sh run-addr2line-i-lex-test.sh \
run-addr2line-i-demangle-test.sh run-addr2line-alt-debugpath.sh \
run-varlocs.sh run-exprlocs.sh run-varlocs-vars.sh run-funcretval.sh \
@@ -413,6 +415,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
testfile69.core.bz2 testfile69.so.bz2 \
testfile70.core.bz2 testfile70.exec.bz2 testfile71.bz2 \
run-dwfllines.sh run-dwfl-report-elf-align.sh \
+ run-dwfl-report-offline-memory.sh \
testfile-dwfl-report-elf-align-shlib.so.bz2 \
testfilenolines.bz2 test-core-lib.so.bz2 test-core.core.bz2 \
test-core.exec.bz2 run-addr2line-test.sh \
@@ -711,6 +714,7 @@ test_elf_cntl_gelf_getshdr_LDADD = $(libelf)
dwflsyms_LDADD = $(libdw) $(libelf) $(argp_LDADD)
dwfllines_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD)
dwfl_report_elf_align_LDADD = $(libeu) $(libdw)
+dwfl_report_offline_memory_LDADD = $(libeu) $(libdw)
dwfl_report_segment_contiguous_LDADD = $(libdw) $(libebl) $(libelf)
varlocs_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD)
backtrace_LDADD = $(libeu) $(libdw) $(libelf) $(argp_LDADD)
diff --git a/tests/dwfl-report-offline-memory.c b/tests/dwfl-report-offline-memory.c
new file mode 100644
index 00000000..837aca5e
--- /dev/null
+++ b/tests/dwfl-report-offline-memory.c
@@ -0,0 +1,97 @@
+/* Test program for dwfl_report_offline_memory.
+ Copyright (C) 2022 Google LLC
+ 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 <assert.h>
+#include <config.h>
+#include <fcntl.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdio_ext.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include ELFUTILS_HEADER(dwfl)
+#include "system.h"
+
+
+static const Dwfl_Callbacks offline_callbacks =
+ {
+ .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
+ .section_address = INTUSE(dwfl_offline_section_address),
+ };
+
+static int
+count_modules (Dwfl_Module *mod __attribute__ ((unused)),
+ void **userdata __attribute__ ((unused)),
+ const char *name __attribute__ ((unused)),
+ Dwarf_Addr base __attribute__ ((unused)), void *arg)
+{
+ unsigned long long *counter = arg;
+ ++(*counter);
+ return DWARF_CB_OK;
+}
+
+int
+main (int argc, char **argv)
+{
+ /* We use no threads here which can interfere with handling a stream. */
+ (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
+
+ /* Set locale. */
+ (void) setlocale (LC_ALL, "");
+
+ if (argc != 3)
+ error (-1, 0,
+ "usage: dwfl_report_offline_memory [filename] "
+ "[expected number of modules]");
+
+ const char *fname = argv[1];
+ int fd = open (fname, O_RDONLY);
+ if (fd < 0)
+ error (-1, 0, "can't open file %s: %s", fname, strerror (errno));
+ size_t size = lseek (fd, 0, SEEK_END);
+ lseek (fd, 0, SEEK_SET);
+ char *data = malloc (size);
+ size_t bytes_read = read (fd, data, size);
+ assert (bytes_read == size);
+ close (fd);
+
+ Dwfl *dwfl = dwfl_begin (&offline_callbacks);
+ assert (dwfl != NULL);
+
+ Dwfl_Module *mod =
+ dwfl_report_offline_memory (dwfl, argv[1], argv[1], data, size);
+ assert (mod != NULL);
+ dwfl_report_end (dwfl, NULL, NULL);
+
+ unsigned long long number_of_modules = 0;
+ ptrdiff_t offset =
+ dwfl_getmodules (dwfl, &count_modules, &number_of_modules, 0);
+ if (offset < 0)
+ error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
+ assert (offset == 0);
+
+ char *endptr;
+ unsigned long long expected_number_of_modules =
+ strtoull (argv[2], &endptr, 0);
+ assert (endptr && !*endptr);
+ assert (number_of_modules == expected_number_of_modules);
+
+ dwfl_end (dwfl);
+ free (data);
+
+ return 0;
+}
diff --git a/tests/run-dwfl-report-offline-memory.sh b/tests/run-dwfl-report-offline-memory.sh
new file mode 100755
index 00000000..644a45dc
--- /dev/null
+++ b/tests/run-dwfl-report-offline-memory.sh
@@ -0,0 +1,26 @@
+#! /bin/sh
+# Copyright (C) 2022 Google LLC
+# 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/test-subr.sh
+
+testfiles testfile-dwfl-report-elf-align-shlib.so
+testfiles testarchive64.a
+
+testrun ${abs_builddir}/dwfl-report-offline-memory ./testfile-dwfl-report-elf-align-shlib.so 1
+testrun ${abs_builddir}/dwfl-report-offline-memory ./testarchive64.a 3
+
+exit 0