From: Alexei Starovoitov The kernel sets the 'unwound' bit in bpf_prog_info when a run of the prog does not end in the usual way. It prints the reason to stderr of the prog, the BPF_STDERR stream. Run four progs one time each. The bit is 0 before each run. 'call f; unwind_goto pad' continues at pad, the landing pad, when f exits with unwind_exit. prog what occurs retval unwound ---------- ----------------------------------------- ------ ------- caught a landing pad catches unwind_exit 42 0 not_caught the main prog exits with unwind_exit 7 1 spins may_goto that the verifier adds times out 0 1 too_deep a subprog calls itself without end 3 1 stderr of caught is empty. The other progs print: not_caught ERROR: The program was unwound spins ERROR: Timeout detected for may_goto instruction too_deep ERROR: No room in the stack for a call of a recursive function not_caught has 7 in r0 at unwind_exit. The call that reports the event must not change r0. The test also attaches to the recursive subprog. fentry loads. The verifier rejects fexit with "Only fentry can attach to calls_itself, which is recursive". The trampoline of fexit stays on the stack at each level of the recursion, and the check of the stack does not count it. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/unwound.c | 96 +++++++++++++++ tools/testing/selftests/bpf/progs/unwound.c | 113 ++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/unwound.c create mode 100644 tools/testing/selftests/bpf/progs/unwound.c diff --git a/tools/testing/selftests/bpf/prog_tests/unwound.c b/tools/testing/selftests/bpf/prog_tests/unwound.c new file mode 100644 index 000000000000..d5b4454ae011 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/unwound.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "unwound.skel.h" + +/* Run the prog once. What it returns, what bpf_prog_info says and what is in its stderr. */ +static void run(struct bpf_program *prog, int retval, bool unwound, const char *msg) +{ + char pkt[64] = {}, buf[4096] = {}; + LIBBPF_OPTS(bpf_test_run_opts, opts, .data_in = pkt, .data_size_in = sizeof(pkt), .repeat = 1); + struct bpf_prog_info info = {}; + __u32 len = sizeof(info); + int fd = bpf_program__fd(prog); + + if (!test__start_subtest(bpf_program__name(prog))) + return; + + ASSERT_OK(bpf_prog_get_info_by_fd(fd, &info, &len), "info_before"); + ASSERT_EQ((int)info.unwound, 0, "unwound_before"); + + ASSERT_OK(bpf_prog_test_run_opts(fd, &opts), "test_run"); + ASSERT_EQ(opts.retval, retval, "retval"); + + memset(&info, 0, sizeof(info)); + len = sizeof(info); + ASSERT_OK(bpf_prog_get_info_by_fd(fd, &info, &len), "info_after"); + ASSERT_EQ((int)info.unwound, (int)unwound, "unwound_after"); + + bpf_prog_stream_read(fd, BPF_STREAM_STDERR, buf, sizeof(buf) - 1, NULL); + if (msg) + ASSERT_HAS_SUBSTR(buf, msg, "stderr"); + else + ASSERT_STREQ(buf, "", "stderr"); +} + +/* + * The trampoline of fexit calls the target and stays on the stack at every level of a recursion. + * The check of the stack in the target knows nothing about it. + */ +static void attach(struct unwound *tgt, const char *name, const char *msg) +{ + static char log_buf[4096]; + struct bpf_program *prog, *p; + struct unwound *skel; + int err; + + if (!test__start_subtest(name)) + return; + skel = unwound__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + prog = bpf_object__find_program_by_name(skel->obj, name); + if (!ASSERT_OK_PTR(prog, name)) + goto out; + bpf_object__for_each_program(p, skel->obj) + bpf_program__set_autoload(p, p == prog); + log_buf[0] = 0; + bpf_program__set_log_buf(prog, log_buf, sizeof(log_buf)); + err = bpf_program__set_attach_target(prog, bpf_program__fd(tgt->progs.too_deep), + "calls_itself"); + if (!ASSERT_OK(err, "set_attach_target")) + goto out; + err = unwound__load(skel); + if (msg) { + ASSERT_ERR(err, name); + ASSERT_HAS_SUBSTR(log_buf, msg, name); + } else { + ASSERT_OK(err, name); + } +out: + unwound__destroy(skel); +} + +void test_unwound(void) +{ + struct unwound *skel; + +#if !defined(__x86_64__) && !defined(__aarch64__) + /* unwinding, per-cpu insns and timed may_goto in JIT */ + test__skip(); + return; +#endif + skel = unwound__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + return; + + run(skel->progs.caught, 42, false, NULL); + run(skel->progs.not_caught, 7, true, "ERROR: The program was unwound"); + run(skel->progs.spins, 0, true, "ERROR: Timeout detected for may_goto instruction"); + run(skel->progs.too_deep, 3, true, + "ERROR: No room in the stack for a call of a recursive function"); + + attach(skel, "on_entry", NULL); + attach(skel, "on_exit", "Only fentry can attach to calls_itself, which is recursive"); + + unwound__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/unwound.c b/tools/testing/selftests/bpf/progs/unwound.c new file mode 100644 index 000000000000..66ec90908838 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/unwound.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include "../../../include/linux/filter.h" +#include "bpf_misc.h" + +#define unwind_goto(off) ".8byte %[insn_unwind_goto_" #off "];" +#define unwind_exit ".8byte %[insn_unwind_exit];" + +#define __imm_unwind_goto(off) \ + __imm_insn(insn_unwind_goto_##off, \ + BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, BPF_UNWIND_GOTO, off, 0)) +#define __imm_unwind_exit \ + __imm_insn(insn_unwind_exit, \ + BPF_RAW_INSN(BPF_JMP | BPF_EXIT, 0, BPF_UNWIND_EXIT, 0, 0)) + +unsigned long one = 1; + +__naked __noinline __used +static unsigned long unwinds(void) +{ + asm volatile ( + "r0 = 0;" + unwind_exit + : + : __imm_unwind_exit + ); +} + +SEC("socket") +__naked void caught(void) +{ + asm volatile ( + "call unwinds;" + unwind_goto(1) + "exit;" + "r0 = 42;" + "exit;" + : + : __imm_unwind_goto(1) + : __clobber_all + ); +} + +SEC("socket") +__naked void not_caught(void) +{ + asm volatile ( + "call unwinds;" + unwind_goto(1) + "exit;" + "r0 = 7;" + unwind_exit + : + : __imm_unwind_goto(1), + __imm_unwind_exit + : __clobber_all + ); +} + +SEC("socket") +__naked void spins(void) +{ + asm volatile ( + "r6 = %[one] ll;" + "1:" + "r1 = *(u64 *)(r6 + 0);" + "if r1 != 0 goto 1b;" + "r0 = 1;" + "exit;" + : + : __imm_addr(one) + : __clobber_all + ); +} + +__naked __noinline __used +static unsigned long calls_itself(void) +{ + asm volatile ( + "r1 += 1;" + "call calls_itself;" + "exit;" + ::: __clobber_all + ); +} + +SEC("socket") +__naked void too_deep(void) +{ + asm volatile ( + "r1 = 0;" + "call calls_itself;" + "r0 = 3;" + "exit;" + ::: __clobber_all + ); +} + +/* the test attaches them to calls_itself() */ +SEC("?fentry") +int on_entry(void *ctx) +{ + return 0; +} + +SEC("?fexit") +int on_exit(void *ctx) +{ + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.55.0