xref: /linux/tools/testing/selftests/bpf/prog_tests/btf_kind.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2026, Oracle and/or its affiliates. */
3 
4 #include <test_progs.h>
5 #include <bpf/btf.h>
6 #include <bpf/libbpf.h>
7 
8 /* Verify kind encoding exists for each kind */
9 static void test_btf_kind_encoding(void)
10 {
11 	LIBBPF_OPTS(btf_new_opts, opts);
12 	const struct btf_header *hdr;
13 	const void *raw_btf;
14 	struct btf *btf;
15 	__u32 raw_size;
16 
17 	opts.add_layout = true;
18 	btf = btf__new_empty_opts(&opts);
19 	if (!ASSERT_OK_PTR(btf, "btf_new"))
20 		return;
21 
22 	raw_btf = btf__raw_data(btf, &raw_size);
23 	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
24 		return;
25 
26 	hdr = raw_btf;
27 
28 	ASSERT_EQ(hdr->layout_off % 4, 0, "layout_aligned");
29 	ASSERT_EQ(hdr->layout_len, sizeof(struct btf_layout) * NR_BTF_KINDS,
30 		  "layout_len");
31 	ASSERT_EQ(hdr->str_off, hdr->layout_off + hdr->layout_len, "str_after_layout");
32 	btf__free(btf);
33 
34 	opts.add_layout = false;
35 	btf = btf__new_empty_opts(&opts);
36 	if (!ASSERT_OK_PTR(btf, "btf_new"))
37 		return;
38 
39 	raw_btf = btf__raw_data(btf, &raw_size);
40 	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
41 		return;
42 
43 	hdr = raw_btf;
44 
45 	ASSERT_EQ(hdr->layout_off, 0, "no_layout_off");
46 	ASSERT_EQ(hdr->layout_len, 0, "no_layout_len");
47 	ASSERT_EQ(hdr->str_off, hdr->type_off + hdr->type_len, "strs_after_types");
48 	btf__free(btf);
49 }
50 
51 static int write_raw_btf(void *raw_btf, size_t raw_size, char *file)
52 {
53 	int fd = mkstemp(file);
54 	ssize_t n;
55 
56 	if (!ASSERT_OK_FD(fd, "open_raw_btf"))
57 		return -1;
58 	n = write(fd, raw_btf, raw_size);
59 	close(fd);
60 	if (!ASSERT_EQ(n, (ssize_t)raw_size, "write_raw_btf"))
61 		return -1;
62 	return 0;
63 }
64 
65 /*
66  * Fabricate an unrecognized kind at BTF_KIND_MAX + 1, and after adding
67  * the appropriate struct/typedefs to the BTF such that it recognizes
68  * this kind, ensure that parsing of BTF containing the unrecognized kind
69  * can succeed.
70  */
71 void test_btf_kind_decoding(void)
72 {
73 	char btf_kind_file1[] = "/tmp/test_btf_kind.XXXXXX";
74 	char btf_kind_file2[] = "/tmp/test_btf_kind.XXXXXX";
75 	char btf_kind_file3[] = "/tmp/test_btf_kind.XXXXXX";
76 	struct btf *btf = NULL, *new_btf = NULL;
77 	__s32 int_id, unrec_id, id, id2;
78 	LIBBPF_OPTS(btf_new_opts, opts);
79 	struct btf_layout *l;
80 	struct btf_header *hdr;
81 	const void *raw_btf;
82 	struct btf_type *t;
83 	void *new_raw_btf;
84 	void *str_data;
85 	__u32 raw_size;
86 
87 	opts.add_layout = true;
88 	btf = btf__new_empty_opts(&opts);
89 	if (!ASSERT_OK_PTR(btf, "btf_new"))
90 		return;
91 
92 	int_id = btf__add_int(btf, "test_char", 1, BTF_INT_CHAR);
93 	if (!ASSERT_GT(int_id, 0, "add_int_id"))
94 		return;
95 
96 	/*
97 	 * Create our type with unrecognized kind by adding a typedef kind
98 	 * we will overwrite it with our unrecognized kind value.
99 	 */
100 	unrec_id = btf__add_typedef(btf, "unrec_kind", int_id);
101 	if (!ASSERT_GT(unrec_id, 0, "add_unrec_id"))
102 		return;
103 
104 	/*
105 	 * Add an id after it that we will look up to verify we can parse
106 	 * beyond unrecognized kinds.
107 	 */
108 	id = btf__add_typedef(btf, "test_lookup", int_id);
109 	if (!ASSERT_GT(id, 0, "add_test_lookup_id"))
110 		return;
111 	id2 = btf__add_typedef(btf, "test_lookup2", int_id);
112 	if (!ASSERT_GT(id2, 0, "add_test_lookup_id2"))
113 		return;
114 
115 	raw_btf = (void *)btf__raw_data(btf, &raw_size);
116 	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
117 		return;
118 
119 	new_raw_btf = calloc(1, raw_size + sizeof(*l));
120 	if (!ASSERT_OK_PTR(new_raw_btf, "calloc_raw_btf"))
121 		return;
122 	memcpy(new_raw_btf, raw_btf, raw_size);
123 
124 	hdr = new_raw_btf;
125 
126 	/* Move strings to make space for one new layout description */
127 	raw_size += sizeof(*l);
128 	str_data = new_raw_btf + hdr->hdr_len + hdr->str_off;
129 	memmove(str_data + sizeof(*l), str_data, hdr->str_len);
130 	hdr->str_off += sizeof(*l);
131 
132 	/* Add new layout description */
133 	hdr->layout_len += sizeof(*l);
134 	l = new_raw_btf + hdr->hdr_len + hdr->layout_off;
135 	l[NR_BTF_KINDS].info_sz = 0;
136 	l[NR_BTF_KINDS].elem_sz = 0;
137 	l[NR_BTF_KINDS].flags = 0;
138 
139 	/* Now modify typedef added above to be an unrecognized kind. */
140 	t = (void *)hdr + hdr->hdr_len + hdr->type_off + sizeof(struct btf_type) +
141 		sizeof(__u32);
142 	t->info = (NR_BTF_KINDS << 24);
143 
144 	/* Write BTF to a raw file, ready for parsing. */
145 	if (write_raw_btf(new_raw_btf, raw_size, btf_kind_file1))
146 		goto out;
147 
148 	/*
149 	 * Verify parsing succeeds, and that we can read type info past
150 	 * the unrecognized kind.
151 	 */
152 	new_btf = btf__parse_raw(btf_kind_file1);
153 	if (ASSERT_OK_PTR(new_btf, "btf__parse_raw")) {
154 		ASSERT_EQ(btf__find_by_name(new_btf, "unrec_kind"), unrec_id,
155 			  "unrec_kind_found");
156 		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup",
157 						 BTF_KIND_TYPEDEF), id,
158 			  "verify_id_lookup");
159 		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup2",
160 						 BTF_KIND_TYPEDEF), id2,
161 			  "verify_id2_lookup");
162 	}
163 	btf__free(new_btf);
164 	new_btf = NULL;
165 
166 	/*
167 	 * Next, change info_sz to equal sizeof(struct btf_type); this means the
168 	 * "test_lookup" kind will be reinterpreted as a singular info element
169 	 * following the unrecognized kind.
170 	 */
171 	l[NR_BTF_KINDS].info_sz = sizeof(struct btf_type);
172 	if (write_raw_btf(new_raw_btf, raw_size, btf_kind_file2))
173 		goto out;
174 
175 	new_btf = btf__parse_raw(btf_kind_file2);
176 	if (ASSERT_OK_PTR(new_btf, "btf__parse_raw")) {
177 		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup",
178 						 BTF_KIND_TYPEDEF), -ENOENT,
179 			  "verify_id_not_found");
180 		/* id of "test_lookup2" will be id2 -1 as we have removed one type */
181 		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup2",
182 						 BTF_KIND_TYPEDEF), id2 - 1,
183 			  "verify_id_lookup2");
184 
185 	}
186 	btf__free(new_btf);
187 	new_btf = NULL;
188 
189 	/*
190 	 * Change elem_sz to equal sizeof(struct btf_type) and set vlen
191 	 * associated with unrecognized type to 1; this allows us to verify
192 	 * vlen-specified BTF can still be parsed.
193 	 */
194 	l[NR_BTF_KINDS].info_sz = 0;
195 	l[NR_BTF_KINDS].elem_sz = sizeof(struct btf_type);
196 	t->info |= 1;
197 	if (write_raw_btf(new_raw_btf, raw_size, btf_kind_file3))
198 		goto out;
199 
200 	new_btf = btf__parse_raw(btf_kind_file3);
201 	if (ASSERT_OK_PTR(new_btf, "btf__parse_raw")) {
202 		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup",
203 						 BTF_KIND_TYPEDEF), -ENOENT,
204 			  "verify_id_not_found");
205 		/* id of "test_lookup2" will be id2 -1 as we have removed one type */
206 		ASSERT_EQ(btf__find_by_name_kind(new_btf, "test_lookup2",
207 						 BTF_KIND_TYPEDEF), id2 - 1,
208 			  "verify_id_lookup2");
209 
210 	}
211 out:
212 	btf__free(new_btf);
213 	free(new_raw_btf);
214 	unlink(btf_kind_file1);
215 	unlink(btf_kind_file2);
216 	unlink(btf_kind_file3);
217 	btf__free(btf);
218 }
219 
220 void test_btf_kind(void)
221 {
222 	if (test__start_subtest("btf_kind_encoding"))
223 		test_btf_kind_encoding();
224 	if (test__start_subtest("btf_kind_decoding"))
225 		test_btf_kind_decoding();
226 }
227