Skip to content

Commit

Permalink
selftests/bpf: Add btf_dedup case with duplicated structs within CU
Browse files Browse the repository at this point in the history
Add an artificial minimal example simulating compilers producing two
different types within a single CU that correspond to identical struct
definitions.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211117194114.347675-2-andrii@kernel.org
  • Loading branch information
Jiri Olsa authored and Daniel Borkmann committed Nov 19, 2021
1 parent efdd3eb commit 9a49afe
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/btf_dedup_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,117 @@ static void test_split_struct_duped() {
btf__free(btf1);
}

static void btf_add_dup_struct_in_cu(struct btf *btf, int start_id)
{
#define ID(n) (start_id + n)
btf__set_pointer_size(btf, 8); /* enforce 64-bit arch */

btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */

btf__add_struct(btf, "s", 8); /* [2] struct s { */
btf__add_field(btf, "a", ID(3), 0, 0); /* struct anon a; */
btf__add_field(btf, "b", ID(4), 0, 0); /* struct anon b; */
/* } */

btf__add_struct(btf, "(anon)", 8); /* [3] struct anon { */
btf__add_field(btf, "f1", ID(1), 0, 0); /* int f1; */
btf__add_field(btf, "f2", ID(1), 32, 0); /* int f2; */
/* } */

btf__add_struct(btf, "(anon)", 8); /* [4] struct anon { */
btf__add_field(btf, "f1", ID(1), 0, 0); /* int f1; */
btf__add_field(btf, "f2", ID(1), 32, 0); /* int f2; */
/* } */
#undef ID
}

static void test_split_dup_struct_in_cu()
{
struct btf *btf1, *btf2;
int err;

/* generate the base data.. */
btf1 = btf__new_empty();
if (!ASSERT_OK_PTR(btf1, "empty_main_btf"))
return;

btf_add_dup_struct_in_cu(btf1, 0);

VALIDATE_RAW_BTF(
btf1,
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
"[2] STRUCT 's' size=8 vlen=2\n"
"\t'a' type_id=3 bits_offset=0\n"
"\t'b' type_id=4 bits_offset=0",
"[3] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=1 bits_offset=0\n"
"\t'f2' type_id=1 bits_offset=32",
"[4] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=1 bits_offset=0\n"
"\t'f2' type_id=1 bits_offset=32");

/* ..dedup them... */
err = btf__dedup(btf1, NULL, NULL);
if (!ASSERT_OK(err, "btf_dedup"))
goto cleanup;

VALIDATE_RAW_BTF(
btf1,
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
"[2] STRUCT 's' size=8 vlen=2\n"
"\t'a' type_id=3 bits_offset=0\n"
"\t'b' type_id=3 bits_offset=0",
"[3] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=1 bits_offset=0\n"
"\t'f2' type_id=1 bits_offset=32");

/* and add the same data on top of it */
btf2 = btf__new_empty_split(btf1);
if (!ASSERT_OK_PTR(btf2, "empty_split_btf"))
goto cleanup;

btf_add_dup_struct_in_cu(btf2, 3);

VALIDATE_RAW_BTF(
btf2,
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
"[2] STRUCT 's' size=8 vlen=2\n"
"\t'a' type_id=3 bits_offset=0\n"
"\t'b' type_id=3 bits_offset=0",
"[3] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=1 bits_offset=0\n"
"\t'f2' type_id=1 bits_offset=32",
"[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
"[5] STRUCT 's' size=8 vlen=2\n"
"\t'a' type_id=6 bits_offset=0\n"
"\t'b' type_id=7 bits_offset=0",
"[6] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=4 bits_offset=0\n"
"\t'f2' type_id=4 bits_offset=32",
"[7] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=4 bits_offset=0\n"
"\t'f2' type_id=4 bits_offset=32");

err = btf__dedup(btf2, NULL, NULL);
if (!ASSERT_OK(err, "btf_dedup"))
goto cleanup;

/* after dedup it should match the original data */
VALIDATE_RAW_BTF(
btf2,
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
"[2] STRUCT 's' size=8 vlen=2\n"
"\t'a' type_id=3 bits_offset=0\n"
"\t'b' type_id=3 bits_offset=0",
"[3] STRUCT '(anon)' size=8 vlen=2\n"
"\t'f1' type_id=1 bits_offset=0\n"
"\t'f2' type_id=1 bits_offset=32");

cleanup:
btf__free(btf2);
btf__free(btf1);
}

void test_btf_dedup_split()
{
if (test__start_subtest("split_simple"))
Expand All @@ -322,4 +433,6 @@ void test_btf_dedup_split()
test_split_struct_duped();
if (test__start_subtest("split_fwd_resolve"))
test_split_fwd_resolve();
if (test__start_subtest("split_dup_struct_in_cu"))
test_split_dup_struct_in_cu();
}

0 comments on commit 9a49afe

Please sign in to comment.