selftests/bpf: Use target-less SEC() definitions in various tests
authorAndrii Nakryiko <andrii@kernel.org>
Thu, 28 Apr 2022 18:53:49 +0000 (11:53 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 28 Apr 2022 21:46:04 +0000 (23:46 +0200)
Add new or modify existing SEC() definitions to be target-less and
validate that libbpf handles such program definitions correctly.

For kprobe/kretprobe we also add explicit test that generic
bpf_program__attach() works in cases when kprobe definition contains
proper target. It wasn't previously tested as selftests code always
explicitly specified the target regardless.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Song Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20220428185349.3799599-4-andrii@kernel.org
tools/testing/selftests/bpf/prog_tests/attach_probe.c
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
tools/testing/selftests/bpf/progs/kprobe_multi.c
tools/testing/selftests/bpf/progs/test_attach_probe.c
tools/testing/selftests/bpf/progs/test_module_attach.c

index c0c6d410751d24476455b864ee2c4e83d34b9876..08c0601b3e84f29bddf36956eca2bd7014e8bea2 100644 (file)
@@ -55,6 +55,7 @@ void test_attach_probe(void)
        if (!ASSERT_OK_PTR(skel->bss, "check_bss"))
                goto cleanup;
 
+       /* manual-attach kprobe/kretprobe */
        kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
                                                 false /* retprobe */,
                                                 SYS_NANOSLEEP_KPROBE_NAME);
@@ -69,6 +70,13 @@ void test_attach_probe(void)
                goto cleanup;
        skel->links.handle_kretprobe = kretprobe_link;
 
+       /* auto-attachable kprobe and kretprobe */
+       skel->links.handle_kprobe_auto = bpf_program__attach(skel->progs.handle_kprobe_auto);
+       ASSERT_OK_PTR(skel->links.handle_kprobe_auto, "attach_kprobe_auto");
+
+       skel->links.handle_kretprobe_auto = bpf_program__attach(skel->progs.handle_kretprobe_auto);
+       ASSERT_OK_PTR(skel->links.handle_kretprobe_auto, "attach_kretprobe_auto");
+
        if (!legacy)
                ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_before");
 
@@ -157,7 +165,9 @@ void test_attach_probe(void)
        trigger_func2();
 
        ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res");
+       ASSERT_EQ(skel->bss->kprobe2_res, 11, "check_kprobe_auto_res");
        ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res");
+       ASSERT_EQ(skel->bss->kretprobe2_res, 22, "check_kretprobe_auto_res");
        ASSERT_EQ(skel->bss->uprobe_res, 3, "check_uprobe_res");
        ASSERT_EQ(skel->bss->uretprobe_res, 4, "check_uretprobe_res");
        ASSERT_EQ(skel->bss->uprobe_byname_res, 5, "check_uprobe_byname_res");
index b9876b55fc0c8956b04ead48f3975843ac60a83d..c56db65d4c151e6f318a3c6e8b8386494722f796 100644 (file)
@@ -140,14 +140,14 @@ test_attach_api(const char *pattern, struct bpf_kprobe_multi_opts *opts)
                goto cleanup;
 
        skel->bss->pid = getpid();
-       link1 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
+       link1 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
                                                      pattern, opts);
        if (!ASSERT_OK_PTR(link1, "bpf_program__attach_kprobe_multi_opts"))
                goto cleanup;
 
        if (opts) {
                opts->retprobe = true;
-               link2 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kretprobe,
+               link2 = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kretprobe_manual,
                                                              pattern, opts);
                if (!ASSERT_OK_PTR(link2, "bpf_program__attach_kprobe_multi_opts"))
                        goto cleanup;
@@ -232,7 +232,7 @@ static void test_attach_api_fails(void)
        skel->bss->pid = getpid();
 
        /* fail_1 - pattern and opts NULL */
-       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
+       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
                                                     NULL, NULL);
        if (!ASSERT_ERR_PTR(link, "fail_1"))
                goto cleanup;
