1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
3 #include <test_progs.h>
4
5 #include "struct_ops_arena.skel.h"
6 #include "struct_ops_arena_attach.skel.h"
7 #include "struct_ops_arena_fail.skel.h"
8
9 #if defined(__x86_64__) || defined(__aarch64__)
10 /*
11 * Attach callbacks with __arena and __arena__nullable arguments and drive
12 * them through the bpf_testmod_ops3_call_test_arena*() kfuncs.
13 */
arena_arg(void)14 static void arena_arg(void)
15 {
16 LIBBPF_OPTS(bpf_test_run_opts, topts);
17 struct struct_ops_arena *skel;
18 struct bpf_link *link = NULL;
19 int err;
20
21 skel = struct_ops_arena__open_and_load();
22 if (!ASSERT_OK_PTR(skel, "struct_ops_arena__open_and_load"))
23 return;
24
25 link = bpf_map__attach_struct_ops(skel->maps.testmod_arena);
26 if (!ASSERT_OK_PTR(link, "attach_struct_ops"))
27 goto out;
28
29 err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.trigger),
30 &topts);
31 ASSERT_OK(err, "test_run");
32 ASSERT_EQ(topts.retval, 0, "trigger_retval");
33
34 out:
35 bpf_link__destroy(link);
36 struct_ops_arena__destroy(skel);
37 }
38
39 /*
40 * A program with no arena cannot attach to a member with an __arena
41 * argument.
42 */
arena_arg_fail(void)43 static void arena_arg_fail(void)
44 {
45 struct struct_ops_arena_fail *skel;
46
47 skel = struct_ops_arena_fail__open_and_load();
48 if (ASSERT_ERR_PTR(skel, "struct_ops_arena_fail__open_and_load"))
49 return;
50
51 struct_ops_arena_fail__destroy(skel);
52 }
53
arena_arg_attach_one(int target_fd,const char * prog_name)54 static void arena_arg_attach_one(int target_fd, const char *prog_name)
55 {
56 struct struct_ops_arena_attach *skel;
57 struct bpf_program *prog, *pos;
58 char log_buf[64 * 1024];
59 int err;
60
61 skel = struct_ops_arena_attach__open();
62 if (!ASSERT_OK_PTR(skel, "struct_ops_arena_attach__open"))
63 return;
64
65 prog = bpf_object__find_program_by_name(skel->obj, prog_name);
66 if (!ASSERT_OK_PTR(prog, prog_name))
67 goto out;
68
69 bpf_object__for_each_program(pos, skel->obj)
70 bpf_program__set_autoload(pos, pos == prog);
71
72 err = bpf_program__set_attach_target(prog, target_fd, "test_arena_cb");
73 if (!ASSERT_OK(err, "set_attach_target"))
74 goto out;
75
76 log_buf[0] = '\0';
77 bpf_program__set_log_buf(prog, log_buf, sizeof(log_buf));
78 err = struct_ops_arena_attach__load(skel);
79
80 ASSERT_EQ(err, -EOPNOTSUPP, prog_name);
81 ASSERT_HAS_SUBSTR(log_buf, "Cannot attach to a target with arena context arguments",
82 "verifier_log");
83
84 out:
85 struct_ops_arena_attach__destroy(skel);
86 }
87
arena_arg_attach(void)88 static void arena_arg_attach(void)
89 {
90 struct struct_ops_arena *skel;
91 int target_fd;
92
93 skel = struct_ops_arena__open_and_load();
94 if (!ASSERT_OK_PTR(skel, "struct_ops_arena__open_and_load"))
95 return;
96
97 target_fd = bpf_program__fd(skel->progs.test_arena_cb);
98 arena_arg_attach_one(target_fd, "fentry_test_arena");
99 arena_arg_attach_one(target_fd, "fexit_test_arena");
100 arena_arg_attach_one(target_fd, "freplace_test_arena");
101
102 struct_ops_arena__destroy(skel);
103 }
104 #endif
105
106 /*
107 * Serialized because it attaches the singleton bpf_testmod_ops3, which
108 * test_struct_ops_private_stack also attaches; registering it twice fails
109 * with -EEXIST.
110 */
serial_test_struct_ops_arena(void)111 void serial_test_struct_ops_arena(void)
112 {
113 /*
114 * Arena struct_ops arguments need JIT support, currently x86-64 and
115 * arm64 only. Elsewhere verification fails with "JIT does not support
116 * arena arguments", so the programs cannot even load.
117 */
118 #if defined(__x86_64__) || defined(__aarch64__)
119 if (test__start_subtest("arena_arg"))
120 arena_arg();
121 if (test__start_subtest("arena_arg_fail"))
122 arena_arg_fail();
123 if (test__start_subtest("arena_arg_attach"))
124 arena_arg_attach();
125 #else
126 test__skip();
127 #endif
128 }
129