From 8a60c93096326ef818dd72d0a44bd575a04cc55a Mon Sep 17 00:00:00 2001 From: Nick Alcock Date: Tue, 19 Dec 2023 16:58:19 +0000 Subject: libctf: support addition of types to dicts read via ctf_open() libctf has long declared deserialized dictionaries (out of files or ELF sections or memory buffers or whatever) to be read-only: back in the furthest prehistory this was not the case, in that you could add a few sorts of type to such dicts, but attempting to do so often caused horrible memory corruption, so I banned the lot. But it turns out real consumers want it (notably DTrace, which synthesises pointers to types that don't have them and adds them to the ctf_open()ed dicts if it needs them). Let's bring it back again, but without the memory corruption and without the massive code duplication required in days of yore to distinguish between static and dynamic types: the representation of both types has been identical for a few years, with the only difference being that types as a whole are stored in a big buffer for types read in via ctf_open and per-type hashtables for newly-added types. So we discard the internally-visible concept of "readonly dictionaries" in favour of declaring the *range of types* that were already present when the dict was read in to be read-only: you can't modify them (say, by adding members to them if they're structs, or calling ctf_set_array on them), but you can add more types and point to them. (The API remains the same, with calls sometimes returning ECTF_RDONLY, but now they do so less often.) This is a fairly invasive change, mostly because code written since the ban was introduced didn't take the possibility of a static/dynamic split into account. Some of these irregularities were hard to define as anything but bugs. Notably: - The symbol handling was assuming that symbols only needed to be looked for in dynamic hashtabs or static linker-laid-out indexed/ nonindexed layouts, but now we want to check both in case people added more symbols to a dict they opened. - The code that handles type additions wasn't checking to see if types with the same name existed *at all* (so you could do ctf_add_typedef (fp, "foo", bar) repeatedly without error). This seems reasonable for types you just added, but we probably *do* want to ban addition of types with names that override names we already used in the ctf_open()ed portion, since that would probably corrupt existing type relationships. (Doing things this way also avoids causing new errors for any existing code that was doing this sort of thing.) - ctf_lookup_variable entirely failed to work for variables just added by ctf_add_variable: you had to write the dict out and read it back in again before they appeared. - The symbol handling remembered what symbols you looked up but didn't remember their types, so you could look up an object symbol and then find it popping up when you asked for function symbols, which seems less than ideal. Since we had to rejig things enough to be able to distinguish function and object symbols internally anyway (in order to give suitable errors if you try to add a symbol with a name that already existed in the ctf_open()ed dict), this bug suddenly became more visible and was easily fixed. We do not (yet) support writing out dicts that have been previously read in via ctf_open() or other deserializer (you can look things up in them, but not write them out a second time). This never worked, so there is no incompatibility; if it is needed at a later date, the serializer is a little bit closer to having it work now (the only table we don't deal with is the types table, and that's because the upcoming CTFv4 changes are likely to make major changes to the way that table is represented internally, so adding more code that depends on its current form seems like a bad idea). There is a new testcase that tests much of this, in particular that modification of existing types is still banned and that you can add new ones and chase them without error. libctf/ * ctf-impl.h (struct ctf_dict.ctf_symhash): Split into... (ctf_dict.ctf_symhash_func): ... this and... (ctf_dict.ctf_symhash_objt): ... this. (ctf_dict.ctf_stypes): New, counts static types. (LCTF_INDEX_TO_TYPEPTR): Use it instead of CTF_RDWR. (LCTF_RDWR): Deleted. (LCTF_DIRTY): Renumbered. (LCTF_LINKING): Likewise. (ctf_lookup_variable_here): New. (ctf_lookup_by_sym_or_name): Likewise. (ctf_symbol_next_static): Likewise. (ctf_add_variable_forced): Likewise. (ctf_add_funcobjt_sym_forced): Likewise. (ctf_simple_open_internal): Adjust. (ctf_bufopen_internal): Likewise. * ctf-create.c (ctf_grow_ptrtab): Adjust a lot to start with. (ctf_create): Migrate a bunch of initializations into bufopen. Force recreation of name tables. Do not forcibly override the model, let ctf_bufopen do it. (ctf_static_type): New. (ctf_update): Drop LCTF_RDWR check. (ctf_dynamic_type): Likewise. (ctf_add_function): Likewise. (ctf_add_type_internal): Likewise. (ctf_rollback): Check ctf_stypes, not LCTF_RDWR. (ctf_set_array): Likewise. (ctf_add_struct_sized): Likewise. (ctf_add_union_sized): Likewise. (ctf_add_enum): Likewise. (ctf_add_enumerator): Likewise (only on the target dict). (ctf_add_member_offset): Likewise. (ctf_add_generic): Drop LCTF_RDWR check. Ban addition of types with colliding names. (ctf_add_forward): Note safety under the new rules. (ctf_add_variable): Split all but the existence check into... (ctf_add_variable_forced): ... this new function. (ctf_add_funcobjt_sym): Likewise... (ctf_add_funcobjt_sym_forced): ... for this new function. * ctf-link.c (ctf_link_add_linker_symbol): Ban calling on dicts with any stypes. (ctf_link_add_strtab): Likewise. (ctf_link_shuffle_syms): Likewise. (ctf_link_intern_extern_string): Note pre-existing prohibition. * ctf-lookup.c (ctf_lookup_by_id): Drop LCTF_RDWR check. (ctf_lookup_variable): Split out looking in a dict but not its parent into... (ctf_lookup_variable_here): ... this new function. (ctf_lookup_symbol_idx): Track whether looking up a function or object: cache them separately. (ctf_symbol_next): Split out looking in non-dynamic symtypetab entries to... (ctf_symbol_next_static): ... this new function. Don't get confused by the simultaneous presence of static and dynamic symtypetab entries. (ctf_try_lookup_indexed): Don't waste time looking up symbols by index before there can be any idea how symbols are numbered. (ctf_lookup_by_sym_or_name): Distinguish between function and data object lookups. Drop LCTF_RDWR. (ctf_lookup_by_symbol): Adjust. (ctf_lookup_by_symbol_name): Likewise. * ctf-open.c (init_types): Rename to... (init_static_types): ... this. Drop LCTF_RDWR. Populate ctf_stypes. (ctf_simple_open): Drop writable arg. (ctf_simple_open_internal): Likewise. (ctf_bufopen): Likewise. (ctf_bufopen_internal): Populate fields only used for writable dicts. Drop LCTF_RDWR. (ctf_dict_close): Cater for symhash cache split. * ctf-serialize.c (ctf_serialize): Use ctf_stypes, not LCTF_RDWR. * ctf-types.c (ctf_variable_next): Drop LCTF_RDWR. * testsuite/libctf-lookup/add-to-opened*: New test. --- libctf/testsuite/libctf-lookup/add-to-opened-ctf.c | 19 +++ libctf/testsuite/libctf-lookup/add-to-opened.c | 147 +++++++++++++++++++++ libctf/testsuite/libctf-lookup/add-to-opened.lk | 3 + 3 files changed, 169 insertions(+) create mode 100644 libctf/testsuite/libctf-lookup/add-to-opened-ctf.c create mode 100644 libctf/testsuite/libctf-lookup/add-to-opened.c create mode 100644 libctf/testsuite/libctf-lookup/add-to-opened.lk (limited to 'libctf/testsuite') diff --git a/libctf/testsuite/libctf-lookup/add-to-opened-ctf.c b/libctf/testsuite/libctf-lookup/add-to-opened-ctf.c new file mode 100644 index 0000000..b5d483e --- /dev/null +++ b/libctf/testsuite/libctf-lookup/add-to-opened-ctf.c @@ -0,0 +1,19 @@ +int an_int; +char *a_char_ptr; +typedef int (*a_typedef) (int main); +struct struct_forward; +enum enum_forward; +union union_forward; +typedef int an_array[50]; +struct a_struct { int foo; }; +union a_union { int bar; }; +enum an_enum { FOO }; + +a_typedef a; +struct struct_forward *x; +union union_forward *y; +enum enum_forward *z; +struct a_struct *xx; +union a_union *yy; +enum an_enum *zz; +an_array ar; diff --git a/libctf/testsuite/libctf-lookup/add-to-opened.c b/libctf/testsuite/libctf-lookup/add-to-opened.c new file mode 100644 index 0000000..dc2e1f5 --- /dev/null +++ b/libctf/testsuite/libctf-lookup/add-to-opened.c @@ -0,0 +1,147 @@ +/* Make sure you can add to ctf_open()ed CTF dicts, and that you + cannot make changes to existing types. */ + +#include +#include +#include + +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, "Writeout unexpectedly succeeded: %s\n", ctf_errmsg (ctf_errno (fp))); + + if (ctf_errno (fp) != ECTF_RDONLY) + fprintf (stderr, "unexpected error %s trying to write out previously serialized dict\n", ctf_errmsg (ctf_errno (fp))); + + /* 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); + + printf ("All done.\n"); + return 0; + + open_err: + fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err)); + return 1; +} diff --git a/libctf/testsuite/libctf-lookup/add-to-opened.lk b/libctf/testsuite/libctf-lookup/add-to-opened.lk new file mode 100644 index 0000000..af84259 --- /dev/null +++ b/libctf/testsuite/libctf-lookup/add-to-opened.lk @@ -0,0 +1,3 @@ +# source: add-to-opened-ctf.c +# lookup: add-to-opened.c +All done. -- cgit v1.1