Skip to content

Commit 556a399

Browse files
Pu Lehuianakryiko
authored andcommitted
selftests/bpf: Add distilled BTF test about marking BTF_IS_EMBEDDED
When redirecting the split BTF to the vmlinux base BTF, we need to mark the distilled base struct/union members of split BTF structs/unions in id_map with BTF_IS_EMBEDDED. This indicates that these types must match both name and size later. So if a needed composite type, which is the member of composite type in the split BTF, has a different size in the base BTF we wish to relocate with, btf__relocate() should error out. Signed-off-by: Pu Lehui <pulehui@huawei.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20250115100241.4171581-4-pulehui@huaweicloud.com
1 parent 5ca681a commit 556a399

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,76 @@ static void test_distilled_endianness(void)
601601
btf__free(base);
602602
}
603603

604+
/* If a needed composite type, which is the member of composite type
605+
* in the split BTF, has a different size in the base BTF we wish to
606+
* relocate with, btf__relocate() should error out.
607+
*/
608+
static void test_distilled_base_embedded_err(void)
609+
{
610+
struct btf *btf1 = NULL, *btf2 = NULL, *btf3 = NULL, *btf4 = NULL, *btf5 = NULL;
611+
612+
btf1 = btf__new_empty();
613+
if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
614+
return;
615+
616+
btf__add_int(btf1, "int", 4, BTF_INT_SIGNED); /* [1] int */
617+
btf__add_struct(btf1, "s1", 4); /* [2] struct s1 { */
618+
btf__add_field(btf1, "f1", 1, 0, 0); /* int f1; */
619+
/* } */
620+
VALIDATE_RAW_BTF(
621+
btf1,
622+
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
623+
"[2] STRUCT 's1' size=4 vlen=1\n"
624+
"\t'f1' type_id=1 bits_offset=0");
625+
626+
btf2 = btf__new_empty_split(btf1);
627+
if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
628+
goto cleanup;
629+
630+
btf__add_struct(btf2, "with_embedded", 8); /* [3] struct with_embedded { */
631+
btf__add_field(btf2, "e1", 2, 0, 0); /* struct s1 e1; */
632+
/* } */
633+
634+
VALIDATE_RAW_BTF(
635+
btf2,
636+
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
637+
"[2] STRUCT 's1' size=4 vlen=1\n"
638+
"\t'f1' type_id=1 bits_offset=0",
639+
"[3] STRUCT 'with_embedded' size=8 vlen=1\n"
640+
"\t'e1' type_id=2 bits_offset=0");
641+
642+
if (!ASSERT_EQ(0, btf__distill_base(btf2, &btf3, &btf4),
643+
"distilled_base") ||
644+
!ASSERT_OK_PTR(btf3, "distilled_base") ||
645+
!ASSERT_OK_PTR(btf4, "distilled_split") ||
646+
!ASSERT_EQ(2, btf__type_cnt(btf3), "distilled_base_type_cnt"))
647+
goto cleanup;
648+
649+
VALIDATE_RAW_BTF(
650+
btf4,
651+
"[1] STRUCT 's1' size=4 vlen=0",
652+
"[2] STRUCT 'with_embedded' size=8 vlen=1\n"
653+
"\t'e1' type_id=1 bits_offset=0");
654+
655+
btf5 = btf__new_empty();
656+
if (!ASSERT_OK_PTR(btf5, "empty_reloc_btf"))
657+
goto cleanup;
658+
659+
btf__add_int(btf5, "int", 4, BTF_INT_SIGNED); /* [1] int */
660+
/* struct with the same name but different size */
661+
btf__add_struct(btf5, "s1", 8); /* [2] struct s1 { */
662+
btf__add_field(btf5, "f1", 1, 0, 0); /* int f1; */
663+
/* } */
664+
665+
ASSERT_EQ(btf__relocate(btf4, btf5), -EINVAL, "relocate_split");
666+
cleanup:
667+
btf__free(btf5);
668+
btf__free(btf4);
669+
btf__free(btf3);
670+
btf__free(btf2);
671+
btf__free(btf1);
672+
}
673+
604674
void test_btf_distill(void)
605675
{
606676
if (test__start_subtest("distilled_base"))
@@ -613,6 +683,8 @@ void test_btf_distill(void)
613683
test_distilled_base_multi_err();
614684
if (test__start_subtest("distilled_base_multi_err2"))
615685
test_distilled_base_multi_err2();
686+
if (test__start_subtest("distilled_base_embedded_err"))
687+
test_distilled_base_embedded_err();
616688
if (test__start_subtest("distilled_base_vmlinux"))
617689
test_distilled_base_vmlinux();
618690
if (test__start_subtest("distilled_endianness"))

0 commit comments

Comments
 (0)