[PATCH 3/4] libctf: fix slices of slices and of enums
Nick Alcock
nick.alcock@oracle.com
Tue Feb 25 21:10:14 GMT 2025
Slices had a bunch of horrible usability problems. In particular, while
towers of cv-quals are resolved away by functions that need to do it, towers
of cv-quals with slices in the middle are not resolved away by functions
like ctf_enum_value that can see through slices: resolving volatile -> slice
-> const -> enum will leave it with a 'const', which will error pointlessly,
annoying callers, who reasonably expect slices to be more invisible than
this. (The user-callable ctf_type_resolve still does not resolve away
slices, because this is the only way users can see that the slices are there
at all.)
This is induced by a fix for another wart: ctf_add_enumerator does not
resolve anything away at all, so you can't even add enumerators to const or
volatile enums -- and more problematically, you can't add enumerators to
enums with an explicit encoding without resolving away the types by hand,
since ctf_add_enum_encoded works by returning a slice! ctf_add_enumerator
now resolves away all of those, so any cvr-or-typedef-or-slice-qual
terminating in an enum can be added to, exactly as callers likely expect.
(New tests added.)
libctf/
* ctf-create.c (ctf_add_enumerator): Resolve away cvr-qualness.
* ctf-types.c (ctf_type_resolve_unsliced): Don't terminate at
the first slice.
* testsuite/libctf-writable/slice-of-slice.*: New test.
---
libctf/ctf-create.c | 6 +-
libctf/ctf-types.c | 20 +++--
.../libctf-writable/slice-of-slice.c | 77 +++++++++++++++++++
.../libctf-writable/slice-of-slice.lk | 1 +
4 files changed, 98 insertions(+), 6 deletions(-)
create mode 100644 libctf/testsuite/libctf-writable/slice-of-slice.c
create mode 100644 libctf/testsuite/libctf-writable/slice-of-slice.lk
diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c
index 6d2479c8cca..25dd44def3b 100644
--- a/libctf/ctf-create.c
+++ b/libctf/ctf-create.c
@@ -1044,7 +1044,7 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
int value)
{
ctf_dict_t *ofp = fp;
- ctf_dtdef_t *dtd = ctf_dtd_lookup (fp, enid);
+ ctf_dtdef_t *dtd;
unsigned char *old_vlen;
ctf_enum_t *en;
@@ -1053,6 +1053,10 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
if (name == NULL)
return (ctf_set_errno (fp, EINVAL));
+ if ((enid = ctf_type_resolve_unsliced (fp, enid)) == CTF_ERR)
+ return -1; /* errno is set for us. */
+
+ dtd = ctf_dtd_lookup (fp, enid);
if ((fp->ctf_flags & LCTF_CHILD) && LCTF_TYPE_ISPARENT (fp, enid))
fp = fp->ctf_parent;
diff --git a/libctf/ctf-types.c b/libctf/ctf-types.c
index 45200b78d79..067a5d8c3a0 100644
--- a/libctf/ctf-types.c
+++ b/libctf/ctf-types.c
@@ -594,21 +594,31 @@ ctf_type_resolve_unsliced (ctf_dict_t *fp, ctf_id_t type)
{
ctf_dict_t *ofp = fp;
const ctf_type_t *tp;
+ ctf_id_t resolved_type;
if ((type = ctf_type_resolve (fp, type)) == CTF_ERR)
return CTF_ERR;
if ((tp = ctf_lookup_by_id (&fp, type)) == NULL)
return CTF_ERR; /* errno is set for us. */
+ resolved_type = type;
- if ((LCTF_INFO_KIND (fp, tp->ctt_info)) == CTF_K_SLICE)
+ do
{
- ctf_id_t ret;
+ type = resolved_type;
- if ((ret = ctf_type_reference (fp, type)) == CTF_ERR)
- return (ctf_set_typed_errno (ofp, ctf_errno (fp)));
- return ret;
+ if ((LCTF_INFO_KIND (fp, tp->ctt_info)) == CTF_K_SLICE)
+ if ((type = ctf_type_reference (fp, type)) == CTF_ERR)
+ return (ctf_set_typed_errno (ofp, ctf_errno (fp)));
+
+ if ((resolved_type = ctf_type_resolve (fp, type)) == CTF_ERR)
+ return CTF_ERR;
+
+ if ((tp = ctf_lookup_by_id (&fp, resolved_type)) == NULL)
+ return CTF_ERR; /* errno is set for us. */
}
+ while (LCTF_INFO_KIND (fp, tp->ctt_info) == CTF_K_SLICE);
+
return type;
}
diff --git a/libctf/testsuite/libctf-writable/slice-of-slice.c b/libctf/testsuite/libctf-writable/slice-of-slice.c
new file mode 100644
index 00000000000..9bacfe2d69c
--- /dev/null
+++ b/libctf/testsuite/libctf-writable/slice-of-slice.c
@@ -0,0 +1,77 @@
+/* Make sure that slices of slices are properly resolved. If they're not, both
+ population and lookup will fail. */
+
+#include <ctf-api.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main (void)
+{
+ ctf_dict_t *fp;
+ ctf_id_t base;
+ ctf_id_t slice;
+ ctf_id_t slice2;
+ ctf_encoding_t long_encoding = { CTF_INT_SIGNED, 0, sizeof (long) };
+ ctf_encoding_t foo;
+ int val;
+ int err;
+
+ if ((fp = ctf_create (&err)) == NULL)
+ {
+ fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
+ return 1;
+ }
+
+ if ((base = ctf_add_enum_encoded (fp, CTF_ADD_ROOT, "enom", &long_encoding))
+ == CTF_ERR)
+ goto err;
+
+ if (ctf_add_enumerator (fp, base, "a", 1) < 0 ||
+ ctf_add_enumerator (fp, base, "b", 0) < 0)
+ goto err;
+
+ foo.cte_format = 0;
+ foo.cte_bits = 4;
+ foo.cte_offset = 4;
+ if ((slice = ctf_add_slice (fp, CTF_ADD_ROOT, base, &foo)) == CTF_ERR)
+ goto err;
+
+ foo.cte_bits = 6;
+ foo.cte_offset = 2;
+ if ((slice2 = ctf_add_slice (fp, CTF_ADD_ROOT, slice, &foo)) == CTF_ERR)
+ goto err;
+
+ if (ctf_add_variable (fp, "foo", slice) < 0)
+ goto err;
+
+ if (ctf_enum_value (fp, slice, "a", &val) < 0)
+ {
+ fprintf (stderr, "Cannot look up value of sliced enum: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+ }
+ if (val != 1)
+ {
+ fprintf (stderr, "sliced enum value is wrong\n");
+ return 1;
+ }
+
+ if (ctf_enum_value (fp, slice2, "b", &val) < 0)
+ {
+ fprintf (stderr, "Cannot look up value of sliced sliced enum: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+ }
+ if (val != 0)
+ {
+ fprintf (stderr, "sliced sliced enum value is wrong\n");
+ return 1;
+ }
+
+ ctf_dict_close (fp);
+ fprintf (stderr, "All done.\n");
+ return 0;
+
+ err:
+ fprintf (stderr, "cannot populate: %s\n", ctf_errmsg (ctf_errno (fp)));
+ return 1;
+}
diff --git a/libctf/testsuite/libctf-writable/slice-of-slice.lk b/libctf/testsuite/libctf-writable/slice-of-slice.lk
new file mode 100644
index 00000000000..b944f73d013
--- /dev/null
+++ b/libctf/testsuite/libctf-writable/slice-of-slice.lk
@@ -0,0 +1 @@
+All done.
--
2.48.1.283.g18c60a128c
More information about the Binutils
mailing list