binutils-gdb/libctf/testsuite/libctf-lookup/add-to-opened.c
Nick Alcock 483546ce4f libctf: make ctf_serialize() actually serialize
ctf_serialize() evolved from the old ctf_update(), which mutated the
in-memory CTF dict to make all the dynamic in-memory types into static,
unchanging written-to-the-dict types (by deserializing and reserializing
it): back in the days when you could only do type lookups on static types,
this meant you could see all the types you added recently, at the small,
small cost of making it impossible to change those older types ever again
and inducing an amortized O(n^2) cost if you actually wanted to add
references to types you added at arbitrary times to later types.

It also reset things so that ctf_discard() would throw away only types you
added after the most recent ctf_update() call.

Some time ago this was all changed so that you could look up dynamic types
just as easily as static types: ctf_update() changed so that only its
visible side-effect of affecting ctf_discard() remained: the old
ctf_update() was renamed to ctf_serialize(), made internal to libctf, and
called from the various functions that wrote files out.

... but it was still working by serializing and deserializing the entire
dict, swapping out its guts with the newly-serialized copy in an invasive
and horrible fashion that coupled ctf_serialize() to almost every field in
the ctf_dict_t.  This is totally useless, and fixing it is easy: just rip
all that code out and have ctf_serialize return a serialized representation,
and let everything use that directly.  This simplifies most of its callers
significantly.

(It also points up another bug: ctf_gzwrite() failed to call ctf_serialize()
at all, so it would only ever work for a dict you just ctf_write_mem()ed
yourself, just for its invisible side-effect of serializing the dict!)

This lets us simplify away a bunch of internal-only open-side functionality
for overriding the syn_ext_strtab and some just-added functionality for
forcing in an existing atoms table, without loss of functionality, and lets
us lift the restriction on reserializing a dict that was ctf_open()ed rather
than being ctf_create()d: it's now perfectly OK to open a dict, modify it
(except for adding members to existing structs, unions, or enums, which
fails with -ECTF_RDONLY), and write it out again, just as one would expect.

libctf/

	* ctf-serialize.c (ctf_symtypetab_sect_sizes): Fix typos.
	(ctf_type_sect_size): Add static type sizes too.
	(ctf_serialize): Return the new dict rather than updating the
	existing dict.  No longer fail for dicts with static types;
	copy them onto the start of the new types table.
	(ctf_gzwrite): Actually serialize before gzwriting.
	(ctf_write_mem): Improve forced (test-mode) endian-flipping:
	flip dicts even if they are too small to be compressed.
	Improve confusing variable naming.
	* ctf-archive.c (arc_write_one_ctf): Don't bother to call
	ctf_serialize: both the functions we call do so.
	* ctf-string.c (ctf_str_create_atoms): Drop serializing case
	(atoms arg).
	* ctf-open.c (ctf_simple_open): Call ctf_bufopen directly.
	(ctf_simple_open_internal): Delete.
	(ctf_bufopen_internal): Delete/rename to ctf_bufopen: no
	longer bother with syn_ext_strtab or forced atoms table,
	serialization no longer needs them.
	* ctf-create.c (ctf_create): Call ctf_bufopen directly.
	* ctf-impl.h (ctf_str_create_atoms): Drop atoms arg.
	(ctf_simple_open_internal): Delete.
	(ctf_bufopen_internal): Likewise.
	(ctf_serialize): Adjust.
	* testsuite/libctf-lookup/add-to-opened.c: Adjust now that
	this is supposed to work.
2024-04-19 16:14:47 +01:00

149 lines
6.2 KiB
C

/* Make sure you can add to ctf_open()ed CTF dicts, and that you
cannot make changes to existing types. */
#include <ctf-api.h>
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char *argv[])
{
ctf_dict_t *fp;
ctf_archive_t *ctf;
ctf_id_t type, ptrtype;
ctf_arinfo_t ar = {0, 0, 0};
ctf_encoding_t en = { CTF_INT_SIGNED, 0, sizeof (int) };
unsigned char *ctf_written;
size_t size;
int err;
if (argc != 2)
{
fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
exit(1);
}
if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
goto open_err;
if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
goto open_err;
/* Check that various modifications to already-written types
are prohibited. */
if (ctf_add_integer (fp, CTF_ADD_ROOT, "int", &en) == 0)
fprintf (stderr, "allowed to add integer existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add integer in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_typedef (fp, CTF_ADD_ROOT, "a_typedef", 0) == 0)
fprintf (stderr, "allowed to add typedef existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add typedef in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_struct (fp, CTF_ADD_ROOT, "a_struct") == 0)
fprintf (stderr, "allowed to add struct existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add struct in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_union (fp, CTF_ADD_ROOT, "a_union") == 0)
fprintf (stderr, "allowed to add union existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add union in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_enum (fp, CTF_ADD_ROOT, "an_enum") == 0)
fprintf (stderr, "allowed to add enum existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add enum in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_struct (fp, CTF_ADD_ROOT, "struct_forward") == 0)
fprintf (stderr, "allowed to promote struct forward existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to promote struct forward in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_union (fp, CTF_ADD_ROOT, "union_forward") == 0)
fprintf (stderr, "allowed to promote union forward existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to promote union forward in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_enum (fp, CTF_ADD_ROOT, "enum_forward") == 0)
fprintf (stderr, "allowed to promote enum forward existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to promote enum forward in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_member (fp, type, "wombat", 0) == 0)
fprintf (stderr, "allowed to add member to struct existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add member to struct in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if ((type = ctf_lookup_by_name (fp, "union a_union")) == CTF_ERR)
fprintf (stderr, "Lookup of union a_union failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_member (fp, type, "wombat", 0) == 0)
fprintf (stderr, "allowed to add member to union existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add member to union in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if ((type = ctf_lookup_by_name (fp, "enum an_enum")) == CTF_ERR)
fprintf (stderr, "Lookup of enum an_enum failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_add_enumerator (fp, type, "wombat", 0) == 0)
fprintf (stderr, "allowed to add enumerator to enum existing in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to add enumerator to enum in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if ((type = ctf_lookup_by_name (fp, "an_array")) == CTF_ERR)
fprintf (stderr, "Lookup of an_array failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if ((type = ctf_type_reference (fp, type)) == CTF_ERR)
fprintf (stderr, "Lookup of type reffed by an_array failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_set_array (fp, type, &ar) == 0)
fprintf (stderr, "allowed to set array in readonly portion\n");
if (ctf_errno (fp) != ECTF_RDONLY)
fprintf (stderr, "unexpected error %s attempting to set array in readonly portion\n", ctf_errmsg (ctf_errno (fp)));
if ((ctf_written = ctf_write_mem (fp, &size, 4096)) == NULL)
fprintf (stderr, "Re-writeout unexpectedly failed: %s\n", ctf_errmsg (ctf_errno (fp)));
free (ctf_written);
/* Finally, make sure we can add new types, and look them up again. */
if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if ((ptrtype = ctf_add_pointer (fp, CTF_ADD_ROOT, type)) == CTF_ERR)
fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_type_reference (fp, ptrtype) == CTF_ERR)
fprintf (stderr, "Lookup of pointer preserved across writeout failed: %s\n", ctf_errmsg (ctf_errno (fp)));
if (ctf_type_reference (fp, ptrtype) != type)
fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp, ptrtype), type);
ctf_dict_close (fp);
ctf_close (ctf);
printf ("All done.\n");
return 0;
open_err:
fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
return 1;
}