selftests/bpf: cover the exception callback using its own BPF stack

The existing exception tests do not reach a callback that materializes
BPF_REG_FP into a register. They either throw from the main program,
where BPF_REG_FP already holds the value the callback needs, or use a
callback whose only stack accesses are frame pointer relative, which the
arm64 JIT rewrites to be stack pointer relative.

Add a test that throws from a subprogram using its own BPF stack, with a
callback that hands the address of a local variable to
bpf_probe_read_kernel(). The helper and the callback have to name the
same slot for the value read back to be the one the helper stored.

Signed-off-by: Donggeun Yoo <donggeunyoo.kernel@gmail.com>
Link: https://lore.kernel.org/r/20260907130624.611942-3-donggeunyoo.kernel@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Donggeun Yoo 2026-09-07 22:06:24 +09:00 committed by Alexei Starovoitov
parent ef1fb82f12
commit 26a43a5f8c
2 changed files with 31 additions and 0 deletions

View File

@ -55,6 +55,7 @@ static void test_exceptions_success(void)
RUN_SUCCESS(exception_ext, 0);
RUN_SUCCESS(exception_ext_mod_cb_runtime, 35);
RUN_SUCCESS(exception_throw_subprog, 1);
RUN_SUCCESS(exception_throw_subprog_stack_cb, 0x1234);
RUN_SUCCESS(exception_assert_nz_gfunc, 1);
RUN_SUCCESS(exception_assert_zero_gfunc, 1);
RUN_SUCCESS(exception_assert_neg_gfunc, 1);

View File

@ -212,6 +212,36 @@ int exception_throw_subprog(struct __sk_buff *ctx)
return 0;
}
u64 exception_cb_stack_src = 0x1234;
/*
* The address handed to the helper has to be this callback's own stack
* slot, not one from a frame that is already gone.
*/
__noinline int exception_cb_stack(u64 cookie)
{
volatile u64 val = 0xdead;
bpf_probe_read_kernel((void *)&val, sizeof(val), &exception_cb_stack_src);
return val;
}
/* Throws from a subprogram that has a stack of its own. */
__noinline static int throwing_subprog_stack(struct __sk_buff *ctx)
{
volatile u64 pad[4] = {};
bpf_throw(pad[0]);
return 0;
}
SEC("tc")
__exception_cb(exception_cb_stack)
int exception_throw_subprog_stack_cb(struct __sk_buff *ctx)
{
return throwing_subprog_stack(ctx);
}
__noinline int assert_nz_gfunc(u64 c)
{
volatile u64 cookie = c;