public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Nick Alcock <nick.alcock@oracle.com>
To: binutils@sourceware.org
Subject: [PATCH 18/22] libctf: add rewriting tests
Date: Wed, 17 Apr 2024 21:20:14 +0100	[thread overview]
Message-ID: <20240417202018.34966-19-nick.alcock@oracle.com> (raw)
In-Reply-To: <20240417202018.34966-1-nick.alcock@oracle.com>

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.

libctf/

	* testsuite/libctf-regression/gzrewrite*: New test.
	* testsuite/libctf-regression/zrewrite*: Likewise.
---
 .../libctf-regression/gzrewrite-ctf.c         |  19 ++
 .../testsuite/libctf-regression/gzrewrite.c   | 165 ++++++++++++++++++
 .../testsuite/libctf-regression/gzrewrite.lk  |   3 +
 libctf/testsuite/libctf-regression/zrewrite.c | 156 +++++++++++++++++
 .../testsuite/libctf-regression/zrewrite.lk   |   3 +
 5 files changed, 346 insertions(+)
 create mode 100644 libctf/testsuite/libctf-regression/gzrewrite-ctf.c
 create mode 100644 libctf/testsuite/libctf-regression/gzrewrite.c
 create mode 100644 libctf/testsuite/libctf-regression/gzrewrite.lk
 create mode 100644 libctf/testsuite/libctf-regression/zrewrite.c
 create mode 100644 libctf/testsuite/libctf-regression/zrewrite.lk

