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.
This commit is contained in:
Nick Alcock
2025-02-16 19:55:11 +00:00
parent 274cc1f13d
commit b5d3790c66
26 changed files with 1207 additions and 164 deletions

View File

@@ -28,6 +28,16 @@
#include <ctf-ref.h>
/* Functions in this file are roughly divided into two types: sizing functions,
which work out the size of various structures in the final serialized
representation, and emission functions that actually emit data into them.
When the sizing functions are called, the buffer into which the output will
be serialized has not yet been created: so no functions which create
references into that buffer (notably, ctf_*_add_ref) should be called.
This requirement is to some degree enforced by ctf_assert calls. */
/* Symtypetab sections. */
/* Symtypetab emission flags. */
@@ -62,6 +72,54 @@ typedef struct emit_symtypetab_state
size_t maxfunc;
} emit_symtypetab_state_t;
/* Emit a ref to a type in this dict. As with string refs, this ref can be
updated later on to change the type ID recorded in this location. The ref
may not be emitted if the value is already known and cannot change.
All refs must point within the ctf_serializing_buf. */
static int
ctf_type_add_ref (ctf_dict_t *fp, uint32_t *ref)
{
ctf_dtdef_t *dtd;
/* Type in the static portion: cannot change, value already correct. */
if (*ref <= fp->ctf_stypes)
return 0;
dtd = ctf_dtd_lookup (fp, *ref);
if (!ctf_assert (fp, dtd))
return 0;
if (!ctf_assert (fp, fp->ctf_serializing_buf != NULL
&& (unsigned char *) ref > fp->ctf_serializing_buf
&& (unsigned char *) ref < fp->ctf_serializing_buf + fp->ctf_serializing_buf_size))
return -1;
/* Simple case: final ID different from what is recorded, but already known.
Just set it. */
if (dtd->dtd_final_type)
*ref = dtd->dtd_final_type;
/* Otherwise, create a ref to it so we can set it later. */
else if (!ctf_create_ref (fp, &dtd->dtd_refs, ref))
return (ctf_set_errno (fp, ENOMEM));
return 0;
}
/* Purge all refs to this dict's dynamic types (all refs added by
ctf_type_add_ref while serializing this dict). */
static void
ctf_type_purge_refs (ctf_dict_t *fp)
{
ctf_dtdef_t *dtd;
for (dtd = ctf_list_next (&fp->ctf_dtdefs); dtd != NULL;
dtd = ctf_list_next (dtd))
ctf_purge_ref_list (fp, &dtd->dtd_refs);
}
/* Determine if a symbol is "skippable" and should never appear in the
symtypetab sections. */
@@ -88,7 +146,10 @@ ctf_symtab_skippable (ctf_link_sym_t *sym)
will always be set in the flags.
Also figure out if any symbols need to be moved to the variable section, and
add them (if not already present). */
add them (if not already present).
This is a sizing function, called before the output buffer is
constructed. Do not add any refs in this function! */
_libctf_nonnull_ ((1,3,4,5,6,7,8))
static int
@@ -253,7 +314,10 @@ symtypetab_density (ctf_dict_t *fp, ctf_dict_t *symfp, ctf_dynhash_t *symhash,
elements in it: unindexed output would terminate at symbol OUTMAX and is in
any case no larger than SIZE bytes. Some index elements are expected to be
skipped: see symtypetab_density. The linker-reported set of symbols (if any)
is found in SYMFP. */
is found in SYMFP.
Note down type ID refs as we go. */
static int
emit_symtypetab (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
ctf_link_sym_t **idx, const char **nameidx, uint32_t nidx,
@@ -337,7 +401,9 @@ emit_symtypetab (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
if (!ctf_assert (fp, (((char *) dpp) - (char *) dp) < size))
return -1; /* errno is set for us. */
*dpp++ = (ctf_id_t) (uintptr_t) type;
*dpp = (ctf_id_t) (uintptr_t) type;
if (ctf_type_add_ref (fp, dpp++) < 0)
return -1; /* errno is set for us. */
/* When emitting unindexed output, all later symbols are pads: stop
early. */
@@ -431,9 +497,10 @@ emit_symtypetab_index (ctf_dict_t *fp, ctf_dict_t *symfp, uint32_t *dp,
return 0;
}
/* Delete symbols that have been assigned names from the variable section. Must
be called from within ctf_serialize, because that is the only place you can
safely delete variables without messing up ctf_rollback. */
/* Delete variables with the same name as symbols that have been reported by
the linker from the variable section. Must be called from within
ctf_serialize, because that is the only place you can safely delete
variables without messing up ctf_rollback. */
static int
symtypetab_delete_nonstatics (ctf_dict_t *fp, ctf_dict_t *symfp)
@@ -458,7 +525,11 @@ symtypetab_delete_nonstatics (ctf_dict_t *fp, ctf_dict_t *symfp)
}
/* Figure out the sizes of the symtypetab sections, their indexed state,
etc. */
etc.
This is a sizing function, called before the output buffer is
constructed. Do not add any refs in this function! */
static int
ctf_symtypetab_sect_sizes (ctf_dict_t *fp, emit_symtypetab_state_t *s,
ctf_header_t *hdr, size_t *objt_size,
@@ -573,6 +644,8 @@ ctf_symtypetab_sect_sizes (ctf_dict_t *fp, emit_symtypetab_state_t *s,
return 0;
}
/* Emit the symtypetab sections. */
static int
ctf_emit_symtypetab_sects (ctf_dict_t *fp, emit_symtypetab_state_t *s,
unsigned char **tptr, size_t objt_size,
@@ -721,7 +794,10 @@ symerr:
/* Type section. */
/* Iterate through the static types and the dynamic type definition list and
compute the size of the CTF type section. */
compute the size of the CTF type section.
This is a sizing function, called before the output buffer is
constructed. Do not add any refs in this function! */
static size_t
ctf_type_sect_size (ctf_dict_t *fp)
@@ -784,17 +860,23 @@ ctf_type_sect_size (ctf_dict_t *fp)
}
/* Take a final lap through the dynamic type definition list and copy the
appropriate type records to the output buffer, noting down the strings as
we go. */
appropriate type records to the output buffer, noting down the strings
and type IDs as we go. */
static void
static int
ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
{
unsigned char *t = *tptr;
ctf_dtdef_t *dtd;
ctf_id_t id;
if (!(fp->ctf_flags & LCTF_CHILD))
id = fp->ctf_stypes + 1;
else
id = fp->ctf_header->cth_parent_typemax + 1;
for (dtd = ctf_list_next (&fp->ctf_dtdefs);
dtd != NULL; dtd = ctf_list_next (dtd))
dtd != NULL; dtd = ctf_list_next (dtd), id++)
{
uint32_t kind = LCTF_INFO_KIND (fp, dtd->dtd_data.ctt_info);
uint32_t vlen = LCTF_INFO_VLEN (fp, dtd->dtd_data.ctt_info);
@@ -804,6 +886,13 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
const char *name;
size_t i;
/* Make sure the ID hasn't changed, if already assigned by a previous
serialization. */
if (dtd->dtd_final_type != 0
&& !ctf_assert (fp, dtd->dtd_final_type == id))
return -1; /* errno is set for us. */
/* Shrink ctf_type_t-using types from a ctf_type_t to a ctf_stype_t
if possible. */
@@ -836,22 +925,65 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
t += sizeof (uint32_t);
break;
case CTF_K_POINTER:
case CTF_K_VOLATILE:
case CTF_K_CONST:
case CTF_K_RESTRICT:
case CTF_K_TYPEDEF:
if (ctf_type_add_ref (fp, &copied->ctt_type) < 0)
return -1; /* errno is set for us. */
break;
case CTF_K_SLICE:
memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_slice));
{
ctf_slice_t *slice = (ctf_slice_t *) t;
memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_slice));
if (ctf_type_add_ref (fp, &slice->cts_type) < 0)
return -1; /* errno is set for us. */
}
t += sizeof (struct ctf_slice);
break;
case CTF_K_ARRAY:
memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_array));
{
ctf_array_t *array = (ctf_array_t *) t;
memcpy (t, dtd->dtd_vlen, sizeof (struct ctf_array));
if (ctf_type_add_ref (fp, &array->cta_contents) < 0)
return -1; /* errno is set for us. */
if (ctf_type_add_ref (fp, &array->cta_index) < 0)
return -1; /* errno is set for us. */
}
t += sizeof (struct ctf_array);
break;
case CTF_K_FUNCTION:
/* Functions with no args also have no vlen. */
if (dtd->dtd_vlen)
memcpy (t, dtd->dtd_vlen, sizeof (uint32_t) * (vlen + (vlen & 1)));
{
uint32_t *args = (uint32_t *) t;
/* Functions with no args also have no vlen. */
if (dtd->dtd_vlen)
memcpy (t, dtd->dtd_vlen, sizeof (uint32_t) * (vlen + (vlen & 1)));
if (ctf_type_add_ref (fp, &copied->ctt_type) < 0)
return -1; /* errno is set for us. */
for (i = 0; i < vlen; i++)
{
if (ctf_type_add_ref (fp, &args[i]) < 0)
return -1; /* errno is set for us. */
}
t += sizeof (uint32_t) * (vlen + (vlen & 1));
break;
}
/* These need to be copied across element by element, depending on
their ctt_size. */
@@ -873,12 +1005,18 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
t_vlen[i].ctm_name = dtd_vlen[i].ctlm_name;
t_vlen[i].ctm_type = dtd_vlen[i].ctlm_type;
t_vlen[i].ctm_offset = CTF_LMEM_OFFSET (&dtd_vlen[i]);
ctf_str_add_ref (fp, name, &t_vlen[i].ctm_name);
if (ctf_type_add_ref (fp, &t_vlen[i].ctm_type) < 0)
return -1; /* errno is set for us. */
}
else
{
t_lvlen[i] = dtd_vlen[i];
ctf_str_add_ref (fp, name, &t_lvlen[i].ctlm_name);
if (ctf_type_add_ref (fp, &t_lvlen[i].ctlm_type) < 0)
return -1; /* errno is set for us. */
}
}
}
@@ -906,9 +1044,12 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
break;
}
}
dtd->dtd_final_type = id;
}
*tptr = t;
return 0;
}
/* Variable section. */
@@ -935,15 +1076,17 @@ ctf_sort_var (const void *one_, const void *two_, void *arg_)
/* Overall serialization. */
/* Do all aspects of serialization up to strtab writeout and variable table
sorting. The resulting dict will have the LCTF_PRESERIALIZED flag on and
must not be modified in any way before serialization. (This is not enforced,
as this feature is internal-only, employed by the linker machinery.) */
sorting, including final type ID assignment. The resulting dict will have
the LCTF_PRESERIALIZED flag on and must not be modified in any way before
serialization. (This is only lightly enforced, as this feature is internal-
only, employed by the linker machinery.) */
int
ctf_preserialize (ctf_dict_t *fp)
{
ctf_header_t hdr, *hdrp;
ctf_dvdef_t *dvd;
ctf_dtdef_t *dtd;
int sym_functions = 0;
unsigned char *t;
@@ -956,15 +1099,50 @@ ctf_preserialize (ctf_dict_t *fp)
emit_symtypetab_state_t symstate;
memset (&symstate, 0, sizeof (emit_symtypetab_state_t));
ctf_dprintf ("Preserializing dict for %s\n", ctf_cuname (fp));
if (fp->ctf_flags & LCTF_NO_STR)
return (ctf_set_errno (fp, ECTF_NOPARENT));
/* Prohibit reserialization of dicts for which we have dynamic state inherited
from the upgrade process which we cannot record in the dict. Right now,
this applies only to CTFv1 dicts, which have a different parent/child type
offset to v2 and higher, and nowhere to record this in CTFv4. */
/* Make sure that any parents have been serialized at least once since the
last type was added to them, so we have known final IDs for all their
types. */
if (!fp->ctf_parent)
if (fp->ctf_parent)
{
if (fp->ctf_parent->ctf_nprovtypes > 0)
{
ctf_dtdef_t *dtd;
dtd = ctf_list_prev (&fp->ctf_parent->ctf_dtdefs);
if (dtd && dtd->dtd_final_type == 0)
{
ctf_set_errno (fp, ECTF_NOTSERIALIZED);
ctf_err_warn (fp, 0, 0, _("cannot write out child dict: write out the parent dict first"));
return -1; /* errno is set for us. */
}
}
/* Prohibit serialization of a dict which has already been serialized and
whose parent has had more types added to it since then: this dict would
have overlapping types if serialized, since we only pass through
newly-added types to renumber them, not already-existing types in the
read-in buffer. You can emit such dicts using ctf_link, which can
change type IDs arbitrarily, resolving all overlaps. */
if (fp->ctf_header->cth_stroff - fp->ctf_header->cth_typeoff > 0 &&
fp->ctf_header->cth_parent_typemax < fp->ctf_parent->ctf_typemax)
{
ctf_set_errno (fp, ECTF_NOTSERIALIZED);
ctf_err_warn (fp, 0, 0, _("cannot write out already-written child dict: parent has had %u types added"),
fp->ctf_parent->ctf_typemax - fp->ctf_header->cth_parent_typemax);
return -1; /* errno is set for us. */
}
fp->ctf_header->cth_parent_typemax = fp->ctf_parent->ctf_typemax;
}
else
{
/* Prohibit serialization of a parent dict which has already been
serialized, has children, and has had strings added since the last
@@ -1054,12 +1232,17 @@ ctf_preserialize (ctf_dict_t *fp)
hdr.cth_stroff = hdr.cth_typeoff + type_size;
hdr.cth_strlen = 0;
hdr.cth_parent_strlen = 0;
if (fp->ctf_parent)
hdr.cth_parent_typemax = fp->ctf_parent->ctf_typemax;
buf_size = sizeof (ctf_header_t) + hdr.cth_stroff + hdr.cth_strlen;
if ((buf = malloc (buf_size)) == NULL)
return (ctf_set_errno (fp, EAGAIN));
fp->ctf_serializing_buf = buf;
fp->ctf_serializing_buf_size = buf_size;
memcpy (buf, &hdr, sizeof (ctf_header_t));
t = (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_objtoff;
@@ -1071,10 +1254,7 @@ ctf_preserialize (ctf_dict_t *fp)
if (ctf_emit_symtypetab_sects (fp, &symstate, &t, objt_size, func_size,
objtidx_size, funcidx_size) < 0)
{
free (buf);
return -1; /* errno is set for us. */
}
goto err;
assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_varoff);
@@ -1089,6 +1269,9 @@ ctf_preserialize (ctf_dict_t *fp)
ctf_str_add_ref (fp, dvd->dvd_name, &var->ctv_name);
var->ctv_type = (uint32_t) dvd->dvd_type;
if (ctf_type_add_ref (fp, &var->ctv_type) < 0)
goto err;
}
assert (i == nvars);
@@ -1102,39 +1285,66 @@ ctf_preserialize (ctf_dict_t *fp)
memcpy (t, fp->ctf_buf + fp->ctf_header->cth_typeoff,
fp->ctf_header->cth_stroff - fp->ctf_header->cth_typeoff);
t += fp->ctf_header->cth_stroff - fp->ctf_header->cth_typeoff;
ctf_emit_type_sect (fp, &t);
if (ctf_emit_type_sect (fp, &t) < 0)
goto err;
assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff);
fp->ctf_serializing_buf = buf;
fp->ctf_serializing_buf_size = buf_size;
/* All types laid out: update all refs to types to cite the final IDs. */
/* Prohibit additions and the like from this point on. */
fp->ctf_flags |= LCTF_NO_STR;
for (dtd = ctf_list_next (&fp->ctf_dtdefs);
dtd != NULL; dtd = ctf_list_next (dtd))
{
if (!ctf_assert (fp, dtd->dtd_type != 0 && dtd->dtd_final_type != 0))
goto err;
ctf_update_refs (&dtd->dtd_refs, dtd->dtd_final_type);
}
ctf_type_purge_refs (fp);
/* Prohibit type and string additions from this point on. */
fp->ctf_flags |= LCTF_NO_STR | LCTF_NO_TYPE;
return 0;
err:
fp->ctf_serializing_buf = NULL;
fp->ctf_serializing_buf_size = 0;
free (buf);
ctf_str_purge_refs (fp);
ctf_type_purge_refs (fp);
return -1; /* errno is set for us. */
}
/* Undo preserialization (called on error). */
void
ctf_depreserialize (ctf_dict_t *fp)
{
fp->ctf_flags &= ~LCTF_NO_STR;
ctf_str_purge_refs (fp);
ctf_type_purge_refs (fp);
free (fp->ctf_serializing_buf);
fp->ctf_serializing_buf = NULL;
fp->ctf_serializing_vars = NULL;
fp->ctf_serializing_buf_size = 0;
fp->ctf_serializing_nvars = 0;
fp->ctf_flags &= ~(LCTF_NO_STR | LCTF_NO_TYPE);
}
/* Emit a new CTF dict which is a serialized copy of this one: also reify
the string table and update all offsets in the current dict suitably.
(This simplifies ctf-string.c a little, at the cost of storing a second
copy of the strtab if this dict was originally read in via ctf_open.)
/* Emit a new CTF dict which is a serialized copy of this one: also reify the
string table and update all offsets in the newly-serialized dict suitably.
(This simplifies ctf-string.c a little, at the cost of storing a second copy
of the strtab during serialization.)
Other aspects of the existing dict are unchanged, although some
static entries may be duplicated in the dynamic state (which should
have no effect on visible operation). */
Other aspects of the existing dict are unchanged, although some static
entries may be duplicated in the dynamic state (which should have no effect
on visible operation). */
static unsigned char *
ctf_serialize (ctf_dict_t *fp, size_t *bufsiz)
@@ -1177,13 +1387,15 @@ ctf_serialize (ctf_dict_t *fp, size_t *bufsiz)
hdrp = (ctf_header_t *) fp->ctf_serializing_buf;
ctf_dprintf ("Writing strtab for %s\n", ctf_cuname (fp));
strtab = ctf_str_write_strtab (fp);
if (strtab == NULL)
goto err;
/* Now the string table is constructed, we can sort the buffer of
ctf_varent_t's. */
/* Now the string table is constructed and all the refs updated, we can sort
the buffer of ctf_varent_t's. */
ctf_sort_var_arg_cb_t sort_var_arg = { fp, (ctf_strs_t *) strtab };
ctf_qsort_r (fp->ctf_serializing_vars, fp->ctf_serializing_nvars,
sizeof (ctf_varent_t), ctf_sort_var, &sort_var_arg);
@@ -1207,6 +1419,7 @@ ctf_serialize (ctf_dict_t *fp, size_t *bufsiz)
fp->ctf_serializing_vars = NULL;
fp->ctf_serializing_buf_size = 0;
fp->ctf_serializing_nvars = 0;
fp->ctf_flags &= ~LCTF_NO_TYPE;
return buf;