selftests/bpf: Add type match test against kernel's task_struct
authorDaniel Müller <deso@posteo.net>
Tue, 28 Jun 2022 16:01:27 +0000 (16:01 +0000)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 6 Jul 2022 04:15:19 +0000 (21:15 -0700)
This change extends the existing core_reloc/kernel test to include a
type match check of a local task_struct against the kernel's definition
-- which we assume to succeed.

Signed-off-by: Daniel Müller <deso@posteo.net>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220628160127.607834-11-deso@posteo.net
tools/testing/selftests/bpf/prog_tests/core_reloc.c
tools/testing/selftests/bpf/progs/core_reloc_types.h
tools/testing/selftests/bpf/progs/test_core_reloc_kernel.c

index 8882c9c..a6f65e2 100644 (file)
@@ -555,6 +555,7 @@ static const struct core_reloc_test_case test_cases[] = {
                        .valid = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, },
                        .comm = "test_progs",
                        .comm_len = sizeof("test_progs"),
+                       .local_task_struct_matches = true,
                },
                .output_len = sizeof(struct core_reloc_kernel_output),
                .raw_tp_name = "sys_enter",
index 474411c..7ef91d1 100644 (file)
@@ -13,6 +13,7 @@ struct core_reloc_kernel_output {
        int valid[10];
        char comm[sizeof("test_progs")];
        int comm_len;
+       bool local_task_struct_matches;
 };
 
 /*
index 145028b..a17dd83 100644 (file)
@@ -21,6 +21,7 @@ struct core_reloc_kernel_output {
        /* we have test_progs[-flavor], so cut flavor part */
        char comm[sizeof("test_progs")];
        int comm_len;
+       bool local_task_struct_matches;
 };
 
 struct task_struct {
@@ -30,11 +31,25 @@ struct task_struct {
        struct task_struct *group_leader;
 };
 
+struct mm_struct___wrong {
+    int abc_whatever_should_not_exist;
+};
+
+struct task_struct___local {
+    int pid;
+    struct mm_struct___wrong *mm;
+};
+
 #define CORE_READ(dst, src) bpf_core_read(dst, sizeof(*(dst)), src)
 
 SEC("raw_tracepoint/sys_enter")
 int test_core_kernel(void *ctx)
 {
+       /* Support for the BPF_TYPE_MATCHES argument to the
+        * __builtin_preserve_type_info builtin was added at some point during
+        * development of clang 15 and it's what we require for this test.
+        */
+#if __has_builtin(__builtin_preserve_type_info) && __clang_major__ >= 15
        struct task_struct *task = (void *)bpf_get_current_task();
        struct core_reloc_kernel_output *out = (void *)&data.out;
        uint64_t pid_tgid = bpf_get_current_pid_tgid();
@@ -93,6 +108,10 @@ int test_core_kernel(void *ctx)
                group_leader, group_leader, group_leader, group_leader,
                comm);
 
+       out->local_task_struct_matches = bpf_core_type_matches(struct task_struct___local);
+#else
+       data.skip = true;
+#endif
        return 0;
 }