@@ -246,7 +246,7 @@ static void test_attach_api_fails(void)
        opts.cnt = ARRAY_SIZE(syms);
        opts.cookies = NULL;
 
-       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
+       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
                                                     NULL, &opts);
        if (!ASSERT_ERR_PTR(link, "fail_2"))
                goto cleanup;
@@ -260,7 +260,7 @@ static void test_attach_api_fails(void)
        opts.cnt = ARRAY_SIZE(syms);
        opts.cookies = NULL;
 
-       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
+       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
                                                     "ksys_*", &opts);
        if (!ASSERT_ERR_PTR(link, "fail_3"))
                goto cleanup;
@@ -274,7 +274,7 @@ static void test_attach_api_fails(void)
        opts.cnt = ARRAY_SIZE(syms);
        opts.cookies = NULL;
 
-       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
+       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
                                                     "ksys_*", &opts);
        if (!ASSERT_ERR_PTR(link, "fail_4"))
                goto cleanup;
@@ -288,7 +288,7 @@ static void test_attach_api_fails(void)
        opts.cnt = 0;
        opts.cookies = cookies;
 
-       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe,
+       link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_kprobe_manual,
                                                     "ksys_*", &opts);
        if (!ASSERT_ERR_PTR(link, "fail_5"))
                goto cleanup;
index 600be50800f837eaf996921149d05eab28ba35f7..93510f4f0f3aa9ba228beff9f0a8dacbff52c4e8 100644 (file)
@@ -98,3 +98,17 @@ int test_kretprobe(struct pt_regs *ctx)
        kprobe_multi_check(ctx, true);
        return 0;
 }
+
+SEC("kprobe.multi")
+int test_kprobe_manual(struct pt_regs *ctx)
+{
+       kprobe_multi_check(ctx, false);
+       return 0;
+}
+
+SEC("kretprobe.multi")
+int test_kretprobe_manual(struct pt_regs *ctx)
+{
+       kprobe_multi_check(ctx, true);
+       return 0;
+}
index af994d16bb10f4a5a153f9a4c9947ee47eb6bf66..ce9acf4db8d2f0ea10151e8ea0b8eeed0b3c3460 100644 (file)
@@ -5,9 +5,12 @@
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
 
 int kprobe_res = 0;
+int kprobe2_res = 0;
 int kretprobe_res = 0;
+int kretprobe2_res = 0;
 int uprobe_res = 0;
 int uretprobe_res = 0;
 int uprobe_byname_res = 0;
@@ -15,20 +18,34 @@ int uretprobe_byname_res = 0;
 int uprobe_byname2_res = 0;
 int uretprobe_byname2_res = 0;
 
-SEC("kprobe/sys_nanosleep")
+SEC("kprobe")
 int handle_kprobe(struct pt_regs *ctx)
 {
        kprobe_res = 1;
        return 0;
 }
 
-SEC("kretprobe/sys_nanosleep")
-int BPF_KRETPROBE(handle_kretprobe)
+SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
+int BPF_KPROBE(handle_kprobe_auto)
+{
+       kprobe2_res = 11;
+       return 0;
+}
+
+SEC("kretprobe")
+int handle_kretprobe(struct pt_regs *ctx)
 {
        kretprobe_res = 2;
        return 0;
 }
 
+SEC("kretprobe/" SYS_PREFIX "sys_nanosleep")
+int BPF_KRETPROBE(handle_kretprobe_auto)
+{
+       kretprobe2_res = 22;
+       return 0;
+}
+
 SEC("uprobe")
 int handle_uprobe(struct pt_regs *ctx)
 {
index 50ce16d02da7b1a9a4bdfe1ec0f0276e8603e1e3..08628afedb779e7aac8039f74e5147bfbc928f82 100644 (file)
@@ -64,7 +64,7 @@ int BPF_PROG(handle_fentry,
 
 __u32 fentry_manual_read_sz = 0;
 
-SEC("fentry/placeholder")
+SEC("fentry")
 int BPF_PROG(handle_fentry_manual,
             struct file *file, struct kobject *kobj,
             struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)