Files
binutils-gdb/libctf/testsuite/libctf-writable/id-assignment.c
Nick Alcock b5d3790c66 libctf: consecutive ctf_id_t assignment
This change modifies type ID assignment in CTF so that it works like BTF:
rather than flipping the high bit on for types in child dicts, types ascend
directly from IDs in the parent to IDs in the child, without interruption
(so type 0x4 in the parent is immediately followed by 0x5 in all children).

Doing this while retaining useful semantics for modification of parents is
challenging.  By definition, child type IDs are not known until the parent
is written out, but we don't want to find ourselves constrained to adding
types to the parent in one go, followed by all child types: that would make
the deduplicator a nightmare and would frankly make the entire ctf_add*()
interface next to useless: all existing clients that add types at all
add types to both parents and children without regard for ordering, and
breaking that would probably necessitate redesigning all of them.

So we have to be a litle cleverer.

We approach this the same way as we approach strings in the recent refs
rework: if a parent has children attached (or has ever had them attached
since it was created or last read in), any new types created in the parent
are assigned provisional IDs starting at the very top of the type space and
working down.  (Their indexes in the internal libctf arrays remain
unchanged, so we don't suddenly need multigigabyte indexes!).  At writeout
(preserialization) time, we traverse the type table (and all other table
containing type IDs) and assign refs to every type ID in exactly the same
way we assign refs to every string offset (just a different set of refs --
we don't want to update type IDs with string offset values!).

For a parent dict with children, these refs are real entities in memory:
pointers to the memory locations where type IDs are stored, tracked in the
DTD of each type.  As we traverse the type table, we assign real IDs to each
type (by simple incrementation), storing those IDs in a new dtd_final_type
field in the DTD for each type.  Once the type table and all other tables
containing type IDs are fully traversed, we update all the refs and
overwrite the IDs currently residing in each with the final IDs for each
type.

That fixes up IDs in the parent dict itself (including forward references in
structs and the like: that's why the ref updates only happen at the end);
but what about child dicts' references, both to parent types and to their
own?  We add armouring to enforce that parent dicts are always serialized
before their children (which ctf-link.c already does, because it's a
precondition for strtab deduplication), and then arrange that when a ref is
added to a type whose ID has been assigned (has a dtd_final_type), we just
immediately do an update rather than storing a ref for later updating.
Since the parent is already serialized, all parent type IDs have a
dtd_final_type by this point, and all parent IDs in the children are
properly updated. The child types can now be renumbered now we now the
number of types in the parent, and their refs updated identically to what
was just done with the parent.

One wrinkle: before the child refs are updated, while we are working over
the child's type section, the type IDs in the child start from 1 (or
something like that), which might seem to overlap the parent IDs.  But this
is not the case: when you serialize the parent, the IDs written out to disk
are changed, but the only change to the representation in memory is that we
remember a dtd_final_type for each type (and use it to update all the child
type refs): its ID in memory is the same as it always was, a nonoverlapping
provisional ID higher than any other valid ID.  We enforce all of this by
asserting that when you add a ref to a type, the memory location that is
modified must be in the buffer being serialized: the code will not let you
accidentally modify the actual DTDs in memory.

We track the number of types in the parent in a new CTFv4 (not BTF) header
field (the dumper is updated): we will also use this to open CTFv3 child
dicts without change by simply declaring for them that the parent dict has
2^31 types in it (or 2^15, for v2 and below): the IDs in the children then
naturally come out right with no other changes needed.  (Right now, opening
CTFv3 child dicts requires extra compatibility code that has not been
written, but that code will no longer need to worry about type ID
differences.)

Various things are newly forbidden:

 - you cannot ctf_import() a child into a parent if you already ctf_add()ed
   types to the child, because all its IDs would change (and since you
   already cannot ctf_add() types to a child that hasn't had its parent
   imported, this in practice means only that ctf_create() must be followed
   immediately by a ctf_import() if this is a new child, which all sane
   clients were doing anyway).

 - You cannot import a child into a parent which has the wrong number of
   (non-provisional) types, again because all its IDs would be wrong:
   because parents only add types in the provisional space if children are
   attached to it, this would break the not unknown case of opening an
   archive, adding types to the parent, and only then importing children
   into it, so we add a special case: archive members which are not children
   in an archive with more than one member always pretend to have at least
   one child, so type additions in them are always provisional even before
   you ctf_import anything. In practice, this does exactly what we want,
   since all archives so far are created by the linker and have one parent
   and N children of that parent.

Because this introduces huge gaps between index and type ID for provisional
types, some extra assertions are added to ensure that the internal
ctf_type_to_index() is only ever called on types in the current dict (never
a parent dict): before now, this was just taken on trust, and it was often
wrong (which at best led to wrong results, as wrong array indexes were used,
and at worst to a buffer overflow). When hash debugging is on (suggesting
that the user doesn't mind expensive checks), every ctf_type_to_index()
triggers a ctf_index_to_type() to make sure that the operations are proper
inverses.

Lots and lots of tests are added to verify that assignment works and that
updating of every type kind works fine -- existing tests suffice for
type IDs in the variable and symtypetab sections.

The ld-ctf tests get a bunch of largely display-based updates: various
tests refer to 0x8... type IDs, which no longer exist, and because the
IDs are shorter all the spacing and alignment has changed.
2025-03-16 15:25:27 +00:00

553 lines
15 KiB
C

/* Test parent / child ID assignment. */
#include <ctf-api.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
test (int empty_parent, int unserialized_parent)
{
ctf_dict_t *parent;
ctf_dict_t *child;
ctf_id_t pint = 0, pprovint = 0, pptr = 0, parray = 0, pfunction = 0;
ctf_id_t ctype, ctype2, cslice, ctypedef, cfunction, cself;
ctf_id_t foo;
ctf_encoding_t encoding = { CTF_INT_SIGNED, 0, (sizeof (char) * 8) - 1 };
ctf_encoding_t slice_encoding = { CTF_INT_SIGNED, 1, (sizeof (char) * 8) - 1 };
ctf_encoding_t out;
unsigned char *pbuf = NULL, *cbuf = NULL, *pbuf2 = NULL, *cbuf2 = NULL;
size_t psize, csize;
int err;
ctf_id_t first_child_type = 1;
ctf_membinfo_t memb;
ctf_arinfo_t ar;
ctf_funcinfo_t func;
ctf_funcinfo_t pfunc, cfunc;
ctf_id_t args[2], pargs[2], cargs[2];
printf ("Testing with %s, %s parent\n", empty_parent ? "empty" : "nonempty",
unserialized_parent ? "unserialized" : "serialized");
if ((parent = ctf_create (&err)) == NULL)
goto create_err;
if ((child = ctf_create (&err)) == NULL)
goto create_err;
/* Try some tests with a parent that already has some types in it (thus, a
nonempty stypes range). */
if (!empty_parent)
{
if ((pint = ctf_add_integer (parent, CTF_ADD_ROOT, "int", &encoding)) == CTF_ERR)
goto parent_add_err;
first_child_type++;
}
if (!unserialized_parent)
{
if ((pbuf = ctf_write_mem (parent, &psize, -1)) == NULL)
goto parent_write_err;
ctf_dict_close (parent);
if ((parent = ctf_simple_open ((char *) pbuf, psize, NULL, 0, 0, NULL, 0, &err)) == NULL)
goto parent_open_err;
if (!empty_parent)
{
/* Look up int again: its ID will have changed. */
if ((pint = ctf_lookup_by_name (parent, "int")) == CTF_ERR)
{
fprintf (stderr, "Cannot look up int in parent: %s\n", ctf_errmsg (ctf_errno (parent)));
exit (1);
}
}
}
if (ctf_import (child, parent) < 0)
goto import_err;
/* Add some types that should end up with provisional IDs and be reassigned on
writeout, with all references to them in all dicts following along. */
if (!empty_parent)
{
if ((pprovint = ctf_add_integer (parent, CTF_ADD_ROOT, "provint", &encoding)) == CTF_ERR)
goto parent_add_err;
first_child_type++;
if ((pptr = ctf_add_pointer (parent, CTF_ADD_ROOT, pint)) == CTF_ERR)
goto parent_add_err;
first_child_type++;
ar.ctr_contents = pint;
ar.ctr_index = pint;
ar.ctr_nelems = 666;
if ((parray = ctf_add_array (parent, CTF_ADD_ROOT, &ar)) == CTF_ERR)
goto parent_add_err;
first_child_type++;
func.ctc_argc = 2;
func.ctc_flags = 0;
func.ctc_return = pprovint;
args[0] = pptr;
args[1] = parray;
if ((pfunction = ctf_add_function (parent, CTF_ADD_ROOT, &func, args)) == CTF_ERR)
goto parent_add_err;
first_child_type++;
}
if ((ctype = ctf_add_enum (child, CTF_ADD_ROOT, "wombat")) == CTF_ERR)
goto child_add_err;
if ((ctype2 = ctf_add_struct (child, CTF_ADD_ROOT, "foo")) == CTF_ERR)
goto child_add_err;
if (ctf_add_member (child, ctype2, "wombat_member", ctype) < 0)
goto child_add_memb_err;
if (!empty_parent)
{
/* pint is still valid: nonprovisional type. */
if (ctf_add_member (child, ctype2, "a", pint) < 0)
goto child_add_memb_err;
/* (pptr is provisional.) */
if (ctf_add_member (child, ctype2, "b", pptr) < 0)
goto child_add_memb_err;
if ((cself = ctf_add_pointer (child, CTF_ADD_ROOT, ctype2)) == CTF_ERR)
goto child_add_err;
if (ctf_add_member (child, ctype2, "self", cself) < 0)
goto child_add_memb_err;
/* Make sure types are distinct. */
if (pint == pptr || pint == ctype || pint == ctype2 ||
pptr == ctype || pptr == ctype2 || ctype == ctype2)
goto overlapping_err;
if (pint > pptr || ctype > pptr || ctype2 > pptr)
goto provisional_too_low_err;
/* Add an instance of every other serialized type-referencing type,
referencing a type provisional in the parent. */
if (ctf_add_typedef (child, CTF_ADD_ROOT, "td", parray) == CTF_ERR)
goto child_add_err;
if ((cslice = ctf_add_slice (child, CTF_ADD_ROOT, pprovint, &slice_encoding)) == CTF_ERR)
goto child_add_err;
if (ctf_add_member (child, ctype2, "c", cslice) < 0)
goto child_add_memb_err;
if (ctf_add_member (child, ctype2, "pfunc", pfunction) < 0)
goto child_add_memb_err;
func.ctc_argc = 2;
func.ctc_flags = 0;
func.ctc_return = pprovint;
args[0] = pptr;
args[1] = parray;
if ((cfunction = ctf_add_function (parent, CTF_ADD_ROOT, &func, args)) == CTF_ERR)
goto child_add_err;
first_child_type++;
if (ctf_add_member (child, ctype2, "cfunc", pfunction) < 0)
goto child_add_memb_err;
}
/* Make sure we can't write out the child before the parent. */
if (!empty_parent)
{
if ((cbuf = ctf_write_mem (child, &csize, -1)) != NULL)
{
fprintf (stderr, "writing child before parent works unexpectedly\n");
exit (1);
}
if (ctf_errno (child) != ECTF_NOTSERIALIZED)
{
fprintf (stderr, "writing child before parent: unexpected error %s\n",
ctf_errmsg (ctf_errno (child)));
exit (1);
}
}
/* Write out the parent, then the child: read both back in, reimport them,
do some lookups, make sure they work. Make sure we can't write the parent
out if it was already serialized, unless it was empty when that happened */
if (!empty_parent && !unserialized_parent)
{
if ((pbuf2 = ctf_write_mem (parent, &psize, -1)) != NULL)
{
fprintf (stderr, "Writing out modified already-serialized parent succeeded unexpectedly\n");
exit (1);
}
/* Nothing more to test in this case. */
ctf_dict_close (child);
ctf_dict_close (parent);
free (pbuf);
free (pbuf2);
return 0;
}
if ((pbuf2 = ctf_write_mem (parent, &psize, -1)) == NULL)
goto parent_write_err;
if ((cbuf2 = ctf_write_mem (child, &csize, -1)) == NULL)
goto child_write_err;
ctf_dict_close (child);
ctf_dict_close (parent);
free (pbuf);
free (cbuf);
if ((parent = ctf_simple_open ((char *) pbuf2, psize, NULL, 0, 0, NULL, 0, &err)) == NULL)
goto parent_open_err;
if ((child = ctf_simple_open ((char *) cbuf2, csize, NULL, 0, 0, NULL, 0, &err)) == NULL)
goto child_open_err;
if (ctf_import (child, parent) < 0)
goto import_err;
if (!empty_parent)
{
ctf_id_t foo2;
if ((foo = ctf_lookup_by_name (parent, "int")) == CTF_ERR)
{
fprintf (stderr, "Cannot look up int in parent: %s\n", ctf_errmsg (ctf_errno (parent)));
exit (1);
}
if ((foo2 = ctf_lookup_by_name (child, "int")) == CTF_ERR)
{
fprintf (stderr, "Cannot look up int in child: %s\n", ctf_errmsg (ctf_errno (parent)));
exit (1);
}
if (foo != foo2)
{
fprintf (stderr, "int in parent and child have different IDs: %lx versus %lx\n", foo, foo2);
exit (1);
}
/* Verify that ctf_type_pointer still works: it saw changes as part of
the CTFv4 type ID rework. In particular it works on parent types now
too. */
if ((foo2 = ctf_type_pointer (parent, foo)) == CTF_ERR)
{
fprintf (stderr, "pointer lookup in parent failed: %s\n", ctf_errmsg (ctf_errno (parent)));
exit (1);
}
if (ctf_type_kind (parent, foo2) != CTF_K_POINTER)
{
fprintf (stderr, "pointer lookup in parent yields non-pointer: %i\n", ctf_type_kind (parent, foo2));
exit (1);
}
if ((foo2 = ctf_type_pointer (child, foo)) == CTF_ERR)
{
fprintf (stderr, "pointer lookup in child failed: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
}
if (ctf_type_kind (child, foo2) != CTF_K_POINTER)
{
fprintf (stderr, "pointer lookup in child yields non-pointer: %i\n", ctf_type_kind (child, foo2));
exit (1);
}
}
if ((ctype = ctf_lookup_by_name (child, "enum wombat")) == CTF_ERR)
{
fprintf (stderr, "Cannot look up enum wombat in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
}
/* Check consecutiveness. */
if (ctype != first_child_type)
{
fprintf (stderr, "expected first child type to be ID %lx but is %lx\n", first_child_type, ctype);
exit (1);
}
if ((ctype2 = ctf_lookup_by_name (child, "struct foo")) == CTF_ERR)
{
fprintf (stderr, "Cannot look up struct foo in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
}
/* Check consecutiveness. */
if (ctype2 != ctype + 1)
{
fprintf (stderr, "expected second child type to be ID %lx but is %lx\n", ctype + 1, ctype2);
exit (1);
}
if (!empty_parent)
{
if ((ctypedef = ctf_lookup_by_name (child, "td")) == CTF_ERR)
goto typedef_td_err;
if ((parray = ctf_type_reference (child, ctypedef)) == CTF_ERR)
goto typedef_err;
if (ctf_array_info (child, parray, &ar) < 0)
goto array_err;
char *name;
if ((name = ctf_type_aname (child, ar.ctr_contents)) == NULL)
goto type_name_err;
if (strcmp (name, "int") != 0)
{
fprintf (stderr, "expected array member to be int, but was %s\n", name);
exit (1);
}
free (name);
if (ar.ctr_contents != ar.ctr_index)
{
fprintf (stderr, "array contents and index are not the same type: %lx versus %lx",
ar.ctr_contents, ar.ctr_index);
exit (1);
}
}
/* Check membership links. */
if (ctf_member_info (child, ctype2, "wombat_member", &memb) < 0)
goto memb_err;
if (memb.ctm_type != ctype)
{
fprintf (stderr, "child enum member lookup yielded %lx, not %lx\n", memb.ctm_type, ctype);
exit (1);
}
if (!empty_parent)
{
if (ctf_member_info (child, ctype2, "a", &memb) < 0)
goto memb_err;
if (ctf_type_kind (child, memb.ctm_type) != CTF_K_INTEGER)
{
fprintf (stderr, "parent member integer lookup yielded %lx, not %x\n", memb.ctm_type, CTF_K_INTEGER);
exit (1);
}
if (ctf_member_info (child, ctype2, "b", &memb) < 0)
goto memb_err;
if (ctf_type_kind (child, memb.ctm_type) != CTF_K_POINTER)
goto memb_ptr_err;
if ((foo = ctf_type_reference (child, memb.ctm_type)) == CTF_ERR)
goto memb_err;
if ((ctf_type_kind (child, foo)) != CTF_K_INTEGER)
{
fprintf (stderr, "parent member pointer final lookup yielded kind %x, not %x\n", ctf_type_kind (child, foo), CTF_K_INTEGER);
exit (1);
}
if (ctf_member_info (child, ctype2, "c", &memb) < 0)
goto memb_err;
if (ctf_type_encoding (child, memb.ctm_type, &out) < 0)
goto encoding_err;
if (memcmp (&out, &slice_encoding, sizeof (out)) != 0)
{
fprintf (stderr, "slice encoding differs\n");
exit (1);
}
if (ctf_type_kind (child, memb.ctm_type) != CTF_K_INTEGER)
{
fprintf (stderr, "parent member slice final lookup yielded kind %x, not %x\n", ctf_type_kind (child, memb.ctm_type), CTF_K_INTEGER);
exit (1);
}
if (ctf_member_info (child, ctype2, "pfunc", &memb) < 0)
goto memb_err;
if (ctf_type_kind (child, memb.ctm_type) != CTF_K_FUNCTION)
goto func_err;
pfunction = memb.ctm_type;
if (ctf_member_info (child, ctype2, "cfunc", &memb) < 0)
goto memb_err;
if (ctf_type_kind (child, memb.ctm_type) != CTF_K_FUNCTION)
goto func_err;
cfunction = memb.ctm_type;
if (ctf_func_type_info (child, pfunction, &pfunc) < 0 ||
ctf_func_type_info (child, cfunction, &cfunc) < 0)
{
fprintf (stderr, "func info lookup failed: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
}
if (memcmp (&pfunc, &cfunc, sizeof (pfunc)) != 0)
{
fprintf (stderr, "parent and child funcs differ\n");
exit (1);
}
if (ctf_type_kind (child, pfunc.ctc_return) != CTF_K_INTEGER)
{
fprintf (stderr, "func return type lookup yielded kind %x, not %x\n", ctf_type_kind (child, pfunc.ctc_return), CTF_K_INTEGER);
exit (1);
}
/* This isn't a type ID, so we're not really expecting problems here, but if
there are problems, rather an error message than a buffer overrun. */
if (pfunc.ctc_argc != 2)
{
fprintf (stderr, "func has %i args, not 2\n", pfunc.ctc_argc);
exit (1);
}
if (ctf_func_type_args (child, pfunction, pfunc.ctc_argc, pargs) < 0 ||
ctf_func_type_args (child, cfunction, cfunc.ctc_argc, cargs) < 0)
{
fprintf (stderr, "func arg lookup failed: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
}
if (memcmp (pargs, cargs, sizeof (pargs)) != 0)
{
fprintf (stderr, "parent and child func args differ\n");
exit (1);
}
if (ctf_type_kind (child, pargs[0]) != CTF_K_POINTER ||
ctf_type_kind (child, pargs[1]) != CTF_K_ARRAY)
{
fprintf (stderr, "func args lookup not as expected\n");
exit (1);
}
if (ctf_member_info (child, ctype2, "self", &memb) < 0)
goto memb_err;
if (ctf_type_kind (child, memb.ctm_type) != CTF_K_POINTER)
goto memb_ptr_err;
if (ctf_type_reference (child, memb.ctm_type) != ctype2)
{
fprintf (stderr, "structure self-ref yields type %lx, not %lx as expected\n",
ctf_type_reference (child, memb.ctm_type), ctype2);
exit (1);
}
}
ctf_dict_close (child);
ctf_dict_close (parent);
free (cbuf2);
free (pbuf2);
return 0;
create_err:
fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
exit (1);
parent_write_err:
fprintf (stderr, "Cannot serialize parent: %s\n", ctf_errmsg (ctf_errno (parent)));
exit (1);
child_write_err:
fprintf (stderr, "Cannot serialize child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
parent_open_err:
fprintf (stderr, "Cannot open parent: %s\n", ctf_errmsg (err));
exit (1);
child_open_err:
fprintf (stderr, "Cannot open chile: %s\n", ctf_errmsg (err));
exit (1);
import_err:
fprintf (stderr, "Cannot import: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
parent_add_err:
fprintf (stderr, "Cannot add parent types: %s\n", ctf_errmsg (ctf_errno (parent)));
exit (1);
child_add_err:
fprintf (stderr, "Cannot add child types: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
child_add_memb_err:
fprintf (stderr, "Cannot add child members: %s (%i)\n", ctf_errmsg (ctf_errno (child)), ctf_errno (child));
exit (1);
overlapping_err:
fprintf (stderr, "type IDs overlap\n");
exit (1);
provisional_too_low_err:
fprintf (stderr, "provisional ID %lx is too low\n", pptr);
exit (1);
memb_err:
fprintf (stderr, "cannot look up members: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
memb_ptr_err:
fprintf (stderr, "parent member pointer lookup yielded %lx, not %x\n", memb.ctm_type, CTF_K_POINTER);
exit (1);
typedef_td_err:
fprintf (stderr, "Cannot look up typedef td in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
typedef_err:
fprintf (stderr, "Cannot look up typedef array ref in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
array_err:
fprintf (stderr, "Cannot look up array in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
type_name_err:
fprintf (stderr, "Cannot look up type name in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
encoding_err:
fprintf (stderr, "cannot get type encoding in child: %s\n", ctf_errmsg (ctf_errno (child)));
exit (1);
func_err:
fprintf (stderr, "parent member function final lookup yielded kind %x, not %x\n", ctf_type_kind (child, memb.ctm_type), CTF_K_FUNCTION);
exit (1);
}
int main (void)
{
test (1, 1);
test (1, 0);
test (0, 1);
test (0, 0);
printf ("All done.\n");
}