[binutils-gdb] libctf, create: fix ctf_type_add of structs with unnamed members
Nick Alcock
nix@sourceware.org
Tue Jan 19 12:56:19 GMT 2021
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=26503e2f5eae6019c8649a3dd204a82705efc740
commit 26503e2f5eae6019c8649a3dd204a82705efc740
Author: Nick Alcock <nick.alcock@oracle.com>
Date: Tue Jan 19 12:45:18 2021 +0000
libctf, create: fix ctf_type_add of structs with unnamed members
Our recent commit to support unnamed structure members better ditched
the old ctf_member_iter iterator body in favour of ctf_member_next.
However, these functions treat unnamed structure members differently:
ctf_member_iter just returned whatever the internal representation
contained, while ctf_member_next took care to always return "" rather
than sometimes returning "" and sometimes NULL depending on whether the
dict was dynamic (a product of ctf_create) or not (a product of
ctf_open). After this commit, ctf_member_iter did the same.
It was always a bug for external callers not to treat a "" return from
these functions as if it were NULL, so only buggy callers could be
affected -- but one of those buggy callers was ctf_add_type, which
assumed that it could just take whatever name was returned from
ctf_member_iter and slam it directly into the internal representation of
a dynamic dict -- which expects NULL for unnamed members, not "". The
net effect of all of this is that taking a struct containing unnamed
members and ctf_add_type'ing it into a dynamic dict produced a dict
whose unnamed members were inaccessible to ctf_member_info (though if
you wrote that dict out and then ctf_open'ed it, they would magically
reappear again).
Compensate for this by suitably transforming a "" name into NULL in the
internal representation, as should have been done all along.
libctf/ChangeLog
2021-01-19 Nick Alcock <nick.alcock@oracle.com>
* ctf-create.c (membadd): Transform ""-named members into
NULL-named ones.
* testsuite/libctf-regression/type-add-unnamed-struct*: New test.
Diff:
---
libctf/ChangeLog | 6 ++
libctf/ctf-create.c | 6 ++
.../type-add-unnamed-struct-ctf.c | 19 ++++++
.../libctf-regression/type-add-unnamed-struct.c | 72 ++++++++++++++++++++++
.../libctf-regression/type-add-unnamed-struct.lk | 3 +
5 files changed, 106 insertions(+)
diff --git a/libctf/ChangeLog b/libctf/ChangeLog
index b48cc050406..3a7f6ab1ced 100644
--- a/libctf/ChangeLog
+++ b/libctf/ChangeLog
@@ -1,3 +1,9 @@
+2021-01-19 Nick Alcock <nick.alcock@oracle.com>
+
+ * ctf-create.c (membadd): Transform ""-named members into
+ NULL-named ones.
+ * testsuite/libctf-regression/type-add-unnamed-struct*: New test.
+
2021-01-19 Nick Alcock <nick.alcock@oracle.com>
* ctf-lookup.c (ctf_lookup_by_name_internal): Do not return the
diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c
index 651d39d23c8..50f48eb1bb9 100644
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -2403,6 +2403,12 @@ membadd (const char *name, ctf_id_t type, unsigned long offset, void *arg)
if ((dmd = malloc (sizeof (ctf_dmdef_t))) == NULL)
return (ctf_set_errno (ctb->ctb_dict, EAGAIN));
+ /* Unnamed members in non-dynamic dicts have a name of "", while dynamic dicts
+ use NULL. Adapt. */
+
+ if (name[0] == 0)
+ name = NULL;
+
if (name != NULL && (s = strdup (name)) == NULL)
{
free (dmd);
diff --git a/libctf/testsuite/libctf-regression/type-add-unnamed-struct-ctf.c b/libctf/testsuite/libctf-regression/type-add-unnamed-struct-ctf.c
new file mode 100644
index 00000000000..d319aafbac1
--- /dev/null
+++ b/libctf/testsuite/libctf-regression/type-add-unnamed-struct-ctf.c
@@ -0,0 +1,19 @@
+struct foo
+{
+ union
+ {
+ struct
+ {
+ int bar;
+ };
+ };
+ union
+ {
+ struct
+ {
+ int baz;
+ };
+ };
+};
+
+struct foo *bar;
diff --git a/libctf/testsuite/libctf-regression/type-add-unnamed-struct.c b/libctf/testsuite/libctf-regression/type-add-unnamed-struct.c
new file mode 100644
index 00000000000..98be257991a
--- /dev/null
+++ b/libctf/testsuite/libctf-regression/type-add-unnamed-struct.c
@@ -0,0 +1,72 @@
+#include <ctf-api.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int
+main (int argc, char *argv[])
+{
+ ctf_dict_t *fp;
+ ctf_archive_t *ctf;
+ ctf_dict_t *dyn;
+ ctf_next_t *i = NULL;
+ ctf_id_t type;
+ ctf_id_t newtype;
+ const char *memb;
+ ctf_membinfo_t mi;
+ const char *membs[] = { "bar", "baz", NULL };
+ const char **walk;
+ int err;
+
+ if (argc != 2)
+ {
+ fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
+ exit(1);
+ }
+
+ if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
+ goto open_err;
+
+ if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
+ goto open_err;
+
+ if ((dyn = ctf_create (&err)) == NULL)
+ goto create_err;
+
+ /* Copy 'struct foo' into the dynamic dict, then make sure we can look up a
+ member situated inside an unnamed struct. */
+
+ if ((type = ctf_lookup_by_name (fp, "struct foo")) == CTF_ERR)
+ {
+ fprintf (stderr, "Cannot look up struct foo: %s\n", ctf_errmsg (ctf_errno (dyn)));
+ return 1;
+ }
+
+ if ((newtype = ctf_add_type (dyn, fp, type)) == CTF_ERR)
+ goto copy_err;
+
+ for (walk = membs; *walk != NULL; walk++)
+ {
+ if (ctf_member_info (dyn, newtype, *walk, &mi) < 0)
+ goto lookup_err;
+ printf ("Looked up %s, type %lx, offset %lx\n", *walk, (long) mi.ctm_type, mi.ctm_offset);
+ }
+
+ ctf_dict_close (dyn);
+ ctf_dict_close (fp);
+ ctf_close (ctf);
+
+ return 0;
+
+ open_err:
+ fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
+ return 1;
+ create_err:
+ fprintf (stderr, "%s: cannot create: %s\n", argv[0], ctf_errmsg (err));
+ return 1;
+ copy_err:
+ fprintf (stderr, "Type addition failed: %s\n", ctf_errmsg (ctf_errno (dyn)));
+ return 1;
+ lookup_err:
+ fprintf (stderr, "Cannot look up %s: %s\n", *walk, ctf_errmsg (ctf_errno (dyn)));
+ return 1;
+}
diff --git a/libctf/testsuite/libctf-regression/type-add-unnamed-struct.lk b/libctf/testsuite/libctf-regression/type-add-unnamed-struct.lk
new file mode 100644
index 00000000000..caa8934ed2e
--- /dev/null
+++ b/libctf/testsuite/libctf-regression/type-add-unnamed-struct.lk
@@ -0,0 +1,3 @@
+# source: type-add-unnamed-struct-ctf.c
+Looked up bar, type [0-9a-f]*, offset [0-9a-f]*
+Looked up baz, type [0-9a-f]*, offset [0-9a-f]*
More information about the Binutils-cvs
mailing list