Debian Codegraph @

libctf/testsuite/libctf-writable/ctf-nonroot-linking.c

raw
1 /* Make sure linking a non-root-visible type emits a non-root-visible
2 type, rather than silently promoting it to root-visible. Do it by dumping,
3 thus also testing the {non-root sigils} you get when dumping
4 non-root-visible types. */
5
6 #include <ctf-api.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 int
12 main (int argc, char *argv[])
13 {
14 ctf_dict_t *in1;
15 ctf_dict_t *fp;
16 ctf_dict_t *dump_fp;
17 ctf_archive_t *arc1;
18 ctf_archive_t *final_arc;
19 ctf_sect_t s;
20 ctf_encoding_t encoding = { CTF_INT_SIGNED, 0, sizeof (char) };
21 unsigned char *buf1, *buf2;
22 size_t buf1_sz, buf2_sz;
23 ctf_dump_state_t *dump_state = NULL;
24 ctf_next_t *i = NULL;
25 int err;
26
27 if ((fp = ctf_create (&err)) == NULL)
28 goto create_err;
29
30 if ((in1 = ctf_create (&err)) == NULL)
31 goto create_err;
32
33 /* A non-root addition. */
34
35 if ((ctf_add_integer (in1, CTF_ADD_NONROOT, "foo", &encoding)) == CTF_ERR)
36 {
37 fprintf (stderr, "Cannot add: %s\n", ctf_errmsg (ctf_errno (in1)));
38 return 1;
39 }
40
41 /* Write it out and read it back in, to turn it into an archive.
42 This would be unnecessary if ctf_link_add() were public :( */
43 if ((buf1 = ctf_write_mem (in1, &buf1_sz, -1)) == NULL)
44 {
45 fprintf (stderr, "Cannot serialize: %s\n", ctf_errmsg (ctf_errno (in1)));
46 return 1;
47 }
48
49 s.cts_name = "foo";
50 s.cts_data = (void *) buf1;
51 s.cts_size = buf1_sz;
52 s.cts_entsize = 64; /* Unimportant. */
53
54 if ((arc1 = ctf_arc_bufopen (&s, NULL, NULL, &err)) == NULL)
55 goto open_err;
56
57 ctf_dict_close (in1);
58
59 /* Link! Even a one-file link does deduplication. */
60
61 if (ctf_link_add_ctf (fp, arc1, "a") < 0)
62 goto link_err;
63
64 if (ctf_link (fp, 0) < 0)
65 goto link_err;
66
67 /* Write it out. We need a new buf here, because the archive is still
68 using the other buf. */
69
70 if ((buf2 = ctf_link_write (fp, &buf2_sz, 4096)) == NULL)
71 goto link_err;
72
73 /* Read it back in. */
74
75 s.cts_data = (void *) buf2;
76 s.cts_size = buf2_sz;
77
78 if ((final_arc = ctf_arc_bufopen (&s, NULL, NULL, &err)) == NULL)
79 goto open_err;
80
81 /* Dump the types, and search for the {sigils of non-rootedness}. */
82 while ((dump_fp = ctf_archive_next (final_arc, &i, NULL, 0, &err)) != NULL)
83 {
84 char *dumpstr;
85
86 while ((dumpstr = ctf_dump (dump_fp, &dump_state, CTF_SECT_TYPE,
87 NULL, NULL)) != NULL)
88 {
89 if (strchr (dumpstr, '{') != NULL && strchr (dumpstr, '}') != NULL)
90 printf ("Non-root type found.\n");
91 free (dumpstr);
92 }
93 ctf_dict_close (dump_fp);
94 }
95 if (err != ECTF_NEXT_END)
96 {
97 fprintf (stderr, "Archive iteration error: %s\n", ctf_errmsg (err));
98 return 1;
99 }
100
101 ctf_arc_close (final_arc);
102 free (buf1);
103 free (buf2);
104 ctf_dict_close (fp);
105 return 0;
106
107 create_err:
108 fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
109 return 1;
110 open_err:
111 fprintf (stderr, "Cannot open: %s\n", ctf_errmsg (err));
112 return 1;
113 link_err:
114 fprintf (stderr, "Cannot link: %s\n", ctf_errmsg (ctf_errno (fp)));
115 return 1;
116 }