selftests/bpf: Test attach rejection for struct_ops arena programs

Exercise fentry, fexit, and freplace programs that target a struct_ops
callback with an arena context argument. Verify each load is rejected with
-EOPNOTSUPP and the arena-specific verifier diagnostic.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://patch.msgid.link/20260808003938.3486067-15-memxor@gmail.com
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
This commit is contained in:
Kumar Kartikeya Dwivedi
2026-08-08 02:39:34 +02:00
committed by Eduard Zingerman
parent fd6094ac87
commit 4976cce08b
2 changed files with 79 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
#include <test_progs.h>
#include "struct_ops_arena.skel.h"
#include "struct_ops_arena_attach.skel.h"
#include "struct_ops_arena_fail.skel.h"
#if defined(__x86_64__)
@@ -49,6 +50,57 @@ static void arena_arg_fail(void)
struct_ops_arena_fail__destroy(skel);
}
static void arena_arg_attach_one(int target_fd, const char *prog_name)
{
struct struct_ops_arena_attach *skel;
struct bpf_program *prog, *pos;
char log_buf[64 * 1024];
int err;
skel = struct_ops_arena_attach__open();
if (!ASSERT_OK_PTR(skel, "struct_ops_arena_attach__open"))
return;
prog = bpf_object__find_program_by_name(skel->obj, prog_name);
if (!ASSERT_OK_PTR(prog, prog_name))
goto out;
bpf_object__for_each_program(pos, skel->obj)
bpf_program__set_autoload(pos, pos == prog);
err = bpf_program__set_attach_target(prog, target_fd, "test_arena_cb");
if (!ASSERT_OK(err, "set_attach_target"))
goto out;
log_buf[0] = '\0';
bpf_program__set_log_buf(prog, log_buf, sizeof(log_buf));
err = struct_ops_arena_attach__load(skel);
ASSERT_EQ(err, -EOPNOTSUPP, prog_name);
ASSERT_HAS_SUBSTR(log_buf, "Cannot attach to a target with arena context arguments",
"verifier_log");
out:
struct_ops_arena_attach__destroy(skel);
}
static void arena_arg_attach(void)
{
struct struct_ops_arena *skel;
int target_fd;
skel = struct_ops_arena__open_and_load();
if (!ASSERT_OK_PTR(skel, "struct_ops_arena__open_and_load"))
return;
target_fd = bpf_program__fd(skel->progs.test_arena_cb);
arena_arg_attach_one(target_fd, "fentry_test_arena");
arena_arg_attach_one(target_fd, "fexit_test_arena");
arena_arg_attach_one(target_fd, "freplace_test_arena");
struct_ops_arena__destroy(skel);
}
#endif
/*
@@ -68,6 +120,8 @@ void serial_test_struct_ops_arena(void)
arena_arg();
if (test__start_subtest("arena_arg_fail"))
arena_arg_fail();
if (test__start_subtest("arena_arg_attach"))
arena_arg_attach();
#else
test__skip();
#endif

View File

@@ -0,0 +1,25 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <linux/bpf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
SEC("fentry")
int BPF_PROG(fentry_test_arena, unsigned long long *st_ops_ctx)
{
return 0;
}
SEC("fexit")
int BPF_PROG(fexit_test_arena, unsigned long long *st_ops_ctx, int ret)
{
return 0;
}
SEC("freplace")
int freplace_test_arena(unsigned long long *st_ops_ctx)
{
return 0;
}
char _license[] SEC("license") = "GPL";