Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2025 Meta */
3#include <test_progs.h>
4#include "clone_attach_btf_id.skel.h"
5
6/*
7 * Test that bpf_program__clone() respects caller-provided attach_btf_id
8 * override via bpf_prog_load_opts.
9 *
10 * The BPF program has SEC("fentry/bpf_fentry_test1"). Clone it twice
11 * from the same prepared object: first with no opts (callback resolves
12 * attach_btf_id from sec_name), then with attach_btf_id overridden to
13 * bpf_fentry_test2. Verify each loaded program's attach_btf_id via
14 * bpf_prog_get_info_by_fd().
15 */
16
17static int get_prog_attach_btf_id(int prog_fd)
18{
19 struct bpf_prog_info info = {};
20 __u32 info_len = sizeof(info);
21 int err;
22
23 err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
24 if (err)
25 return err;
26 return info.attach_btf_id;
27}
28
29void test_clone_attach_btf_id(void)
30{
31 struct clone_attach_btf_id *skel;
32 int fd1 = -1, fd2 = -1, err;
33 int btf_id_test1, btf_id_test2;
34
35 btf_id_test1 = libbpf_find_vmlinux_btf_id("bpf_fentry_test1", BPF_TRACE_FENTRY);
36 if (!ASSERT_GT(btf_id_test1, 0, "find_btf_id_test1"))
37 return;
38
39 btf_id_test2 = libbpf_find_vmlinux_btf_id("bpf_fentry_test2", BPF_TRACE_FENTRY);
40 if (!ASSERT_GT(btf_id_test2, 0, "find_btf_id_test2"))
41 return;
42
43 skel = clone_attach_btf_id__open();
44 if (!ASSERT_OK_PTR(skel, "skel_open"))
45 return;
46
47 err = bpf_object__prepare(skel->obj);
48 if (!ASSERT_OK(err, "obj_prepare"))
49 goto out;
50
51 /* Clone with no opts — callback resolves BTF from sec_name */
52 fd1 = bpf_program__clone(skel->progs.fentry_handler, NULL);
53 if (!ASSERT_GE(fd1, 0, "clone_default"))
54 goto out;
55 ASSERT_EQ(get_prog_attach_btf_id(fd1), btf_id_test1,
56 "attach_btf_id_default");
57
58 /*
59 * Clone with attach_btf_id override pointing to a different
60 * function. The BPF program never accesses arguments, so the
61 * load succeeds regardless of signature mismatch.
62 */
63 LIBBPF_OPTS(bpf_prog_load_opts, opts,
64 .attach_btf_id = btf_id_test2,
65 );
66 fd2 = bpf_program__clone(skel->progs.fentry_handler, &opts);
67 if (!ASSERT_GE(fd2, 0, "clone_override"))
68 goto out;
69 ASSERT_EQ(get_prog_attach_btf_id(fd2), btf_id_test2,
70 "attach_btf_id_override");
71
72out:
73 if (fd1 >= 0)
74 close(fd1);
75 if (fd2 >= 0)
76 close(fd2);
77 clone_attach_btf_id__destroy(skel);
78}