Compare commits

...

4 Commits

Author SHA1 Message Date
Bruce McCulloch
11bacd955a libctf: doc: add __float128 and SIMD vector classification to spec.
This patch adds two additional distinct types (__float128 and the SIMD
vector type generated from the vector_size attribute) to the umbrella of
two existing types (long double and array, respectively). These types
were previously invalid, producing CTF_K_UNKNOWN in the case of
__float128 or a float in the case of the SIMD vector. This patch will
cleanly allow these types to be represented more accurately without
breaking back-compat.

Signed-off-by: Bruce McCulloch <bruce.mcculloch@oracle.com>
Reviewed-by: Nick Alcock <nick.alcock@oracle.com>
2025-06-27 19:13:16 +01:00
Nick Alcock
ee42b0ad05 libctf: add root-visibility-addition test
libctf/
	* testsuite/libctf-writable/ctf-nonroot-addition.*: New test.
2025-06-27 17:48:42 +01:00
Nick Alcock
70edb89629 libctf: create: check the right root-visible flag when adding enumerands
The root-visible flag we're dealing with here is directly out of the dict,
not a flag passed in to the API, so it does not have the values CTF_ADD_ROOT
or CTF_ADD_NONROOT: instead it's simply zero for non-root-visible, nonzero
otherwise.  Fix the test.

libctf/
	* ctf-create.c (ctf_add_enumerator): Fix root-visibility test.
2025-06-27 14:36:07 +01:00
Nick Alcock
ed6eed44b8 libctf: create: addition of non-root types should not return root types
If you add a non-root type to a dict, you should always get a new, unique
type ID back, even if a root-visible type with the same name already exists.
Unfortunately, if the root-visible type is a forward, and you're adding a
non-root-visible struct, union, or enum, the machinery to detect forwards
and promote them to the concrete type fires in this case and returns the
root-visible type!  If this is an enum being inserted hidden because its
enumerands conflict with some other enum, this will lead to failure later
on: in any case, it's seriously counterintuitive to add a non-root- visible
type and get a root-visible one instead.

Fix this by checking the root-visible flag properly and only checking for
forwards if this type is root-visible.  (This may lead to a certain degree
of proliferation of non-root-visible forwards: we can add a cleanup pass for
those later if needed.)

libctf/
	* ctf-create.c (ctf_add_struct_sized): Check the root-visible flag when
	doing forward promotion.
	(ctf_add_union_sized): Likewise.
	(ctf_add_enum): Likewise.
2025-06-27 14:36:07 +01:00
4 changed files with 52 additions and 6 deletions

View File

@@ -788,7 +788,7 @@ ctf_add_struct_sized (ctf_dict_t *fp, uint32_t flag, const char *name,
size_t initial_vlen = sizeof (ctf_lmember_t) * INITIAL_VLEN;
/* Promote root-visible forwards to structs. */
if (name != NULL)
if (name != NULL && flag == CTF_ADD_ROOT)
type = ctf_lookup_by_rawname (fp, CTF_K_STRUCT, name);
/* Prohibit promotion if this type was ctf_open()ed. */
@@ -832,7 +832,7 @@ ctf_add_union_sized (ctf_dict_t *fp, uint32_t flag, const char *name,
size_t initial_vlen = sizeof (ctf_lmember_t) * INITIAL_VLEN;
/* Promote root-visible forwards to unions. */
if (name != NULL)
if (name != NULL && flag == CTF_ADD_ROOT)
type = ctf_lookup_by_rawname (fp, CTF_K_UNION, name);
/* Prohibit promotion if this type was ctf_open()ed. */
@@ -875,7 +875,7 @@ ctf_add_enum (ctf_dict_t *fp, uint32_t flag, const char *name)
size_t initial_vlen = sizeof (ctf_enum_t) * INITIAL_VLEN;
/* Promote root-visible forwards to enums. */
if (name != NULL)
if (name != NULL && flag == CTF_ADD_ROOT)
type = ctf_lookup_by_rawname (fp, CTF_K_ENUM, name);
/* Prohibit promotion if this type was ctf_open()ed. */
@@ -1073,7 +1073,7 @@ ctf_add_enumerator (ctf_dict_t *fp, ctf_id_t enid, const char *name,
/* Enumeration constant names are only added, and only checked for duplicates,
if the enum they are part of is a root-visible type. */
if (root == CTF_ADD_ROOT && ctf_dynhash_lookup (fp->ctf_names, name))
if (root && ctf_dynhash_lookup (fp->ctf_names, name))
{
if (fp->ctf_flags & LCTF_STRICT_NO_DUP_ENUMERATORS)
return (ctf_set_errno (ofp, ECTF_DUPLICATE));

View File

@@ -829,7 +829,7 @@ of kind @code{CTF_K_UNKNOWN}.
@item 4
@tab @code{CTF_K_ARRAY}
@tab An array. @xref{Arrays}.
@tab An array or SIMD vector. @xref{Arrays}.
@item 5
@tab @code{CTF_K_FUNCTION}
@@ -1064,7 +1064,7 @@ unused and will become used in future.
@tindex CTF_FP_LDCPLX
@item 6
@tab @code{CTF_FP_LDOUBLE}
@tab This is a @code{long double}.
@tab This is a @code{long double}, or quad-precision IEEE 754-2008 @code{__float128}.
@tindex CTF_FP_LDOUBLE
@item 7
@tab @code{CTF_FP_INTRVL}
@@ -1232,6 +1232,13 @@ Arrays are encoded as types of kind @code{CTF_K_ARRAY} in a @code{ctf_stype_t}.
Both size and kind for arrays are zero. The variable-length data is a
@code{ctf_array_t}: @code{vlen} in the info word should be disregarded and is
always zero.
@c In CTFv4 and BTF, the @code{kind_flag} member of @{ctf_array_t} is not set.
SIMD vectors are also encoded as types of kind @code{CTF_K_ARRAY} in a
@code{ctf_stype_t}. Both size and kind for arrays are zero. The
variable-length data is a @code{ctf_array_t}: @code{vlen} in the info word
should be disregarded and is always zero.
@c In CTFv4 and BTF, the @code{kind_flag} member of @{ctf_array_t} is set.
@verbatim
typedef struct ctf_array

View File

@@ -0,0 +1,38 @@
/* Make sure adding a non-root-visible type after adding a root-visible forward
adds a new type rather than promoting and returning the existing one. */
#include <ctf-api.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main (int argc, char *argv[])
{
ctf_dict_t *fp;
ctf_id_t root, nonroot;
int err;
if ((fp = ctf_create (&err)) == NULL)
{
fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
return 1;
}
if ((root = ctf_add_forward (fp, CTF_ADD_ROOT, "foo", CTF_K_ENUM)) == CTF_ERR)
goto add_err;
if ((nonroot = ctf_add_enum (fp, CTF_ADD_NONROOT, "foo")) == CTF_ERR)
goto add_err;
if (nonroot == root)
fprintf (stderr, "Non-root addition should not promote root-visible forwards\n");
else
printf ("All done.\n");
ctf_dict_close (fp);
return 0;
add_err:
fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (fp)));
}

View File

@@ -0,0 +1 @@
All done.