Skip to content

Commit cc77a20

Browse files
alan-maguireanakryiko
authored andcommitted
selftests/bpf: Test parsing of (multi-)split BTF
Write raw BTF to files, parse it and compare to original; this allows us to test parsing of (multi-)split BTF code. Signed-off-by: Alan Maguire <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4f596ac commit cc77a20

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

tools/testing/selftests/bpf/prog_tests/btf_split.c

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,45 @@ static void btf_dump_printf(void *ctx, const char *fmt, va_list args)
1212
vfprintf(ctx, fmt, args);
1313
}
1414

15+
/* Write raw BTF to file, return number of bytes written or negative errno */
16+
static ssize_t btf_raw_write(struct btf *btf, char *file)
17+
{
18+
ssize_t written = 0;
19+
const void *data;
20+
__u32 size = 0;
21+
int fd, ret;
22+
23+
fd = mkstemp(file);
24+
if (!ASSERT_GE(fd, 0, "create_file"))
25+
return -errno;
26+
27+
data = btf__raw_data(btf, &size);
28+
if (!ASSERT_OK_PTR(data, "btf__raw_data")) {
29+
close(fd);
30+
return -EINVAL;
31+
}
32+
while (written < size) {
33+
ret = write(fd, data + written, size - written);
34+
if (!ASSERT_GE(ret, 0, "write succeeded")) {
35+
close(fd);
36+
return -errno;
37+
}
38+
written += ret;
39+
}
40+
close(fd);
41+
return written;
42+
}
43+
1544
static void __test_btf_split(bool multi)
1645
{
46+
char multisplit_btf_file[] = "/tmp/test_btf_multisplit.XXXXXX";
47+
char split_btf_file[] = "/tmp/test_btf_split.XXXXXX";
48+
char base_btf_file[] = "/tmp/test_btf_base.XXXXXX";
49+
ssize_t multisplit_btf_sz = 0, split_btf_sz = 0, base_btf_sz = 0;
1750
struct btf_dump *d = NULL;
18-
const struct btf_type *t;
19-
struct btf *btf1, *btf2, *btf3 = NULL;
51+
const struct btf_type *t, *ot;
52+
struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL;
53+
struct btf *btf4 = NULL, *btf5 = NULL, *btf6 = NULL;
2054
int str_off, i, err;
2155

2256
btf1 = btf__new_empty();
@@ -123,6 +157,45 @@ static void __test_btf_split(bool multi)
123157
" int uf2;\n"
124158
"};\n\n", "c_dump");
125159

160+
/* write base, split BTFs to files and ensure parsing succeeds */
161+
base_btf_sz = btf_raw_write(btf1, base_btf_file);
162+
if (base_btf_sz < 0)
163+
goto cleanup;
164+
split_btf_sz = btf_raw_write(btf2, split_btf_file);
165+
if (split_btf_sz < 0)
166+
goto cleanup;
167+
btf4 = btf__parse(base_btf_file, NULL);
168+
if (!ASSERT_OK_PTR(btf4, "parse_base"))
169+
goto cleanup;
170+
btf5 = btf__parse_split(split_btf_file, btf4);
171+
if (!ASSERT_OK_PTR(btf5, "parse_split"))
172+
goto cleanup;
173+
if (multi) {
174+
multisplit_btf_sz = btf_raw_write(btf3, multisplit_btf_file);
175+
if (multisplit_btf_sz < 0)
176+
goto cleanup;
177+
btf6 = btf__parse_split(multisplit_btf_file, btf5);
178+
if (!ASSERT_OK_PTR(btf6, "parse_multisplit"))
179+
goto cleanup;
180+
} else {
181+
btf6 = btf5;
182+
}
183+
184+
if (!ASSERT_EQ(btf__type_cnt(btf3), btf__type_cnt(btf6), "cmp_type_cnt"))
185+
goto cleanup;
186+
187+
/* compare parsed to original BTF */
188+
for (i = 1; i < btf__type_cnt(btf6); i++) {
189+
t = btf__type_by_id(btf6, i);
190+
if (!ASSERT_OK_PTR(t, "type_in_parsed_btf"))
191+
goto cleanup;
192+
ot = btf__type_by_id(btf3, i);
193+
if (!ASSERT_OK_PTR(ot, "type_in_orig_btf"))
194+
goto cleanup;
195+
if (!ASSERT_EQ(memcmp(t, ot, sizeof(*ot)), 0, "cmp_parsed_orig_btf"))
196+
goto cleanup;
197+
}
198+
126199
cleanup:
127200
if (dump_buf_file)
128201
fclose(dump_buf_file);
@@ -132,6 +205,16 @@ static void __test_btf_split(bool multi)
132205
btf__free(btf2);
133206
if (btf2 != btf3)
134207
btf__free(btf3);
208+
btf__free(btf4);
209+
btf__free(btf5);
210+
if (btf5 != btf6)
211+
btf__free(btf6);
212+
if (base_btf_sz > 0)
213+
unlink(base_btf_file);
214+
if (split_btf_sz > 0)
215+
unlink(split_btf_file);
216+
if (multisplit_btf_sz > 0)
217+
unlink(multisplit_btf_file);
135218
}
136219

137220
void test_btf_split(void)

0 commit comments

Comments
 (0)