Skip to content

Commit 181b0d1

Browse files
eddyz87anakryiko
authored andcommitted
selftests/bpf: Check if distilled base inherits source endianness
Create a BTF with endianness different from host, make a distilled base/split BTF pair from it, dump as raw bytes, import again and verify that endianness is preserved. Reviewed-by: Alan Maguire <[email protected]> Tested-by: Alan Maguire <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent da18bfa commit 181b0d1

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,72 @@ static void test_distilled_base_vmlinux(void)
535535
btf__free(vmlinux_btf);
536536
}
537537

538+
/* Split and new base BTFs should inherit endianness from source BTF. */
539+
static void test_distilled_endianness(void)
540+
{
541+
struct btf *base = NULL, *split = NULL, *new_base = NULL, *new_split = NULL;
542+
struct btf *new_base1 = NULL, *new_split1 = NULL;
543+
enum btf_endianness inverse_endianness;
544+
const void *raw_data;
545+
__u32 size;
546+
547+
base = btf__new_empty();
548+
if (!ASSERT_OK_PTR(base, "empty_main_btf"))
549+
return;
550+
inverse_endianness = btf__endianness(base) == BTF_LITTLE_ENDIAN ? BTF_BIG_ENDIAN
551+
: BTF_LITTLE_ENDIAN;
552+
btf__set_endianness(base, inverse_endianness);
553+
btf__add_int(base, "int", 4, BTF_INT_SIGNED); /* [1] int */
554+
VALIDATE_RAW_BTF(
555+
base,
556+
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED");
557+
split = btf__new_empty_split(base);
558+
if (!ASSERT_OK_PTR(split, "empty_split_btf"))
559+
goto cleanup;
560+
btf__add_ptr(split, 1);
561+
VALIDATE_RAW_BTF(
562+
split,
563+
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
564+
"[2] PTR '(anon)' type_id=1");
565+
if (!ASSERT_EQ(0, btf__distill_base(split, &new_base, &new_split),
566+
"distilled_base") ||
567+
!ASSERT_OK_PTR(new_base, "distilled_base") ||
568+
!ASSERT_OK_PTR(new_split, "distilled_split") ||
569+
!ASSERT_EQ(2, btf__type_cnt(new_base), "distilled_base_type_cnt"))
570+
goto cleanup;
571+
VALIDATE_RAW_BTF(
572+
new_split,
573+
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
574+
"[2] PTR '(anon)' type_id=1");
575+
576+
raw_data = btf__raw_data(new_base, &size);
577+
if (!ASSERT_OK_PTR(raw_data, "btf__raw_data #1"))
578+
goto cleanup;
579+
new_base1 = btf__new(raw_data, size);
580+
if (!ASSERT_OK_PTR(new_base1, "new_base1 = btf__new()"))
581+
goto cleanup;
582+
raw_data = btf__raw_data(new_split, &size);
583+
if (!ASSERT_OK_PTR(raw_data, "btf__raw_data #2"))
584+
goto cleanup;
585+
new_split1 = btf__new_split(raw_data, size, new_base1);
586+
if (!ASSERT_OK_PTR(new_split1, "new_split1 = btf__new()"))
587+
goto cleanup;
588+
589+
ASSERT_EQ(btf__endianness(new_base1), inverse_endianness, "new_base1 endianness");
590+
ASSERT_EQ(btf__endianness(new_split1), inverse_endianness, "new_split1 endianness");
591+
VALIDATE_RAW_BTF(
592+
new_split1,
593+
"[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED",
594+
"[2] PTR '(anon)' type_id=1");
595+
cleanup:
596+
btf__free(new_split1);
597+
btf__free(new_base1);
598+
btf__free(new_split);
599+
btf__free(new_base);
600+
btf__free(split);
601+
btf__free(base);
602+
}
603+
538604
void test_btf_distill(void)
539605
{
540606
if (test__start_subtest("distilled_base"))
@@ -549,4 +615,6 @@ void test_btf_distill(void)
549615
test_distilled_base_multi_err2();
550616
if (test__start_subtest("distilled_base_vmlinux"))
551617
test_distilled_base_vmlinux();
618+
if (test__start_subtest("distilled_endianness"))
619+
test_distilled_endianness();
552620
}

0 commit comments

Comments
 (0)