aboutsummaryrefslogtreecommitdiff
path: root/libctf/ctf-serialize.c
diff options
context:
space:
mode:
authorNick Alcock <nick.alcock@oracle.com>2021-03-18 12:37:52 +0000
committerNick Alcock <nick.alcock@oracle.com>2021-03-18 12:40:40 +0000
commit986e9e3aa03f854bedacef7fac38fe8f009a416c (patch)
tree7e553bb8e278ab340513d57d67e37a4c2c503109 /libctf/ctf-serialize.c
parent2a05d50e90c2c8219dd4119788548f64a934190e (diff)
downloadgdb-986e9e3aa03f854bedacef7fac38fe8f009a416c.zip
gdb-986e9e3aa03f854bedacef7fac38fe8f009a416c.tar.gz
gdb-986e9e3aa03f854bedacef7fac38fe8f009a416c.tar.bz2
libctf: do not corrupt strings across ctf_serialize
The preceding change revealed a new bug: the string table is sorted for better compression, so repeated serialization with type (or member) additions in the middle can move strings around. But every serialization flushes the set of refs (the memory locations that are automatically updated with a final string offset when the strtab is updated), so if we are not to have string offsets go stale, we must do all ref additions within the serialization code (which walks the complete set of types and symbols anyway). Unfortunately, we were adding one ref in another place: the type name in the dynamic type definitions, which has a ref added to it by ctf_add_generic. So adding a type, serializing (via, say, one of the ctf_write functions), adding another type with a name that sorts earlier, and serializing again will corrupt the name of the first type because it no longer had a ref pointing to its dtd entry's name when its string offset was shifted later in the strtab to mae way for the other type. To ensure that we don't miss strings, we also maintain a set of *pending refs* that will be added later (during serialization), and remove entries from that set when the ref is finally added. We always use ctf_str_add_pending outside ctf-serialize.c, ensure that ctf_serialize adds all strtab offsets as refs (even those in the dtds) on every serialization, and mandate that no refs are live on entry to ctf_serialize and that all pending refs are gone before strtab finalization. (Of necessity ctf_serialize has to traverse all strtab offsets in the dtds in order to serialize them, so adding them as refs at the same time is easy.) (Note that we still can't erase unused atoms when we roll back, though we can erase unused refs: members and enums are still not removed by rollbacks and might reference strings added after the snapshot.) libctf/ChangeLog 2021-03-18 Nick Alcock <nick.alcock@oracle.com> * ctf-hash.c (ctf_dynset_elements): New. * ctf-impl.h (ctf_dynset_elements): Declare it. (ctf_str_add_pending): Likewise. (ctf_dict_t) <ctf_str_pending_ref>: New, set of refs that must be added during serialization. * ctf-string.c (ctf_str_create_atoms): Initialize it. (CTF_STR_ADD_REF): New flag. (CTF_STR_MAKE_PROVISIONAL): Likewise. (CTF_STR_PENDING_REF): Likewise. (ctf_str_add_ref_internal): Take a flags word rather than int params. Populate, and clear out, ctf_str_pending_ref. (ctf_str_add): Adjust accordingly. (ctf_str_add_external): Likewise. (ctf_str_add_pending): New. (ctf_str_remove_ref): Also remove the potential ref if it is a pending ref. * ctf-serialize.c (ctf_serialize): Prohibit addition of strings with ctf_str_add_ref before serialization. Ensure that the ctf_str_pending_ref set is empty before strtab finalization. (ctf_emit_type_sect): Add a ref to the ctt_name. * ctf-create.c (ctf_add_generic): Add the ctt_name as a pending ref. * testsuite/libctf-writable/reserialize-strtab-corruption.*: New test.
Diffstat (limited to 'libctf/ctf-serialize.c')
-rw-r--r--libctf/ctf-serialize.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/libctf/ctf-serialize.c b/libctf/ctf-serialize.c
index 17f11f6..0eff0e1 100644
--- a/libctf/ctf-serialize.c
+++ b/libctf/ctf-serialize.c
@@ -870,7 +870,10 @@ ctf_emit_type_sect (ctf_dict_t *fp, unsigned char **tptr)
copied = (ctf_stype_t *) t; /* name is at the start: constant offset. */
if (copied->ctt_name
&& (name = ctf_strraw (fp, copied->ctt_name)) != NULL)
- ctf_str_add_ref (fp, name, &copied->ctt_name);
+ {
+ ctf_str_add_ref (fp, name, &copied->ctt_name);
+ ctf_str_add_ref (fp, name, &dtd->dtd_data.ctt_name);
+ }
t += len;
switch (kind)
@@ -955,6 +958,7 @@ ctf_serialize (ctf_dict_t *fp)
ctf_varent_t *dvarents;
ctf_strs_writable_t strtab;
int err;
+ int num_missed_str_refs;
unsigned char *t;
unsigned long i;
@@ -973,6 +977,16 @@ ctf_serialize (ctf_dict_t *fp)
if (!(fp->ctf_flags & LCTF_DIRTY))
return 0;
+ /* The strtab refs table must be empty at this stage. Any refs already added
+ will be corrupted by any modifications, including reserialization, after
+ strtab finalization is complete. Only this function, and functions it
+ calls, may add refs, and all memory locations (including in the dtds)
+ containing strtab offsets must be traversed as part of serialization, and
+ refs added. */
+
+ if (!ctf_assert (fp, fp->ctf_str_num_refs == 0))
+ return -1; /* errno is set for us. */
+
/* Fill in an initial CTF header. We will leave the label, object,
and function sections empty and only output a header, type section,
and string table. The type section begins at a 4-byte aligned
@@ -1052,6 +1066,12 @@ ctf_serialize (ctf_dict_t *fp)
assert (t == (unsigned char *) buf + sizeof (ctf_header_t) + hdr.cth_stroff);
+ /* Every string added outside serialization by ctf_str_add_pending should
+ now have been added by ctf_add_ref. */
+ num_missed_str_refs = ctf_dynset_elements (fp->ctf_str_pending_ref);
+ if (!ctf_assert (fp, num_missed_str_refs == 0))
+ goto err; /* errno is set for us. */
+
/* Construct the final string table and fill out all the string refs with the
final offsets. Then purge the refs list, because we're about to move this
strtab onto the end of the buf, invalidating all the offsets. */
@@ -1154,8 +1174,10 @@ ctf_serialize (ctf_dict_t *fp)
ctf_str_free_atoms (nfp);
nfp->ctf_str_atoms = fp->ctf_str_atoms;
nfp->ctf_prov_strtab = fp->ctf_prov_strtab;
+ nfp->ctf_str_pending_ref = fp->ctf_str_pending_ref;
fp->ctf_str_atoms = NULL;
fp->ctf_prov_strtab = NULL;
+ fp->ctf_str_pending_ref = NULL;
memset (&fp->ctf_dtdefs, 0, sizeof (ctf_list_t));
memset (&fp->ctf_errs_warnings, 0, sizeof (ctf_list_t));
fp->ctf_add_processing = NULL;