Skip to content

Commit 4cb4897

Browse files
committed
Merge branch 'multi-split-btf-fixes-and-test'
Alan Maguire says: ==================== Multi-split BTF fixes and test This small series consists of a fix to multi-split BTF parsing (patch 1) and a test which exercises (multi-)split BTF parsing (patch 2). Changes since v3 [1] - add asserts to ensure number of types in original and parsed BTF are identical, and the calls to btf__type_by_id() return valid pointers (code review bot, patch 2) Changes since v2 [2] - fix Fixes: tag formatting (Andrii, patch 1) - BPF code-review bot saw we were doing ASSERT_OK_PTR() on wrong BTF (not multisplit) in patch 2 - ensure cleanup is correctly handled for BTF, unlink in split tests (Andrii, patch 2) Changes since v1 [3] - BPF code-review bot spotted another place that the string offset needed to be adjusted based upon base start string offset + header string offset. - added selftests to extend split BTF testing to parsing [1] https://lore.kernel.org/bpf/[email protected]/ [2] https://lore.kernel.org/bpf/[email protected]/ [3] https://lore.kernel.org/bpf/[email protected]/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents b3387b3 + cc77a20 commit 4cb4897

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

tools/lib/bpf/btf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ static struct btf *btf_new(const void *data, __u32 size, struct btf *base_btf, b
10611061
if (base_btf) {
10621062
btf->base_btf = base_btf;
10631063
btf->start_id = btf__type_cnt(base_btf);
1064-
btf->start_str_off = base_btf->hdr->str_len;
1064+
btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
10651065
}
10661066

10671067
if (is_mmap) {
@@ -5818,7 +5818,7 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
58185818
{
58195819
btf->base_btf = (struct btf *)base_btf;
58205820
btf->start_id = btf__type_cnt(base_btf);
5821-
btf->start_str_off = base_btf->hdr->str_len;
5821+
btf->start_str_off = base_btf->hdr->str_len + base_btf->start_str_off;
58225822
}
58235823

58245824
int btf__relocate(struct btf *btf, const struct btf *base_btf)

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)