Skip to content

Commit 41df073

Browse files
anakryikoMartin KaFai Lau
authored andcommitted
selftests/bpf: validate struct_ops early failure detection logic
Add a simple test that validates that libbpf will reject isolated struct_ops program early with helpful warning message. Also validate that explicit use of such BPF program through BPF skeleton after BPF object is open won't trigger any warnings. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20240507001335.1445325-7-andrii@kernel.org Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent c78420b commit 41df073

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "struct_ops_module.skel.h"
77
#include "struct_ops_nulled_out_cb.skel.h"
8+
#include "struct_ops_forgotten_cb.skel.h"
89

910
static void check_map_info(struct bpf_map_info *info)
1011
{
@@ -199,6 +200,48 @@ static void test_struct_ops_nulled_out_cb(void)
199200
struct_ops_nulled_out_cb__destroy(skel);
200201
}
201202

203+
/* validate that libbpf generates reasonable error message if struct_ops is
204+
* not referenced in any struct_ops map
205+
*/
206+
static void test_struct_ops_forgotten_cb(void)
207+
{
208+
struct struct_ops_forgotten_cb *skel;
209+
char *log;
210+
int err;
211+
212+
skel = struct_ops_forgotten_cb__open();
213+
if (!ASSERT_OK_PTR(skel, "skel_open"))
214+
return;
215+
216+
start_libbpf_log_capture();
217+
218+
err = struct_ops_forgotten_cb__load(skel);
219+
if (!ASSERT_ERR(err, "skel_load"))
220+
goto cleanup;
221+
222+
log = stop_libbpf_log_capture();
223+
ASSERT_HAS_SUBSTR(log,
224+
"prog 'test_1_forgotten': SEC(\"struct_ops\") program isn't referenced anywhere, did you forget to use it?",
225+
"libbpf_log");
226+
free(log);
227+
228+
struct_ops_forgotten_cb__destroy(skel);
229+
230+
/* now let's programmatically use it, we should be fine now */
231+
skel = struct_ops_forgotten_cb__open();
232+
if (!ASSERT_OK_PTR(skel, "skel_open"))
233+
return;
234+
235+
skel->struct_ops.ops->test_1 = skel->progs.test_1_forgotten; /* not anymore */
236+
237+
err = struct_ops_forgotten_cb__load(skel);
238+
if (!ASSERT_OK(err, "skel_load"))
239+
goto cleanup;
240+
241+
cleanup:
242+
struct_ops_forgotten_cb__destroy(skel);
243+
}
244+
202245
void serial_test_struct_ops_module(void)
203246
{
204247
if (test__start_subtest("test_struct_ops_load"))
@@ -209,5 +252,7 @@ void serial_test_struct_ops_module(void)
209252
test_struct_ops_incompatible();
210253
if (test__start_subtest("test_struct_ops_null_out_cb"))
211254
test_struct_ops_nulled_out_cb();
255+
if (test__start_subtest("struct_ops_forgotten_cb"))
256+
test_struct_ops_forgotten_cb();
212257
}
213258

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "../bpf_testmod/bpf_testmod.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
SEC("struct_ops/test_1")
10+
int BPF_PROG(test_1_forgotten)
11+
{
12+
return 0;
13+
}
14+
15+
SEC(".struct_ops.link")
16+
struct bpf_testmod_ops ops = {
17+
/* we forgot to reference test_1_forgotten above, oops */
18+
};
19+

0 commit comments

Comments
 (0)