selftests/bpf: Test if shadow types work correctly.
authorKui-Feng Lee <thinker.li@gmail.com>
Thu, 29 Feb 2024 06:45:23 +0000 (22:45 -0800)
committerAndrii Nakryiko <andrii@kernel.org>
Thu, 29 Feb 2024 22:23:53 +0000 (14:23 -0800)
Change the values of fields, including scalar types and function pointers,
and check if the struct_ops map works as expected.

The test changes the field "test_2" of "testmod_1" from the pointer to
test_2() to pointer to test_3() and the field "data" to 13. The function
test_2() and test_3() both compute a new value for "test_2_result", but in
different way. By checking the value of "test_2_result", it ensures the
struct_ops map works as expected with changes through shadow types.

Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240229064523.2091270-6-thinker.li@gmail.com
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c
tools/testing/selftests/bpf/progs/struct_ops_module.c

index 66787e99ba1b075aa415c0e4a5957e21d7d8e9e8..098ddd0672244f3373642dae611b3bcaec54dbab 100644 (file)
@@ -539,6 +539,15 @@ static int bpf_testmod_ops_init_member(const struct btf_type *t,
                                       const struct btf_member *member,
                                       void *kdata, const void *udata)
 {
+       if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) {
+               /* For data fields, this function has to copy it and return
+                * 1 to indicate that the data has been handled by the
+                * struct_ops type, or the verifier will reject the map if
+                * the value of the data field is not zero.
+                */
+               ((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
+               return 1;
+       }
        return 0;
 }
 
@@ -559,7 +568,7 @@ static int bpf_dummy_reg(void *kdata)
         * initialized, so we need to check for NULL.
         */
        if (ops->test_2)
-               ops->test_2(4, 3);
+               ops->test_2(4, ops->data);
 
        return 0;
 }
index c3b0cf788f9f734695606d554ca27613c875b9c1..971458acfac350c0e09c7d76457a31ae9564b4b0 100644 (file)
@@ -35,6 +35,14 @@ struct bpf_testmod_ops {
        void (*test_2)(int a, int b);
        /* Used to test nullable arguments. */
        int (*test_maybe_null)(int dummy, struct task_struct *task);
+
+       /* The following fields are used to test shadow copies. */
+       char onebyte;
+       struct {
+               int a;
+               int b;
+       } unsupported;
+       int data;
 };
 
 #endif /* _BPF_TESTMOD_H */
index 8d833f0c75806c0e851798930475233268d4694b..7d6facf46ebb21ac01c0880063d6f3735d4fa473 100644 (file)
@@ -32,17 +32,23 @@ cleanup:
 
 static void test_struct_ops_load(void)
 {
-       DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
        struct struct_ops_module *skel;
        struct bpf_map_info info = {};
        struct bpf_link *link;
        int err;
        u32 len;
 
-       skel = struct_ops_module__open_opts(&opts);
+       skel = struct_ops_module__open();
        if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
                return;
 
+       skel->struct_ops.testmod_1->data = 13;
+       skel->struct_ops.testmod_1->test_2 = skel->progs.test_3;
+       /* Since test_2() is not being used, it should be disabled from
+        * auto-loading, or it will fail to load.
+        */
+       bpf_program__set_autoload(skel->progs.test_2, false);
+
        err = struct_ops_module__load(skel);
        if (!ASSERT_OK(err, "struct_ops_module_load"))
                goto cleanup;
@@ -56,8 +62,13 @@ static void test_struct_ops_load(void)
        link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
        ASSERT_OK_PTR(link, "attach_test_mod_1");
 
-       /* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */
-       ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result");
+       /* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c
+        *
+        * In bpf_testmod.c it will pass 4 and 13 (the value of data) to
+        * .test_2.  So, the value of test_2_result should be 20 (4 + 13 +
+        * 3).
+        */
+       ASSERT_EQ(skel->bss->test_2_result, 20, "check_shadow_variables");
 
        bpf_link__destroy(link);
 
index b78746b3cef35c783bcfaea16bb3c04e6aae7c96..25952fa093482fa2d89e36510460f5119d48f5e8 100644 (file)
@@ -21,9 +21,17 @@ void BPF_PROG(test_2, int a, int b)
        test_2_result = a + b;
 }
 
+SEC("struct_ops/test_3")
+int BPF_PROG(test_3, int a, int b)
+{
+       test_2_result = a + b + 3;
+       return a + b + 3;
+}
+
 SEC(".struct_ops.link")
 struct bpf_testmod_ops testmod_1 = {
        .test_1 = (void *)test_1,
        .test_2 = (void *)test_2,
+       .data = 0x1,
 };