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 (CHECK(fd < 0, "create_tmp", "failed to create file: %d\n", fd)) {
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 void test_btf_dump() {
133 	int i;
134 
135 	for (i = 0; i < ARRAY_SIZE(btf_dump_test_cases); i++) {
136 		struct btf_dump_test_case *t = &btf_dump_test_cases[i];
137 
138 		if (!test__start_subtest(t->name))
139 			continue;
140 
141 		test_btf_dump_case(i, &btf_dump_test_cases[i]);
142 	}
143 }
144