From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2103) id E18593849AEB; Fri, 19 Apr 2024 15:52:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E18593849AEB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1713541945; bh=nK+Dgu1srsEcdDaaOwFZofXEUoXGz0QP6Q+vgyyhwUg=; h=From:To:Subject:Date:From; b=qc4SVPxMjxJ13HxoJ2J0wOe5xnztvFPol2AtBDe0+Q4uo1g12eWb7C3pTmNKkQzDL hxAqaUNCTEUk0vzy5UzchZHNoUbWW+nDk0gjJbxxU/mmvLMccW+3NG42t/QeTTc67k 3WyRtdL0C3AMI5uyZbCUlWK+4b3oNscVhD64KYis= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nick Alcock To: binutils-cvs@sourceware.org Subject: [binutils-gdb] libctf: add rewriting tests X-Act-Checkin: binutils-gdb X-Git-Author: Nick Alcock X-Git-Refname: refs/heads/master X-Git-Oldrev: a96a9ca9535e0a59bd4367dd84040aa0a9ac6f48 X-Git-Newrev: 3b5e2d0e8d55fd61c8b0f7aaf61b6b097654a5c1 Message-Id: <20240419155225.E18593849AEB@sourceware.org> Date: Fri, 19 Apr 2024 15:52:25 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D3b5e2d0e8d55= fd61c8b0f7aaf61b6b097654a5c1 commit 3b5e2d0e8d55fd61c8b0f7aaf61b6b097654a5c1 Author: Nick Alcock Date: Tue Apr 2 16:13:46 2024 +0100 libctf: add rewriting tests =20 Now there's a chance of it actually working, we can add more tests for the long-broken dict read-and-rewrite cases. This is the first ever test for the (rarely-used, unpleasant, and until recently completely broken) ctf_gzwrite function. =20 libctf/ =20 * testsuite/libctf-regression/gzrewrite*: New test. * testsuite/libctf-regression/zrewrite*: Likewise. Diff: --- libctf/testsuite/libctf-regression/gzrewrite-ctf.c | 19 +++ libctf/testsuite/libctf-regression/gzrewrite.c | 165 +++++++++++++++++= ++++ libctf/testsuite/libctf-regression/gzrewrite.lk | 3 + libctf/testsuite/libctf-regression/zrewrite.c | 156 +++++++++++++++++= ++ libctf/testsuite/libctf-regression/zrewrite.lk | 3 + 5 files changed, 346 insertions(+) diff --git a/libctf/testsuite/libctf-regression/gzrewrite-ctf.c b/libctf/te= stsuite/libctf-regression/gzrewrite-ctf.c new file mode 100644 index 00000000000..b5d483ea1cb --- /dev/null +++ b/libctf/testsuite/libctf-regression/gzrewrite-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-regression/gzrewrite.c b/libctf/testsu= ite/libctf-regression/gzrewrite.c new file mode 100644 index 00000000000..8e279ca3fac --- /dev/null +++ b/libctf/testsuite/libctf-regression/gzrewrite.c @@ -0,0 +1,165 @@ +/* Make sure that you can modify then ctf_gzwrite() a dict + and it changes after modification. */ + +#include +#include +#include +#include +#include +#include +#include + +char *read_gz(const char *path, size_t *len) +{ + char *in =3D NULL; + char buf[4096]; + gzFile foo; + size_t ret; + + if ((foo =3D gzopen (path, "rb")) =3D=3D NULL) + return NULL; + + *len =3D 0; + while ((ret =3D gzread (foo, buf, 4096)) > 0) + { + if ((in =3D realloc (in, *len + ret)) =3D=3D NULL) + { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + memcpy (&in[*len], buf, ret); + *len +=3D ret; + } + if (ret < 0) + { + int errnum; + const char *err; + err =3D gzerror (foo, &errnum); + if (errnum !=3D Z_ERRNO) + fprintf (stderr, "error reading %s: %s\n", path, err); + else + fprintf (stderr, "error reading %s: %s\n", path, strerror(errno)); + exit (1); + } + gzclose (foo); + return in; +} + +int +main (int argc, char *argv[]) +{ + ctf_dict_t *fp, *fp_b; + ctf_archive_t *ctf; + gzFile foo; + char *a, *b; + size_t a_len, b_len; + ctf_id_t type, ptrtype; + int err; + + if (argc !=3D 2) + { + fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]); + exit(1); + } + + if ((ctf =3D ctf_open (argv[1], NULL, &err)) =3D=3D NULL) + goto open_err; + if ((fp =3D ctf_dict_open (ctf, NULL, &err)) =3D=3D NULL) + goto open_err; + + if ((foo =3D gzopen ("tmpdir/one.gz", "wb")) =3D=3D NULL) + goto write_gzerr; + if (ctf_gzwrite (fp, foo) < 0) + goto write_err; + gzclose (foo); + + if ((foo =3D gzopen ("tmpdir/two.gz", "wb")) =3D=3D NULL) + goto write_gzerr; + if (ctf_gzwrite (fp, foo) < 0) + goto write_err; + gzclose (foo); + + if ((a =3D read_gz ("tmpdir/one.gz", &a_len)) =3D=3D NULL) + goto read_err; + + if ((b =3D read_gz ("tmpdir/two.gz", &b_len)) =3D=3D NULL) + goto read_err; + + if (a_len !=3D b_len || memcmp (a, b, a_len) !=3D 0) + { + fprintf (stderr, "consecutive gzwrites are different: lengths %i and= %i\n", a_len, b_len); + return 1; + } + + free (b); + + /* Add some new types to the dict and write it out, then read it back in= and + make sure they're still there, and that at least some of the + originally-present data objects are still there too. */ + + if ((type =3D ctf_lookup_by_name (fp, "struct a_struct")) =3D=3D CTF_ERR) + fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg = (ctf_errno (fp))); + + if ((ptrtype =3D ctf_add_pointer (fp, CTF_ADD_ROOT, type)) =3D=3D CTF_ER= R) + fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_er= rmsg (ctf_errno (fp))); + + unlink ("tmpdir/two.gz"); + if ((foo =3D gzopen ("tmpdir/two.gz", "wb")) =3D=3D NULL) + goto write_gzerr; + if (ctf_gzwrite (fp, foo) < 0) + goto write_err; + gzclose (foo); + + if ((b =3D read_gz ("tmpdir/two.gz", &b_len)) =3D=3D NULL) + goto read_err; + + if (memcmp (a, b, b_len) =3D=3D 0) + { + fprintf (stderr, "gzwrites after adding types does not change the di= ct\n"); + return 1; + } + + free (a); + if ((fp_b =3D ctf_simple_open (b, b_len, NULL, 0, 0, NULL, 0, &err)) =3D= =3D NULL) + goto open_err; + + if (ctf_type_reference (fp_b, ptrtype) =3D=3D CTF_ERR) + fprintf (stderr, "Lookup of pointer preserved across writeout failed: = %s\n", ctf_errmsg (ctf_errno (fp_b))); + + if (ctf_type_reference (fp_b, ptrtype) !=3D type) + fprintf (stderr, "Look up of newly-added type in serialized dict yield= s ID %lx, expected %lx\n", ctf_type_reference (fp_b, ptrtype), type); + + if (ctf_lookup_by_symbol_name (fp_b, "an_int") =3D=3D CTF_ERR) + fprintf (stderr, "Lookup of symbol an_int failed: %s\n", ctf_errmsg (c= tf_errno (fp_b))); + + free (b); + ctf_dict_close (fp); + ctf_dict_close (fp_b); + ctf_close (ctf); + + printf ("All done.\n"); + return 0; +=20 + open_err: + fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err)); + return 1; + write_err:=20 + fprintf (stderr, "%s: cannot write: %s\n", argv[0], ctf_errmsg (ctf_errn= o (fp))); + return 1; + write_gzerr: + { + int errnum; + const char *err; + + err =3D gzerror (foo, &errnum); + if (errnum !=3D Z_ERRNO) + fprintf (stderr, "error gzwriting: %s\n", err); + else + fprintf (stderr, "error gzwriting: %s\n", strerror(errno)); + return 1; + } + read_err:=20 + fprintf (stderr, "%s: cannot read\n", argv[0]); + return 1; +} diff --git a/libctf/testsuite/libctf-regression/gzrewrite.lk b/libctf/tests= uite/libctf-regression/gzrewrite.lk new file mode 100644 index 00000000000..2d0de3dc464 --- /dev/null +++ b/libctf/testsuite/libctf-regression/gzrewrite.lk @@ -0,0 +1,3 @@ +# source: gzrewrite-ctf.c +# lookup: gzrewrite.c +All done. diff --git a/libctf/testsuite/libctf-regression/zrewrite.c b/libctf/testsui= te/libctf-regression/zrewrite.c new file mode 100644 index 00000000000..4d5d15e7985 --- /dev/null +++ b/libctf/testsuite/libctf-regression/zrewrite.c @@ -0,0 +1,156 @@ +/* Make sure that you can modify then ctf_compress_write() a dict + and it changes after modification. */ + +#include +#include +#include +#include +#include +#include +#include + +char *read_file(const char *path, size_t *len) +{ + char *in =3D NULL; + char buf[4096]; + int foo; + size_t ret; + + if ((foo =3D open (path, O_RDONLY)) < 0) + { + fprintf (stderr, "error opening %s: %s\n", path, strerror(errno)); + exit (1); + } + + *len =3D 0; + while ((ret =3D read (foo, buf, 4096)) > 0) + { + if ((in =3D realloc (in, *len + ret)) =3D=3D NULL) + { + fprintf (stderr, "Out of memory\n"); + exit (1); + } + + memcpy (&in[*len], buf, ret); + *len +=3D ret; + } + + if (ret < 0) + { + fprintf (stderr, "error reading %s: %s\n", path, strerror(errno)); + exit (1); + } + close (foo); + return in; +} + +int +main (int argc, char *argv[]) +{ + ctf_dict_t *fp, *fp_b; + ctf_archive_t *ctf, *ctf_b; + int foo; + char *a, *b; + size_t a_len, b_len; + ctf_id_t type, ptrtype; + int err; + + if (argc !=3D 2) + { + fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]); + exit(1); + } + + if ((ctf =3D ctf_open (argv[1], NULL, &err)) =3D=3D NULL) + goto open_err; + if ((fp =3D ctf_dict_open (ctf, NULL, &err)) =3D=3D NULL) + goto open_err; + + if ((foo =3D open ("tmpdir/one", O_CREAT | O_TRUNC | O_WRONLY, 0666)) < = 0) + goto write_stderr; + if (ctf_compress_write (fp, foo) < 0) + goto write_err; + close (foo); + + if ((foo =3D open ("tmpdir/two", O_CREAT | O_TRUNC | O_WRONLY, 0666)) < = 0) + goto write_stderr; + if (ctf_compress_write (fp, foo) < 0) + goto write_err; + close (foo); + + a =3D read_file ("tmpdir/one", &a_len); + b =3D read_file ("tmpdir/two", &b_len); + + if (a_len !=3D b_len || memcmp (a, b, a_len) !=3D 0) + { + fprintf (stderr, "consecutive compress_writes are different: lengths= %i and %i\n", a_len, b_len); + return 1; + } + + free (b); + + /* Add some new types to the dict and write it out, then read it back in= and + make sure they're still there, and that at least some of the + originally-present data objects are still there too. */ + + if ((type =3D ctf_lookup_by_name (fp, "struct a_struct")) =3D=3D CTF_ERR) + fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg = (ctf_errno (fp))); + + if ((ptrtype =3D ctf_add_pointer (fp, CTF_ADD_ROOT, type)) =3D=3D CTF_ER= R) + fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_er= rmsg (ctf_errno (fp))); + + unlink ("tmpdir/two"); + + if ((foo =3D open ("tmpdir/two", O_CREAT | O_TRUNC | O_WRONLY, 0666)) < = 0) + goto write_stderr; + if (ctf_compress_write (fp, foo) < 0) + goto write_err; + close (foo); + + b =3D read_file ("tmpdir/two", &b_len); + + if (memcmp (a, b, b_len) =3D=3D 0) + { + fprintf (stderr, "compress_writes after adding types does not change= the dict\n"); + return 1; + } + + free (a); + free (b); + + if ((ctf_b =3D ctf_open ("tmpdir/two", NULL, &err)) =3D=3D NULL) + goto open_err; + if ((fp_b =3D ctf_dict_open (ctf_b, NULL, &err)) =3D=3D NULL) + goto open_err; + + if (ctf_type_reference (fp_b, ptrtype) =3D=3D CTF_ERR) + fprintf (stderr, "Lookup of pointer preserved across writeout failed: = %s\n", ctf_errmsg (ctf_errno (fp_b))); + + if (ctf_type_reference (fp_b, ptrtype) !=3D type) + fprintf (stderr, "Look up of newly-added type in serialized dict yield= s ID %lx, expected %lx\n", ctf_type_reference (fp_b, ptrtype), type); + + if (ctf_lookup_by_symbol_name (fp_b, "an_int") =3D=3D CTF_ERR) + fprintf (stderr, "Lookup of symbol an_int failed: %s\n", ctf_errmsg (c= tf_errno (fp_b))); + + ctf_dict_close (fp); + ctf_close (ctf); + + ctf_dict_close (fp_b); + ctf_close (ctf_b); + + printf ("All done.\n"); + return 0; +=20 + open_err: + fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err)); + return 1; + write_err:=20 + fprintf (stderr, "%s: cannot write: %s\n", argv[0], ctf_errmsg (ctf_errn= o (fp))); + return 1; + write_stderr: + fprintf (stderr, "%s: cannot open for writing: %s\n", argv[0], strerror = (errno)); + return 1; + read_err:=20 + fprintf (stderr, "%s: cannot read\n", argv[0]); + return 1; +} diff --git a/libctf/testsuite/libctf-regression/zrewrite.lk b/libctf/testsu= ite/libctf-regression/zrewrite.lk new file mode 100644 index 00000000000..a0a53d91a04 --- /dev/null +++ b/libctf/testsuite/libctf-regression/zrewrite.lk @@ -0,0 +1,3 @@ +# source: gzrewrite-ctf.c +# lookup: zrewrite.c +All done.