From: Alexei Starovoitov callee_takes_r5() has one parameter in BTF. Its code also reads r5, and it returns in r0 and r2. test attached expected --------------------- -------- ------------------------------------ int128_tgt_prog fentry the subprog gets r5 of its caller tgt_prog_returns_pair fexit the verifier refuses to load the prog The fentry prog reads r5 from its context. That loads only when the trampoline has 5 u64 arguments. Then it clobbers r1-r5, so that the result does not depend on what the kernel leaves in these registers. test_tgt_prog_unreliable() gets the prog, the target and the message as arguments, so that both refusals share it. The two cases need no stack arguments. They still run only where the compiler and the JIT have them, because they share that function and the block of the static subprogs with the cases that need them. Signed-off-by: Alexei Starovoitov --- .../selftests/bpf/prog_tests/tracing_struct.c | 27 +++++++++++++------ .../bpf/progs/tracing_struct_int128_tgt.c | 15 ++++++++++- .../bpf/progs/tracing_struct_int128_tracer.c | 6 ++++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/tracing_struct.c b/tools/testing/selftests/bpf/prog_tests/tracing_struct.c index de358f335d98..ab74ba077041 100644 --- a/tools/testing/selftests/bpf/prog_tests/tracing_struct.c +++ b/tools/testing/selftests/bpf/prog_tests/tracing_struct.c @@ -198,6 +198,10 @@ static void test_int128_tgt_prog(void) "callee_split"); err = err ?: bpf_program__set_attach_target(tracer->progs.split_exit, fd, "callee_split"); + /* The trampoline has to keep r5, which BTF doesn't have. */ + bpf_program__set_autoload(tracer->progs.unreliable_entry, true); + err = err ?: bpf_program__set_attach_target(tracer->progs.unreliable_entry, fd, + "callee_takes_r5"); } else { bpf_program__set_autoload(tracer->progs.stack_entry, false); bpf_program__set_autoload(tracer->progs.stack_exit, false); @@ -239,6 +243,8 @@ static void test_int128_tgt_prog(void) ASSERT_EQ(tracer->bss->split_i, 10, "split:i"); ASSERT_EQ(tgt->bss->ret_split, 246, "split ret"); ASSERT_EQ(tracer->bss->split_ret, 246, "split fexit ret"); + ASSERT_EQ(tracer->bss->unreliable_r5, 2, "takes more:r5"); + ASSERT_EQ(tgt->bss->ret_takes_more, 11, "takes more ret"); } out: @@ -252,14 +258,15 @@ static void test_int128_tgt_prog(void) /* * A static function whose BTF is unreliable gets a trampoline that only knows * of the register arguments, so one taking stack arguments must be refused. + * It only knows of r0 too, so fexit to one returning in r0 and r2 must be refused. */ -static void test_tgt_prog_unreliable(void) +static void test_tgt_prog_unreliable(const char *name, const char *func, const char *msg) { #if defined(__x86_64__) || defined(__aarch64__) struct tracing_struct_int128_tracer *tracer = NULL; struct tracing_struct_int128_tgt *tgt; + struct bpf_program *prog, *entry; char log_buf[4096] = {}; - struct bpf_program *prog; int err, fd; tgt = tracing_struct_int128_tgt__open_and_load(); @@ -277,16 +284,16 @@ static void test_tgt_prog_unreliable(void) bpf_object__for_each_program(prog, tracer->obj) bpf_program__set_autoload(prog, false); - bpf_program__set_autoload(tracer->progs.unreliable_entry, true); - bpf_program__set_log_buf(tracer->progs.unreliable_entry, log_buf, sizeof(log_buf)); - err = bpf_program__set_attach_target(tracer->progs.unreliable_entry, fd, - "callee_unreliable"); + entry = bpf_object__find_program_by_name(tracer->obj, name); + bpf_program__set_autoload(entry, true); + bpf_program__set_log_buf(entry, log_buf, sizeof(log_buf)); + err = bpf_program__set_attach_target(entry, fd, func); if (!ASSERT_OK(err, "set_attach_target")) goto out; err = tracing_struct_int128_tracer__load(tracer); ASSERT_ERR(err, "tracer__load"); - ASSERT_HAS_SUBSTR(log_buf, "unreliable BTF and stack arguments", "log"); + ASSERT_HAS_SUBSTR(log_buf, msg, "log"); out: tracing_struct_int128_tracer__destroy(tracer); @@ -389,7 +396,11 @@ void test_tracing_struct(void) if (test__start_subtest("int128_tgt_prog")) test_int128_tgt_prog(); if (test__start_subtest("tgt_prog_unreliable")) - test_tgt_prog_unreliable(); + test_tgt_prog_unreliable("unreliable_entry", "callee_unreliable", + "unreliable BTF and stack arguments"); + if (test__start_subtest("tgt_prog_returns_pair")) + test_tgt_prog_unreliable("odd_exit", "callee_takes_r5", + "with a >8 byte return value"); if (test__start_subtest("stack_arg_tailcall")) test_stack_arg_tailcall(); if (test__start_subtest("union_args")) diff --git a/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c b/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c index 2561422c25dd..2e2ddd1b52df 100644 --- a/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c +++ b/tools/testing/selftests/bpf/progs/tracing_struct_int128_tgt.c @@ -2,13 +2,14 @@ /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ #include #include +#include "bpf_misc.h" struct int128_arg { __int128 v; }; __u64 base; -__u64 ret_odd, ret_stack, ret_split, ret_unreliable; +__u64 ret_odd, ret_stack, ret_split, ret_unreliable, ret_takes_more; #if defined(__clang__) @@ -68,6 +69,17 @@ static __noinline __u64 callee_unreliable(__u64 *p, __u64 b, __u64 c, __u64 d, return *p + b * 2 + c * 3 + d * 4 + e * 5 + f * 6; } +/* BTF has 'a'. The code also takes r5. It returns in r0 and r2. */ +static __naked __noinline __used __int128 callee_takes_r5(__u64 a) +{ + asm volatile ("r0 = r1; r0 += r5; r2 = r5; exit;"); +} + +static __naked __noinline __u64 call_takes_more(void) +{ + asm volatile ("r1 = 7; r5 = 2; call callee_takes_r5; r0 += r2; exit;"); +} + #else const volatile bool has_stack_arg = false; @@ -93,6 +105,7 @@ int run(void *ctx) ret_unreliable = callee_unreliable(&x, base + 2, base + 3, base + 4, base + 5, base + 6); } + ret_takes_more = call_takes_more(); #endif #endif return 0; diff --git a/tools/testing/selftests/bpf/progs/tracing_struct_int128_tracer.c b/tools/testing/selftests/bpf/progs/tracing_struct_int128_tracer.c index fc81c2eea4a8..6a1d5ce0c171 100644 --- a/tools/testing/selftests/bpf/progs/tracing_struct_int128_tracer.c +++ b/tools/testing/selftests/bpf/progs/tracing_struct_int128_tracer.c @@ -67,10 +67,14 @@ int split_exit(unsigned long long *ctx) return 0; } -/* Refused: the trampoline would lose the stack argument. */ +long unreliable_r5; + +/* For static functions with unreliable BTF: the arguments are r1-r5. It clobbers them. */ SEC("?fentry/callee_unreliable") int unreliable_entry(unsigned long long *ctx) { + unreliable_r5 = ctx[4]; + asm volatile ("r1 = 0; r2 = 0; r3 = 0; r4 = 0; r5 = 0;" ::: "r1", "r2", "r3", "r4", "r5"); return 0; } -- 2.55.0