Skip to content

Commit 0623e73

Browse files
ThinkerYzu1anakryiko
authored andcommitted
selftests/bpf: Test if shadow types work correctly.
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
1 parent f2e8119 commit 0623e73

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,15 @@ static int bpf_testmod_ops_init_member(const struct btf_type *t,
539539
const struct btf_member *member,
540540
void *kdata, const void *udata)
541541
{
542+
if (member->offset == offsetof(struct bpf_testmod_ops, data) * 8) {
543+
/* For data fields, this function has to copy it and return
544+
* 1 to indicate that the data has been handled by the
545+
* struct_ops type, or the verifier will reject the map if
546+
* the value of the data field is not zero.
547+
*/
548+
((struct bpf_testmod_ops *)kdata)->data = ((struct bpf_testmod_ops *)udata)->data;
549+
return 1;
550+
}
542551
return 0;
543552
}
544553

@@ -559,7 +568,7 @@ static int bpf_dummy_reg(void *kdata)
559568
* initialized, so we need to check for NULL.
560569
*/
561570
if (ops->test_2)
562-
ops->test_2(4, 3);
571+
ops->test_2(4, ops->data);
563572

564573
return 0;
565574
}

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ struct bpf_testmod_ops {
3535
void (*test_2)(int a, int b);
3636
/* Used to test nullable arguments. */
3737
int (*test_maybe_null)(int dummy, struct task_struct *task);
38+
39+
/* The following fields are used to test shadow copies. */
40+
char onebyte;
41+
struct {
42+
int a;
43+
int b;
44+
} unsupported;
45+
int data;
3846
};
3947

4048
#endif /* _BPF_TESTMOD_H */

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,23 @@ static void check_map_info(struct bpf_map_info *info)
3232

3333
static void test_struct_ops_load(void)
3434
{
35-
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts);
3635
struct struct_ops_module *skel;
3736
struct bpf_map_info info = {};
3837
struct bpf_link *link;
3938
int err;
4039
u32 len;
4140

42-
skel = struct_ops_module__open_opts(&opts);
41+
skel = struct_ops_module__open();
4342
if (!ASSERT_OK_PTR(skel, "struct_ops_module_open"))
4443
return;
4544

45+
skel->struct_ops.testmod_1->data = 13;
46+
skel->struct_ops.testmod_1->test_2 = skel->progs.test_3;
47+
/* Since test_2() is not being used, it should be disabled from
48+
* auto-loading, or it will fail to load.
49+
*/
50+
bpf_program__set_autoload(skel->progs.test_2, false);
51+
4652
err = struct_ops_module__load(skel);
4753
if (!ASSERT_OK(err, "struct_ops_module_load"))
4854
goto cleanup;
@@ -56,8 +62,13 @@ static void test_struct_ops_load(void)
5662
link = bpf_map__attach_struct_ops(skel->maps.testmod_1);
5763
ASSERT_OK_PTR(link, "attach_test_mod_1");
5864

59-
/* test_2() will be called from bpf_dummy_reg() in bpf_testmod.c */
60-
ASSERT_EQ(skel->bss->test_2_result, 7, "test_2_result");
65+
/* test_3() will be called from bpf_dummy_reg() in bpf_testmod.c
66+
*
67+
* In bpf_testmod.c it will pass 4 and 13 (the value of data) to
68+
* .test_2. So, the value of test_2_result should be 20 (4 + 13 +
69+
* 3).
70+
*/
71+
ASSERT_EQ(skel->bss->test_2_result, 20, "check_shadow_variables");
6172

6273
bpf_link__destroy(link);
6374

tools/testing/selftests/bpf/progs/struct_ops_module.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ void BPF_PROG(test_2, int a, int b)
2121
test_2_result = a + b;
2222
}
2323

24+
SEC("struct_ops/test_3")
25+
int BPF_PROG(test_3, int a, int b)
26+
{
27+
test_2_result = a + b + 3;
28+
return a + b + 3;
29+
}
30+
2431
SEC(".struct_ops.link")
2532
struct bpf_testmod_ops testmod_1 = {
2633
.test_1 = (void *)test_1,
2734
.test_2 = (void *)test_2,
35+
.data = 0x1,
2836
};
2937

0 commit comments

Comments
 (0)