diff --git a/libctf/testsuite/libctf-regression/gzrewrite-ctf.c b/libctf/testsuite/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/testsuite/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 <ctf-api.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <zlib.h>
+
+char *read_gz(const char *path, size_t *len)
+{
+  char *in = NULL;
+  char buf[4096];
+  gzFile foo;
+  size_t ret;
+
+  if ((foo = gzopen (path, "rb")) == NULL)
+    return NULL;
+
+  *len = 0;
+  while ((ret = gzread (foo, buf, 4096)) > 0)
+    {
+      if ((in = realloc (in, *len + ret)) == NULL)
+	{
+	  fprintf (stderr, "Out of memory\n");
+	  exit (1);
+	}
+
+      memcpy (&in[*len], buf, ret);
+      *len += ret;
+    }
+  if (ret < 0)
+    {
+      int errnum;
+      const char *err;
+      err = gzerror (foo, &errnum);
+      if (errnum != 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 != 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;
+
+  if ((foo = gzopen ("tmpdir/one.gz", "wb")) == NULL)
+    goto write_gzerr;
+  if (ctf_gzwrite (fp, foo) < 0)
+    goto write_err;
+  gzclose (foo);
+
+  if ((foo = gzopen ("tmpdir/two.gz", "wb")) == NULL)
+    goto write_gzerr;
+  if (ctf_gzwrite (fp, foo) < 0)
+    goto write_err;
+  gzclose (foo);
+
+  if ((a = read_gz ("tmpdir/one.gz", &a_len)) == NULL)
+    goto read_err;
+
+  if ((b = read_gz ("tmpdir/two.gz", &b_len)) == NULL)
+    goto read_err;
+
+  if (a_len != b_len || memcmp (a, b, a_len) != 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 = 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)));
+
+  unlink ("tmpdir/two.gz");
+  if ((foo = gzopen ("tmpdir/two.gz", "wb")) == NULL)
+    goto write_gzerr;
+  if (ctf_gzwrite (fp, foo) < 0)
+    goto write_err;
+  gzclose (foo);
+
+  if ((b = read_gz ("tmpdir/two.gz", &b_len)) == NULL)
+    goto read_err;
+
+  if (memcmp (a, b, b_len) == 0)
+    {
+      fprintf (stderr, "gzwrites after adding types does not change the dict\n");
+      return 1;
+    }
+
+  free (a);
+  if ((fp_b = ctf_simple_open (b, b_len, NULL, 0, 0, NULL, 0, &err)) == NULL)
+    goto open_err;
+
+  if (ctf_type_reference (fp_b, ptrtype) == 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) != type)
+    fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp_b, ptrtype), type);
+
+  if (ctf_lookup_by_symbol_name (fp_b, "an_int") == CTF_ERR)
+    fprintf (stderr, "Lookup of symbol an_int failed: %s\n", ctf_errmsg (ctf_errno (fp_b)));
+
+  free (b);
+  ctf_dict_close (fp);
+  ctf_dict_close (fp_b);
+  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;
+ write_err: 
+  fprintf (stderr, "%s: cannot write: %s\n", argv[0], ctf_errmsg (ctf_errno (fp)));
+  return 1;
+ write_gzerr:
+  {
+    int errnum;
+    const char *err;
+
+    err = gzerror (foo, &errnum);
+    if (errnum != Z_ERRNO)
+      fprintf (stderr, "error gzwriting: %s\n", err);
+    else
+      fprintf (stderr, "error gzwriting: %s\n", strerror(errno));
+    return 1;
+  }
+ read_err: 
+  fprintf (stderr, "%s: cannot read\n", argv[0]);
+  return 1;
+}
diff --git a/libctf/testsuite/libctf-regression/gzrewrite.lk b/libctf/testsuite/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/testsuite/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 <ctf-api.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char *read_file(const char *path, size_t *len)
+{
+  char *in = NULL;
+  char buf[4096];
+  int foo;
+  size_t ret;
+
+  if ((foo = open (path, O_RDONLY)) < 0)
+    {
+      fprintf (stderr, "error opening %s: %s\n", path, strerror(errno));
+      exit (1);
+    }
+
+  *len = 0;
+  while ((ret = read (foo, buf, 4096)) > 0)
+    {
+      if ((in = realloc (in, *len + ret)) == NULL)
+	{
+	  fprintf (stderr, "Out of memory\n");
+	  exit (1);
+	}
+
+      memcpy (&in[*len], buf, ret);
+      *len += 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 != 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;
+
+  if ((foo = 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 = 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 = read_file ("tmpdir/one", &a_len);
+  b = read_file ("tmpdir/two", &b_len);
+
+  if (a_len != b_len || memcmp (a, b, a_len) != 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 = 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)));
+
+  unlink ("tmpdir/two");
+
+  if ((foo = 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 = read_file ("tmpdir/two", &b_len);
+
+  if (memcmp (a, b, b_len) == 0)
+    {
+      fprintf (stderr, "compress_writes after adding types does not change the dict\n");
+      return 1;
+    }
+
+  free (a);
+  free (b);
+
+  if ((ctf_b = ctf_open ("tmpdir/two", NULL, &err)) == NULL)
+    goto open_err;
+  if ((fp_b = ctf_dict_open (ctf_b, NULL, &err)) == NULL)
+    goto open_err;
+
+  if (ctf_type_reference (fp_b, ptrtype) == 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) != type)
+    fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp_b, ptrtype), type);
+
+  if (ctf_lookup_by_symbol_name (fp_b, "an_int") == CTF_ERR)
+    fprintf (stderr, "Lookup of symbol an_int failed: %s\n", ctf_errmsg (ctf_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;
+ 
+ open_err:
+  fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
+  return 1;
+ write_err: 
+  fprintf (stderr, "%s: cannot write: %s\n", argv[0], ctf_errmsg (ctf_errno (fp)));
+  return 1;
+ write_stderr:
+  fprintf (stderr, "%s: cannot open for writing: %s\n", argv[0], strerror (errno));
+  return 1;
+ read_err: 
+  fprintf (stderr, "%s: cannot read\n", argv[0]);
+  return 1;
+}
diff --git a/libctf/testsuite/libctf-regression/zrewrite.lk b/libctf/testsuite/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.
-- 
2.44.0.273.ge0bd14271f


  parent reply	other threads:[~2024-04-17 20:21 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-17 20:19 [PATCH libctf 00/22] more modifiable CTF dicts (and a few bugfixes) Nick Alcock
2024-04-17 20:19 ` [PATCH 01/22] binutils, objdump: Add --ctf-parent-section Nick Alcock
2024-04-18  2:05   ` Alan Modra
2024-04-18 13:06     ` Nick Alcock
2024-04-17 20:19 ` [PATCH 02/22] libctf: don't leak the symbol name in the name->type cache Nick Alcock
2024-04-17 20:19 ` [PATCH 03/22] libctf: remove static/dynamic name lookup distinction Nick Alcock
2024-04-17 20:20 ` [PATCH 04/22] libctf: fix name lookup in dicts containing base-type bitfields Nick Alcock
2024-04-17 20:20 ` [PATCH 05/22] libctf: support addition of types to dicts read via ctf_open() Nick Alcock
2024-04-17 20:20 ` [PATCH 06/22] libctf: fix a comment Nick Alcock
2024-04-17 20:20 ` [PATCH 07/22] libctf: delete LCTF_DIRTY Nick Alcock
2024-04-17 20:20 ` [PATCH 08/22] libctf: fix a comment typo Nick Alcock
2024-04-17 20:20 ` [PATCH 09/22] libctf: rename ctf_dict.ctf_{symtab,strtab} Nick Alcock
2024-04-17 20:20 ` [PATCH 10/22] Revert "libctf: do not corrupt strings across ctf_serialize" Nick Alcock
2024-04-17 20:20 ` [PATCH 11/22] libctf: replace 'pending refs' abstraction Nick Alcock
2024-04-17 20:20 ` [PATCH 12/22] libctf: rethink strtab writeout Nick Alcock
2024-04-17 20:20 ` [PATCH 13/22] libctf: make ctf_serialize() actually serialize Nick Alcock
2024-04-17 20:20 ` [PATCH 14/22] libctf: fix tiny dumping error Nick Alcock
2024-04-17 20:20 ` [PATCH 15/22] libctf: improve handling of type dumping errors Nick Alcock
2024-04-17 20:20 ` [PATCH 16/22] libctf: make ctf_lookup of symbols by name work in more cases Nick Alcock
2024-04-17 20:20 ` [PATCH 17/22] libctf: fix a debugging typo Nick Alcock
2024-04-17 20:20 ` Nick Alcock [this message]
2024-04-17 20:20 ` [PATCH 19/22] libctf: fix leak in test Nick Alcock
2024-04-17 20:20 ` [PATCH 20/22] libctf: don't pass errno into ctf_err_warn so often Nick Alcock
2024-04-17 20:20 ` [PATCH 21/22] libctf: Remove undefined functions from ver. map Nick Alcock
2024-04-17 20:20 ` [PATCH 22/22] libctf: do not include undefined functions in libctf.ver Nick Alcock
2024-04-19 15:51 ` [PATCH libctf 00/22] more modifiable CTF dicts (and a few bugfixes) Nick Alcock

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240417202018.34966-19-nick.alcock@oracle.com \
    --to=nick.alcock@oracle.com \
    --cc=binutils@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).