From: Alexei Starovoitov Two fmod_ret programs return the same 64-bit value, one for a function that returns int, one for a function that returns bool. The test looks at what the trampoline makes of it: value int function bool function 1 << 32 called called 0x100 skipped, 0x100 called 2 skipped, 2 skipped, 1 -EINVAL skipped, -EINVAL skipped, 1 (u32)-EINVAL skipped, -EINVAL skipped, 1 fexit programs read the value with bpf_get_func_ret(). The side effect of bpf_modify_return_test() tells whether it was called. bpf_testmod gets the bool function, which returns false. The test runs on x86-64 and arm64. Also update the comment of a test that described the trampoline. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/modify_return.c | 51 +++++++++++++++++++ .../selftests/bpf/progs/fmodret_retval.c | 2 +- .../selftests/bpf/progs/modify_return_width.c | 39 ++++++++++++++ .../selftests/bpf/test_kmods/bpf_testmod.c | 9 ++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/modify_return_width.c diff --git a/tools/testing/selftests/bpf/prog_tests/modify_return.c b/tools/testing/selftests/bpf/prog_tests/modify_return.c index 15681b49f86f..2417030f104a 100644 --- a/tools/testing/selftests/bpf/prog_tests/modify_return.c +++ b/tools/testing/selftests/bpf/prog_tests/modify_return.c @@ -7,6 +7,7 @@ #include #include #include "modify_return.skel.h" +#include "modify_return_width.skel.h" #define LOWER(x) ((x) & 0xffff) #define UPPER(x) ((x) >> 16) @@ -66,6 +67,56 @@ void test_modify_return(void) -EINVAL * 2 /* want_ret */); } +/* + * The trampoline fits what an fmod_ret program returns to the return type of + * the function: int for bpf_modify_return_test(), bool for the one of bpf_testmod. + * Serial: get_func_args_test modifies the value of the first one for each task. + */ +void serial_test_modify_return_width(void) +{ +#if defined(__x86_64__) || defined(__aarch64__) + static const struct { + __u64 value; /* The fmod_ret programs return it */ + bool int_called; /* Is bpf_modify_return_test() called? */ + __u64 int_ret, bool_ret; /* The fexit programs read it */ + } tests[] = { + { 1ULL << 32, true, 4, 0 }, + { 0x100, false, 0x100, 0 }, + { 2, false, 2, 1 }, + { -EINVAL, false, -EINVAL, 1 }, + { (__u32)-EINVAL, false, -EINVAL, 1 }, + }; + LIBBPF_OPTS(bpf_test_run_opts, topts); + struct modify_return_width *skel; + int i; + + skel = modify_return_width__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + skel->bss->pid = getpid(); + if (!ASSERT_OK(modify_return_width__attach(skel), "attach")) + goto out; + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + skel->bss->value = tests[i].value; + skel->bss->int_ret = -1; + skel->bss->bool_ret = -1; + ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(skel->progs.mod_int), &topts), + "test_run"); + ASSERT_OK(trigger_module_test_read(1), "trigger_read"); + + /* bpf_modify_return_test2() has the other side effect */ + ASSERT_EQ(UPPER(topts.retval), 1 + tests[i].int_called, "int_called"); + ASSERT_EQ(skel->bss->int_ret, tests[i].int_ret, "int_ret"); + ASSERT_EQ(skel->bss->bool_ret, tests[i].bool_ret, "bool_ret"); + } +out: + modify_return_width__destroy(skel); +#else + test__skip(); +#endif +} + void test_modify_return_graph(void) { char instance[MAX_INSTANCE_PATH_LEN]; diff --git a/tools/testing/selftests/bpf/progs/fmodret_retval.c b/tools/testing/selftests/bpf/progs/fmodret_retval.c index 1045462a4ec7..112f1575ce1f 100644 --- a/tools/testing/selftests/bpf/progs/fmodret_retval.c +++ b/tools/testing/selftests/bpf/progs/fmodret_retval.c @@ -85,7 +85,7 @@ __naked void negated_bool(void) ::: __clobber_all); } -/* Not 0, so the function is not called, but the caller would see 0 */ +/* 0 for the trampoline and for the caller, but not one of the 64-bit values of the list */ SEC("fmod_ret/bpf_testmod_retval_called") __failure __msg("R0 should be 0 or one of -27,-22") __naked void high_bits(void) diff --git a/tools/testing/selftests/bpf/progs/modify_return_width.c b/tools/testing/selftests/bpf/progs/modify_return_width.c new file mode 100644 index 000000000000..add9e80e6a43 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/modify_return_width.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +/* See serial_test_modify_return_width() */ +#include +#include +#include + +char _license[] SEC("license") = "GPL"; + +int pid; +__u64 value; /* The fmod_ret programs return it */ +__u64 int_ret, bool_ret; /* The fexit programs read it */ + +SEC("fmod_ret/bpf_modify_return_test") +long BPF_PROG(mod_int, int a, int *b, int ret) +{ + return bpf_get_current_pid_tgid() >> 32 == pid ? value : 0; +} + +SEC("fexit/bpf_modify_return_test") +int BPF_PROG(exit_int, int a, int *b, int ret) +{ + if (bpf_get_current_pid_tgid() >> 32 == pid) + bpf_get_func_ret(ctx, &int_ret); + return 0; +} + +SEC("fmod_ret/bpf_testmod_modify_return_bool") +long BPF_PROG(mod_bool, bool ret) +{ + return bpf_get_current_pid_tgid() >> 32 == pid ? value : 0; +} + +SEC("fexit/bpf_testmod_modify_return_bool") +int BPF_PROG(exit_bool, bool ret) +{ + if (bpf_get_current_pid_tgid() >> 32 == pid) + bpf_get_func_ret(ctx, &bool_ret); + return 0; +} diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c index 58846056f667..1fbc0061cc24 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c @@ -767,6 +767,13 @@ noinline void bpf_testmod_stacktrace_test_1(void) int bpf_testmod_fentry_ok; +noinline bool bpf_testmod_modify_return_bool(void) +{ + asm volatile (""); + return false; +} +ALLOW_ERROR_INJECTION(bpf_testmod_modify_return_bool, TRUE); + noinline ssize_t bpf_testmod_test_read(struct file *file, struct kobject *kobj, const struct bin_attribute *bin_attr, @@ -856,6 +863,8 @@ bpf_testmod_test_read(struct file *file, struct kobject *kobj, 21, 22, 23, 24, 25, 26) != 231) goto out; + (void)bpf_testmod_modify_return_bool(); + bpf_testmod_stacktrace_test_1(); bpf_testmod_fentry_ok = 1; -- 2.55.0