From: Yazhou Tang The test utilizes an inline assembly block with `.rept 200000` to generate a massive dummy subprogram. By placing this padding between the main program and the target subprogram, it forces the verifier to process a bpf-to-bpf call with the `imm` field exceeding the s16 range. The user-space test driver dynamically adapts to the host's JIT configuration: - When JIT is enabled, it asserts that the program is successfully loaded, JIT-compiled, and executes correctly (returning the expected value). - When JIT is disabled, it asserts that the program is cleanly rejected by the verifier with -EINVAL (or -ENOSPC if the verifier log buffer is truncated), and strictly matches the expected error log. Co-developed-by: Tianci Cao Signed-off-by: Tianci Cao Co-developed-by: Shenghao Yuan Signed-off-by: Shenghao Yuan Signed-off-by: Yazhou Tang --- .../selftests/bpf/prog_tests/call_large_imm.c | 49 +++++++++++++++++++ .../selftests/bpf/progs/call_large_imm.c | 38 ++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/call_large_imm.c create mode 100644 tools/testing/selftests/bpf/progs/call_large_imm.c diff --git a/tools/testing/selftests/bpf/prog_tests/call_large_imm.c b/tools/testing/selftests/bpf/prog_tests/call_large_imm.c new file mode 100644 index 000000000000..ba4a2e27fa5d --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/call_large_imm.c @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "call_large_imm.skel.h" + +void test_call_large_imm(void) +{ + struct call_large_imm *skel; + int err, prog_fd; + char log_buf[4096] = {}; + char dummy_data[64] = {}; + + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = dummy_data, + .data_size_in = sizeof(dummy_data), + ); + + skel = call_large_imm__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + if (!env.jit_enabled) + bpf_program__set_log_buf(skel->progs.call_large_imm_test, + log_buf, sizeof(log_buf)); + + err = call_large_imm__load(skel); + + if (env.jit_enabled) { + if (!ASSERT_OK(err, "load_should_succeed_with_jit")) + goto cleanup; + + prog_fd = bpf_program__fd(skel->progs.call_large_imm_test); + err = bpf_prog_test_run_opts(prog_fd, &opts); + + if (ASSERT_OK(err, "prog_run_success")) + ASSERT_EQ(opts.retval, 3, "prog_retval"); + + } else { + ASSERT_ERR(err, "load_should_fail_in_interpreter"); + ASSERT_TRUE(err == -EINVAL || err == -ENOSPC, "err_should_be_einval_or_enospc"); + + if (!ASSERT_OK_PTR(strstr(log_buf, "bpf-to-bpf call offset out of range for interpreter"), + "check_verifier_log_msg")) { + printf("Actual verifier log:\n%s\n", log_buf); + } + } + +cleanup: + call_large_imm__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/call_large_imm.c b/tools/testing/selftests/bpf/progs/call_large_imm.c new file mode 100644 index 000000000000..d0057f88b48b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/call_large_imm.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +struct __sk_buff; + +static __attribute__((noinline)) void padding_subprog(void) +{ + asm volatile (" \ + r0 = 0; \ + .rept 200000; \ + r0 += 0; \ + .endr; \ + " ::: "r0"); +} + +static __attribute__((noinline)) int target_subprog(struct __sk_buff *ctx) +{ + volatile int magic_ret = 3; + return magic_ret; +} + +SEC("socket") +int call_large_imm_test(struct __sk_buff *ctx) +{ + int ret = 0; + + if (ctx == (void *)0) + padding_subprog(); + + ret = target_subprog(ctx); + + bpf_printk("Target subprog returned: %d\n", ret); + + return ret; +} + +char LICENSE[] SEC("license") = "GPL"; -- 2.53.0