summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/bpf_testmod
diff options
context:
space:
mode:
authorKui-Feng Lee <thinker.li@gmail.com>2024-02-29 09:45:23 +0300
committerAndrii Nakryiko <andrii@kernel.org>2024-03-01 01:23:53 +0300
commit0623e73317940d052216fb6eef4efd55a0a7f602 (patch)
tree524cfad5f7755ea83210ec355b43fa650219b237 /tools/testing/selftests/bpf/bpf_testmod
parentf2e81192e07e87897ff1296c96775eceea8f582a (diff)
downloadlinux-0623e73317940d052216fb6eef4efd55a0a7f602.tar.xz
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
Diffstat (limited to 'tools/testing/selftests/bpf/bpf_testmod')
-rw-r--r--tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c11
-rw-r--r--tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h8
2 files changed, 18 insertions, 1 deletions
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
index 66787e99ba1b..098ddd067224 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
@@ -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;
}
diff --git a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
index c3b0cf788f9f..971458acfac3 100644
--- a/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
+++ b/tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
@@ -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 */