selftests/bpf: Test re-attachment fix for bpf_tracing_prog_attach
authorDmitrii Dolgov <9erthalion6@gmail.com>
Wed, 3 Jan 2024 19:05:47 +0000 (20:05 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 5 Jan 2024 04:40:49 +0000 (20:40 -0800)
Add a test case to verify the fix for "prog->aux->dst_trampoline and
tgt_prog is NULL" branch in bpf_tracing_prog_attach. The sequence of
events:

1. load rawtp program
2. load fentry program with rawtp as target_fd
3. create tracing link for fentry program with target_fd = 0
4. repeat 3

Acked-by: Jiri Olsa <olsajiri@gmail.com>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
Link: https://lore.kernel.org/r/20240103190559.14750-5-9erthalion6@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/recursive_attach.c
tools/testing/selftests/bpf/progs/fentry_recursive_target.c

index 4bd0a0e4231ebbe018d3dfb67a5b0b5a3c5046d3..8100509e561b2ce2609243f29699b677de3eb5f1 100644 (file)
@@ -105,3 +105,47 @@ void test_recursive_fentry(void)
        if (test__start_subtest("detach"))
                test_recursive_fentry_chain(true, true);
 }
+
+/* Test that a tracing prog reattachment (when we land in
+ * "prog->aux->dst_trampoline and tgt_prog is NULL" branch in
+ * bpf_tracing_prog_attach) does not lead to a crash due to missing attach_btf
+ */
+void test_fentry_attach_btf_presence(void)
+{
+       struct fentry_recursive_target *target_skel = NULL;
+       struct fentry_recursive *tracing_skel = NULL;
+       struct bpf_program *prog;
+       int err, link_fd, tgt_prog_fd;
+
+       target_skel = fentry_recursive_target__open_and_load();
+       if (!ASSERT_OK_PTR(target_skel, "fentry_recursive_target__open_and_load"))
+               goto close_prog;
+
+       tracing_skel = fentry_recursive__open();
+       if (!ASSERT_OK_PTR(tracing_skel, "fentry_recursive__open"))
+               goto close_prog;
+
+       prog = tracing_skel->progs.recursive_attach;
+       tgt_prog_fd = bpf_program__fd(target_skel->progs.fentry_target);
+       err = bpf_program__set_attach_target(prog, tgt_prog_fd, "fentry_target");
+       if (!ASSERT_OK(err, "bpf_program__set_attach_target"))
+               goto close_prog;
+
+       err = fentry_recursive__load(tracing_skel);
+       if (!ASSERT_OK(err, "fentry_recursive__load"))
+               goto close_prog;
+
+       tgt_prog_fd = bpf_program__fd(tracing_skel->progs.recursive_attach);
+       link_fd = bpf_link_create(tgt_prog_fd, 0, BPF_TRACE_FENTRY, NULL);
+       if (!ASSERT_GE(link_fd, 0, "link_fd"))
+               goto close_prog;
+
+       fentry_recursive__detach(tracing_skel);
+
+       err = fentry_recursive__attach(tracing_skel);
+       ASSERT_ERR(err, "fentry_recursive__attach");
+
+close_prog:
+       fentry_recursive_target__destroy(target_skel);
+       fentry_recursive__destroy(tracing_skel);
+}
index cb18a8bfffac2503825f82e2225d5154aa8bbc55..267c876d0aba57927a4b200d23a38461795c276d 100644 (file)
@@ -14,3 +14,12 @@ int BPF_PROG(test1, int a)
 {
        return 0;
 }
+
+/* Dummy bpf prog for testing attach_btf presence when attaching an fentry
+ * program.
+ */
+SEC("raw_tp/sys_enter")
+int BPF_PROG(fentry_target, struct pt_regs *regs, long id)
+{
+       return 0;
+}