A struct_ops program can have more arguments than what the corresponding function pointer in the kernel has if the program doesn't access the arguments. For example, a struct_ops operator may have 2 arguments, but your program may defined with 3 or more arguments. It is acceptable as long as the program doesn't use these arguments. Signed-off-by: Kui-Feng Lee <thinker.li@xxxxxxxxx> --- .../bpf/prog_tests/test_struct_ops_module.c | 56 +++++++++++++++++++ .../bpf/progs/struct_ops_extra_arg.c | 49 ++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c index 098776d00ab4..a146bf079a60 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c @@ -4,6 +4,7 @@ #include <time.h> #include "struct_ops_module.skel.h" +#include "struct_ops_extra_arg.skel.h" static void check_map_info(struct bpf_map_info *info) { @@ -138,11 +139,66 @@ static void test_struct_ops_not_zeroed(void) struct_ops_module__destroy(skel); } +/* Handle BPF programs with additional arguments that don't appear in the + * function pointer prototype in the kernel. + */ +static void test_struct_ops_extra_arg(void) +{ + struct struct_ops_extra_arg *skel; + int err; + + /* test_extra_arg() has an extra argument, so testmod_1 should fail + * to load. + * + * Since extra_arg is used in test_extra_arg(), it should be + * rejected by the verifier. + */ + skel = struct_ops_extra_arg__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_extra_arg_open_extra_arg")) + return; + + err = struct_ops_extra_arg__load(skel); + ASSERT_ERR(err, "struct_ops_extra_arg_load_extra_arg"); + + struct_ops_extra_arg__destroy(skel); + + /* Switch to test_2() */ + skel = struct_ops_extra_arg__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_extra_arg_open")) + return; + + skel->struct_ops.testmod_1->test_2 = skel->progs.test_2; + + err = struct_ops_extra_arg__load(skel); + ASSERT_OK(err, "struct_ops_extra_arg_load"); + + struct_ops_extra_arg__destroy(skel); + + /* Switch to test_extra_arg_unused() + * + * Since extra_arg is never used, it should be accepted by the + * verifier. The verifier would accept a program with extra + * arguments as long as they are not used. + */ + skel = struct_ops_extra_arg__open(); + if (!ASSERT_OK_PTR(skel, "struct_ops_extra_arg_open_unused")) + return; + + skel->struct_ops.testmod_1->test_2 = skel->progs.test_extra_arg_unused; + + err = struct_ops_extra_arg__load(skel); + ASSERT_OK(err, "struct_ops_extra_arg_load_unused"); + + struct_ops_extra_arg__destroy(skel); +} + void serial_test_struct_ops_module(void) { if (test__start_subtest("test_struct_ops_load")) test_struct_ops_load(); if (test__start_subtest("test_struct_ops_not_zeroed")) test_struct_ops_not_zeroed(); + if (test__start_subtest("test_struct_ops_extra_arg")) + test_struct_ops_extra_arg(); } diff --git a/tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c b/tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c new file mode 100644 index 000000000000..4b73b279ad22 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/struct_ops_extra_arg.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include "../bpf_testmod/bpf_testmod.h" + +char _license[] SEC("license") = "GPL"; + +int test_1_result = 0; +int test_2_result = 0; + +SEC("struct_ops/test_1") +int BPF_PROG(test_1) +{ + test_1_result = 0xdeadbeef; + return 0; +} + +SEC("?struct_ops/test_2") +void BPF_PROG(test_2, int a, int b) +{ + test_2_result = a + b; +} + +SEC("?struct_ops/test_extra_arg") +void BPF_PROG(test_extra_arg, int a, int b, int extra_arg) +{ + /* Accessing extra_arg will cause a verifier error since it + * accesses the context data beyond the size of the context. + */ + test_2_result = a + b + extra_arg + 3; +} + +SEC("?struct_ops/test_extra_arg_unused") +void BPF_PROG(test_extra_arg_unused, int a, int b, int extra_arg) +{ + /* The extra_arg is not used, so it should not cause a verifier + * error. + */ + test_2_result = a + b + 3; +} + +SEC(".struct_ops.link") +struct bpf_testmod_ops testmod_1 = { + .test_1 = (void *)test_1, + .test_2 = (void *)test_extra_arg, + .data = 0x1, +}; -- 2.34.1