Skip to content

Commit 02f5e7c

Browse files
alan-maguireanakryiko
authored andcommitted
selftests/bpf: Test multi-split BTF
Extend split BTF test to cover case where we create split BTF on top of existing split BTF and add info to it; ensure that such BTF can be created and handled by searching within it, dumping/comparing to expected. Signed-off-by: Alan Maguire <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4e29128 commit 02f5e7c

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

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

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

15-
void test_btf_split() {
15+
static void __test_btf_split(bool multi)
16+
{
1617
struct btf_dump *d = NULL;
1718
const struct btf_type *t;
18-
struct btf *btf1, *btf2;
19+
struct btf *btf1, *btf2, *btf3 = NULL;
1920
int str_off, i, err;
2021

2122
btf1 = btf__new_empty();
@@ -63,14 +64,46 @@ void test_btf_split() {
6364
ASSERT_EQ(btf_vlen(t), 3, "split_struct_vlen");
6465
ASSERT_STREQ(btf__str_by_offset(btf2, t->name_off), "s2", "split_struct_name");
6566

67+
if (multi) {
68+
btf3 = btf__new_empty_split(btf2);
69+
if (!ASSERT_OK_PTR(btf3, "multi_split_btf"))
70+
goto cleanup;
71+
} else {
72+
btf3 = btf2;
73+
}
74+
75+
btf__add_union(btf3, "u1", 16); /* [5] union u1 { */
76+
btf__add_field(btf3, "f1", 4, 0, 0); /* struct s2 f1; */
77+
btf__add_field(btf3, "uf2", 1, 0, 0); /* int f2; */
78+
/* } */
79+
80+
if (multi) {
81+
t = btf__type_by_id(btf2, 5);
82+
ASSERT_NULL(t, "multisplit_type_in_first_split");
83+
}
84+
85+
t = btf__type_by_id(btf3, 5);
86+
if (!ASSERT_OK_PTR(t, "split_union_type"))
87+
goto cleanup;
88+
ASSERT_EQ(btf_is_union(t), true, "split_union_kind");
89+
ASSERT_EQ(btf_vlen(t), 2, "split_union_vlen");
90+
ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "u1", "split_union_name");
91+
ASSERT_EQ(btf__type_cnt(btf3), 6, "split_type_cnt");
92+
93+
t = btf__type_by_id(btf3, 1);
94+
if (!ASSERT_OK_PTR(t, "split_base_type"))
95+
goto cleanup;
96+
ASSERT_EQ(btf_is_int(t), true, "split_base_int");
97+
ASSERT_STREQ(btf__str_by_offset(btf3, t->name_off), "int", "split_base_type_name");
98+
6699
/* BTF-to-C dump of split BTF */
67100
dump_buf_file = open_memstream(&dump_buf, &dump_buf_sz);
68101
if (!ASSERT_OK_PTR(dump_buf_file, "dump_memstream"))
69102
return;
70-
d = btf_dump__new(btf2, btf_dump_printf, dump_buf_file, NULL);
103+
d = btf_dump__new(btf3, btf_dump_printf, dump_buf_file, NULL);
71104
if (!ASSERT_OK_PTR(d, "btf_dump__new"))
72105
goto cleanup;
73-
for (i = 1; i < btf__type_cnt(btf2); i++) {
106+
for (i = 1; i < btf__type_cnt(btf3); i++) {
74107
err = btf_dump__dump_type(d, i);
75108
ASSERT_OK(err, "dump_type_ok");
76109
}
@@ -79,12 +112,15 @@ void test_btf_split() {
79112
ASSERT_STREQ(dump_buf,
80113
"struct s1 {\n"
81114
" int f1;\n"
82-
"};\n"
83-
"\n"
115+
"};\n\n"
84116
"struct s2 {\n"
85117
" struct s1 f1;\n"
86118
" int f2;\n"
87119
" int *f3;\n"
120+
"};\n\n"
121+
"union u1 {\n"
122+
" struct s2 f1;\n"
123+
" int uf2;\n"
88124
"};\n\n", "c_dump");
89125

90126
cleanup:
@@ -94,4 +130,14 @@ void test_btf_split() {
94130
btf_dump__free(d);
95131
btf__free(btf1);
96132
btf__free(btf2);
133+
if (btf2 != btf3)
134+
btf__free(btf3);
135+
}
136+
137+
void test_btf_split(void)
138+
{
139+
if (test__start_subtest("single_split"))
140+
__test_btf_split(false);
141+
if (test__start_subtest("multi_split"))
142+
__test_btf_split(true);
97143
}

0 commit comments

Comments
 (0)