xref: /linux/tools/testing/selftests/bpf/prog_tests/btf_dump.c (revision 172cdcaefea5c297fdb3d20b7d5aff60ae4fbce6)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <bpf/btf.h>
4 
5 static int duration = 0;
6 
7 void btf_dump_printf(void *ctx, const char *fmt, va_list args)
8 {
9 	vfprintf(ctx, fmt, args);
10 }
11 
12 static struct btf_dump_test_case {
13 	const char *name;
14 	const char *file;
15 	bool known_ptr_sz;
16 	struct btf_dump_opts opts;
17 } btf_dump_test_cases[] = {
18 	{"btf_dump: syntax", "btf_dump_test_case_syntax", true, {}},
19 	{"btf_dump: ordering", "btf_dump_test_case_ordering", false, {}},
20 	{"btf_dump: padding", "btf_dump_test_case_padding", true, {}},
21 	{"btf_dump: packing", "btf_dump_test_case_packing", true, {}},
22 	{"btf_dump: bitfields", "btf_dump_test_case_bitfields", true, {}},
23 	{"btf_dump: multidim", "btf_dump_test_case_multidim", false, {}},
24 	{"btf_dump: namespacing", "btf_dump_test_case_namespacing", false, {}},
25 };
26 
27 static int btf_dump_all_types(const struct btf *btf,
28 			      const struct btf_dump_opts *opts)
29 {
30 	size_t type_cnt = btf__get_nr_types(btf);
31 	struct btf_dump *d;
32 	int err = 0, id;
33 
34 	d = btf_dump__new(btf, NULL, opts, btf_dump_printf);
35 	if (IS_ERR(d))
36 		return PTR_ERR(d);
37 
38 	for (id = 1; id <= type_cnt; id++) {
39 		err = btf_dump__dump_type(d, id);
40 		if (err)
41 			goto done;
42 	}
43 
44 done:
45 	btf_dump__free(d);
46 	return err;
47 }
48 
49 static int test_btf_dump_case(int n, struct btf_dump_test_case *t)
50 {
51 	char test_file[256], out_file[256], diff_cmd[1024];
52 	struct btf *btf = NULL;
53 	int err = 0, fd = -1;
54 	FILE *f = NULL;
55 
56 	snprintf(test_file, sizeof(test_file), "%s.o", t->file);
57 
58 	btf = btf__parse_elf(test_file, NULL);
59 	if (CHECK(IS_ERR(btf), "btf_parse_elf",
60 	    "failed to load test BTF: %ld\n", PTR_ERR(btf))) {
61 		err = -PTR_ERR(btf);
62 		btf = NULL;
63 		goto done;
64 	}
65 
66 	/* tests with t->known_ptr_sz have no "long" or "unsigned long" type,
67 	 * so it's impossible to determine correct pointer size; but if they
68 	 * do, it should be 8 regardless of host architecture, becaues BPF
69 	 * target is always 64-bit
70 	 */
71 	if (!t->known_ptr_sz) {
72 		btf__set_pointer_size(btf, 8);
73 	} else {
74 		CHECK(btf__pointer_size(btf) != 8, "ptr_sz", "exp %d, got %zu\n",
75 		      8, btf__pointer_size(btf));
76 	}
77 
78 	snprintf(out_file, sizeof(out_file), "/tmp/%s.output.XXXXXX", t->file);
79 	fd = mkstemp(out_file);
80 	if (!ASSERT_GE(fd, 0, "create_tmp")) {
81 		err = fd;
82 		goto done;
83 	}
84 	f = fdopen(fd, "w");
85 	if (CHECK(f == NULL, "open_tmp",  "failed to open file: %s(%d)\n",
86 		  strerror(errno), errno)) {
87 		close(fd);
88 		goto done;
89 	}
90 
91 	t->opts.ctx = f;
92 	err = btf_dump_all_types(btf, &t->opts);
93 	fclose(f);
94 	close(fd);
95 	if (CHECK(err, "btf_dump", "failure during C dumping: %d\n", err)) {
96 		goto done;
97 	}
98 
99 	snprintf(test_file, sizeof(test_file), "progs/%s.c", t->file);
100 	if (access(test_file, R_OK) == -1)
101 		/*
102 		 * When the test is run with O=, kselftest copies TEST_FILES
103 		 * without preserving the directory structure.
104 		 */
105 		snprintf(test_file, sizeof(test_file), "%s.c", t->file);
106 	/*
107 	 * Diff test output and expected test output, contained between
108 	 * START-EXPECTED-OUTPUT and END-EXPECTED-OUTPUT lines in test case.
109 	 * For expected output lines, everything before '*' is stripped out.
110 	 * Also lines containing comment start and comment end markers are
111 	 * ignored.
112 	 */
113 	snprintf(diff_cmd, sizeof(diff_cmd),
114 		 "awk '/START-EXPECTED-OUTPUT/{out=1;next} "
115 		 "/END-EXPECTED-OUTPUT/{out=0} "
116 		 "/\\/\\*|\\*\\//{next} " /* ignore comment start/end lines */
117 		 "out {sub(/^[ \\t]*\\*/, \"\"); print}' '%s' | diff -u - '%s'",
118 		 test_file, out_file);
119 	err = system(diff_cmd);
120 	if (CHECK(err, "diff",
121 		  "differing test output, output=%s, err=%d, diff cmd:\n%s\n",
122 		  out_file, err, diff_cmd))
123 		goto done;
124 
125 	remove(out_file);
126 
127 done:
128 	btf__free(btf);
129 	return err;
130 }
131 
132 static char *dump_buf;
133 static size_t dump_buf_sz;
134 static FILE *dump_buf_file;
135 
136 void test_btf_dump_incremental(void)
137 {
138 	struct btf *btf = NULL;
139 	struct btf_dump *d = NULL;
140 	struct btf_dump_opts opts;
141 	int id, err, i;
142 
143 	dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
144 	if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
145 		return;
146 	btf = btf__new_empty();
147 	if (!ASSERT_OK_PTR(btf, "new_empty"))
148 		goto err_out;
149 	opts.ctx = dump_buf_file;
150 	d = btf_dump__new(btf, NULL, &opts, btf_dump_printf);
151 	if (!ASSERT_OK(libbpf_get_error(d), "btf_dump__new"))
152 		goto err_out;
153 
154 	/* First, generate BTF corresponding to the following C code:
155 	 *
156 	 * enum { VAL = 1 };
157 	 *
158 	 * struct s { int x; };
159 	 *
160 	 */
161 	id = btf__add_enum(btf, NULL, 4);
162 	ASSERT_EQ(id, 1, "enum_id");
163 	err = btf__add_enum_value(btf, "VAL", 1);
164 	ASSERT_OK(err, "enum_val_ok");
165 
166 	id = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);
167 	ASSERT_EQ(id, 2, "int_id");
168 
169 	id = btf__add_struct(btf, "s", 4);
170 	ASSERT_EQ(id, 3, "struct_id");
171 	err = btf__add_field(btf, "x", 2, 0, 0);
172 	ASSERT_OK(err, "field_ok");
173 
174 	for (i = 1; i <= btf__get_nr_types(btf); i++) {
175 		err = btf_dump__dump_type(d, i);
176 		ASSERT_OK(err, "dump_type_ok");
177 	}
178 
179 	fflush(dump_buf_file);
180 	dump_buf[dump_buf_sz] = 0; /* some libc implementations don't do this */
181 	ASSERT_STREQ(dump_buf,
182 "enum {\n"
183 "	VAL = 1,\n"
184 "};\n"
185 "\n"
186 "struct s {\n"
187 "	int x;\n"
188 "};\n\n", "c_dump1");
189 
190 	/* Now, after dumping original BTF, append another struct that embeds
191 	 * anonymous enum. It also has a name conflict with the first struct:
192 	 *
193 	 * struct s___2 {
194 	 *     enum { VAL___2 = 1 } x;
195 	 *     struct s s;
196 	 * };
197 	 *
198 	 * This will test that btf_dump'er maintains internal state properly.
199 	 * Note that VAL___2 enum value. It's because we've already emitted
200 	 * that enum as a global anonymous enum, so btf_dump will ensure that
201 	 * enum values don't conflict;
202 	 *
203 	 */
204 	fseek(dump_buf_file, 0, SEEK_SET);
205 
206 	id = btf__add_struct(btf, "s", 4);
207 	ASSERT_EQ(id, 4, "struct_id");
208 	err = btf__add_field(btf, "x", 1, 0, 0);
209 	ASSERT_OK(err, "field_ok");
210 	err = btf__add_field(btf, "s", 3, 32, 0);
211 	ASSERT_OK(err, "field_ok");
212 
213 	for (i = 1; i <= btf__get_nr_types(btf); i++) {
214 		err = btf_dump__dump_type(d, i);
215 		ASSERT_OK(err, "dump_type_ok");
216 	}
217 
218 	fflush(dump_buf_file);
219 	dump_buf[dump_buf_sz] = 0; /* some libc implementations don't do this */
220 	ASSERT_STREQ(dump_buf,
221 "struct s___2 {\n"
222 "	enum {\n"
223 "		VAL___2 = 1,\n"
224 "	} x;\n"
225 "	struct s s;\n"
226 "};\n\n" , "c_dump1");
227 
228 err_out:
229 	fclose(dump_buf_file);
230 	free(dump_buf);
231 	btf_dump__free(d);
232 	btf__free(btf);
233 }
234 
235 void test_btf_dump() {
236 	int i;
237 
238 	for (i = 0; i < ARRAY_SIZE(btf_dump_test_cases); i++) {
239 		struct btf_dump_test_case *t = &btf_dump_test_cases[i];
240 
241 		if (!test__start_subtest(t->name))
242 			continue;
243 
244 		test_btf_dump_case(i, &btf_dump_test_cases[i]);
245 	}
246 	if (test__start_subtest("btf_dump: incremental"))
247 		test_btf_dump_incremental();
248 }
249