summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2018-05-09 23:27:12 +0200
committerMark Wielaard <[email protected]>2018-05-15 11:25:03 +0200
commitb9c76ded0f07d270bbb9314fb970bb0afcb71d58 (patch)
tree94c3f714cb6173080056f9357e40ce208b11309d /tests
parent9dd183f3d036221758b5a53a8918fd7c568282cb (diff)
libdw: Fix crashing on illegal/zero Dwarf_Die.
In some cases we create an illegal Dwarf_Die by clearing all fields. The idea is that dwarf_tag () on such a Dwarf_Die will return DW_TAG_invalid, to indicate that the Dwarf_Die is unusable (and other functions will also return errors). But when "reconstructing" the Dwarf_Die addr we might use the cu before realizing the Dwarf_Die is invalid. Fix this with an explicit NULL check and add a testcase. Signed-off-by: Mark Wielaard <[email protected]>
Diffstat (limited to 'tests')
-rw-r--r--tests/ChangeLog9
-rw-r--r--tests/Makefile.am10
-rw-r--r--tests/get-units-invalid.c96
-rwxr-xr-xtests/run-get-units-invalid.sh44
4 files changed, 156 insertions, 3 deletions
diff --git a/tests/ChangeLog b/tests/ChangeLog
index 8a098b4c..b236ee79 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,12 @@
+2018-05-11 Mark Wielaard <[email protected]>
+
+ * Makefile.am (check_PROGRAMS): Add get-units-invalid.
+ (TESTS): Add run-get-units-invalid.sh.
+ (EXTRA_DIST): Likewise.
+ (get_units_invalid_LDADD): New variable.
+ * get-units-invalid.c: New test program.
+ * run-get-units-invalid.sh: New test program runner.
+
2018-05-05 Mark Wielaard <[email protected]>
* testfile-dwarf-45.source: New file.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2f9ae237..ac16a5ec 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -55,7 +55,8 @@ check_PROGRAMS = arextract arsymtest newfile saridx scnnames sectiondump \
getsrc_die strptr newdata elfstrtab dwfl-proc-attach \
elfshphehdr elfstrmerge dwelfgnucompressed elfgetchdr \
elfgetzdata elfputzdata zstrptr emptyfile vendorelf \
- fillfile dwarf_default_lower_bound dwarf-die-addr-die
+ fillfile dwarf_default_lower_bound dwarf-die-addr-die \
+ get-units-invalid
asm_TESTS = asm-tst1 asm-tst2 asm-tst3 asm-tst4 asm-tst5 \
asm-tst6 asm-tst7 asm-tst8 asm-tst9
@@ -138,7 +139,8 @@ TESTS = run-arextract.sh run-arsymtest.sh run-ar.sh newfile test-nlist \
run-compress-test.sh \
run-readelf-zdebug.sh run-readelf-zdebug-rel.sh \
emptyfile vendorelf fillfile dwarf_default_lower_bound \
- run-dwarf-die-addr-die.sh
+ run-dwarf-die-addr-die.sh \
+ run-get-units-invalid.sh
if !BIARCH
export ELFUTILS_DISABLE_BIARCH = 1
@@ -358,7 +360,8 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
run-disasm-bpf.sh \
testfile-bpf-dis1.expect.bz2 testfile-bpf-dis1.o.bz2 \
testfile-m68k-core.bz2 testfile-m68k.bz2 testfile-m68k-s.bz2 \
- run-dwarf-die-addr-die.sh
+ run-dwarf-die-addr-die.sh \
+ run-get-units-invalid.sh
if USE_VALGRIND
valgrind_cmd='valgrind -q --leak-check=full --error-exitcode=1'
@@ -517,6 +520,7 @@ vendorelf_LDADD = $(libelf)
fillfile_LDADD = $(libelf)
dwarf_default_lower_bound_LDADD = $(libdw)
dwarf_die_addr_die_LDADD = $(libdw)
+get_units_invalid_LDADD = $(libdw)
# We want to test the libelf header against the system elf.h header.
# Don't include any -I CPPFLAGS.
diff --git a/tests/get-units-invalid.c b/tests/get-units-invalid.c
new file mode 100644
index 00000000..9ec16ee0
--- /dev/null
+++ b/tests/get-units-invalid.c
@@ -0,0 +1,96 @@
+/* Test cudie and subdie properties.
+ Copyright (C) 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/>. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <dwarf.h>
+#include ELFUTILS_HEADER(dw)
+#include <stdio.h>
+#include <inttypes.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+
+int
+main (int argc, char *argv[])
+{
+ for (int i = 1; i < argc; i++)
+ {
+ printf ("file: %s\n", argv[i]);
+ int fd = open (argv[i], O_RDONLY);
+ Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
+ if (dbg == NULL)
+ {
+ printf ("%s not usable: %s\n", argv[i], dwarf_errmsg (-1));
+ return -1;
+ }
+
+ Dwarf_CU *cu = NULL;
+ Dwarf_Die cudie, subdie;
+ uint8_t unit_type;
+ while (dwarf_get_units (dbg, cu, &cu, NULL,
+ &unit_type, &cudie, &subdie) == 0)
+ {
+ printf ("Got cudie: %s, unit_type: %" PRIx8 "\n",
+ dwarf_diename (&cudie), unit_type);
+
+ int tag = dwarf_tag (&subdie);
+ if (unit_type == DW_UT_compile)
+ {
+ if (tag != DW_TAG_invalid)
+ {
+ printf ("Not invalid: %x\n", dwarf_tag (&subdie));
+ return -1;
+ }
+ if (dwarf_diename (&subdie) != NULL)
+ {
+ printf ("Should have NULL name: %s\n",
+ dwarf_diename (&subdie));
+ return -1;
+ }
+ Dwarf_Die result;
+ if (dwarf_siblingof (&subdie, &result) != -1)
+ {
+ printf ("Should NOT have a valid sibling: %s\n",
+ dwarf_diename (&result));
+ return -1;
+ }
+ if (dwarf_child (&subdie, &result) != -1)
+ {
+ printf ("Should NOT have a valid child: %s\n",
+ dwarf_diename (&result));
+ return -1;
+ }
+ }
+ else if (unit_type == DW_UT_type)
+ printf ("subdie: %s\n", dwarf_diename (&subdie));
+ else
+ printf ("subdie tag: %x\n", dwarf_tag (&subdie));
+ }
+
+ dwarf_end (dbg);
+ close (fd);
+
+ printf ("\n");
+ }
+
+ return 0;
+}
diff --git a/tests/run-get-units-invalid.sh b/tests/run-get-units-invalid.sh
new file mode 100755
index 00000000..66ef9444
--- /dev/null
+++ b/tests/run-get-units-invalid.sh
@@ -0,0 +1,44 @@
+#! /bin/sh
+# Copyright (C) 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/test-subr.sh
+
+# See run-typeiter.sh
+testfiles testfile-debug-types
+
+testrun ${abs_builddir}/get-units-invalid testfile-debug-types
+
+# see run-readelf-dwz-multi.sh
+testfiles testfile_multi_main testfile_multi.dwz
+
+testrun ${abs_builddir}/get-units-invalid testfile_multi_main
+
+# see tests/run-dwflsyms.sh
+testfiles testfilebazdbgppc64.debug
+
+testrun ${abs_builddir}/get-units-invalid testfilebazdbgppc64.debug
+
+# see tests/testfile-dwarf-45.source
+testfiles testfile-dwarf-4 testfile-dwarf-5
+
+testrun ${abs_builddir}/get-units-invalid testfile-dwarf-4
+testrun ${abs_builddir}/get-units-invalid testfile-dwarf-5
+
+# Self test
+testrun_on_self ${abs_builddir}/get-units-invalid
+
+exit 0