提交 32c03c49 编写于 作者: A Andrii Nakryiko 提交者: Daniel Borkmann

selftests/bpf: Use target-less SEC() definitions in various tests

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: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NDaniel Borkmann <daniel@iogearbox.net>
Acked-by: NSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20220428185349.3799599-4-andrii@kernel.org
上级 cc7d8f2c
...@@ -55,6 +55,7 @@ void test_attach_probe(void) ...@@ -55,6 +55,7 @@ void test_attach_probe(void)
if (!ASSERT_OK_PTR(skel->bss, "check_bss")) if (!ASSERT_OK_PTR(skel->bss, "check_bss"))
goto cleanup; goto cleanup;
/* manual-attach kprobe/kretprobe */
kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe, kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
false /* retprobe */, false /* retprobe */,
SYS_NANOSLEEP_KPROBE_NAME); SYS_NANOSLEEP_KPROBE_NAME);
...@@ -69,6 +70,13 @@ void test_attach_probe(void) ...@@ -69,6 +70,13 @@ void test_attach_probe(void)
goto cleanup; goto cleanup;
skel->links.handle_kretprobe = kretprobe_link; 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) if (!legacy)
ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_before"); ASSERT_EQ(uprobe_ref_ctr, 0, "uprobe_ref_ctr_before");
...@@ -157,7 +165,9 @@ void test_attach_probe(void) ...@@ -157,7 +165,9 @@ void test_attach_probe(void)
trigger_func2(); trigger_func2();
ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res"); 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->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->uprobe_res, 3, "check_uprobe_res");
ASSERT_EQ(skel->bss->uretprobe_res, 4, "check_uretprobe_res"); ASSERT_EQ(skel->bss->uretprobe_res, 4, "check_uretprobe_res");
ASSERT_EQ(skel->bss->uprobe_byname_res, 5, "check_uprobe_byname_res"); ASSERT_EQ(skel->bss->uprobe_byname_res, 5, "check_uprobe_byname_res");
......
...@@ -140,14 +140,14 @@ test_attach_api(const char *pattern, struct bpf_kprobe_multi_opts *opts) ...@@ -140,14 +140,14 @@ test_attach_api(const char *pattern, struct bpf_kprobe_multi_opts *opts)
goto cleanup; goto cleanup;
skel->bss->pid = getpid(); 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); pattern, opts);
if (!ASSERT_OK_PTR(link1, "bpf_program__attach_kprobe_multi_opts")) if (!ASSERT_OK_PTR(link1, "bpf_program__attach_kprobe_multi_opts"))
goto cleanup; goto cleanup;
if (opts) { if (opts) {
opts->retprobe = true; 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); pattern, opts);
if (!ASSERT_OK_PTR(link2, "bpf_program__attach_kprobe_multi_opts")) if (!ASSERT_OK_PTR(link2, "bpf_program__attach_kprobe_multi_opts"))
goto cleanup; goto cleanup;
...@@ -232,7 +232,7 @@ static void test_attach_api_fails(void) ...@@ -232,7 +232,7 @@ static void test_attach_api_fails(void)
skel->bss->pid = getpid(); skel->bss->pid = getpid();
/* fail_1 - pattern and opts NULL */ /* 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); NULL, NULL);
if (!ASSERT_ERR_PTR(link, "fail_1")) if (!ASSERT_ERR_PTR(link, "fail_1"))
goto cleanup; goto cleanup;
...@@ -246,7 +246,7 @@ static void test_attach_api_fails(void) ...@@ -246,7 +246,7 @@ static void test_attach_api_fails(void)
opts.cnt = ARRAY_SIZE(syms); opts.cnt = ARRAY_SIZE(syms);
opts.cookies = NULL; 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); NULL, &opts);
if (!ASSERT_ERR_PTR(link, "fail_2")) if (!ASSERT_ERR_PTR(link, "fail_2"))
goto cleanup; goto cleanup;
...@@ -260,7 +260,7 @@ static void test_attach_api_fails(void) ...@@ -260,7 +260,7 @@ static void test_attach_api_fails(void)
opts.cnt = ARRAY_SIZE(syms); opts.cnt = ARRAY_SIZE(syms);
opts.cookies = NULL; 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); "ksys_*", &opts);
if (!ASSERT_ERR_PTR(link, "fail_3")) if (!ASSERT_ERR_PTR(link, "fail_3"))
goto cleanup; goto cleanup;
...@@ -274,7 +274,7 @@ static void test_attach_api_fails(void) ...@@ -274,7 +274,7 @@ static void test_attach_api_fails(void)
opts.cnt = ARRAY_SIZE(syms); opts.cnt = ARRAY_SIZE(syms);
opts.cookies = NULL; 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); "ksys_*", &opts);
if (!ASSERT_ERR_PTR(link, "fail_4")) if (!ASSERT_ERR_PTR(link, "fail_4"))
goto cleanup; goto cleanup;
...@@ -288,7 +288,7 @@ static void test_attach_api_fails(void) ...@@ -288,7 +288,7 @@ static void test_attach_api_fails(void)
opts.cnt = 0; opts.cnt = 0;
opts.cookies = cookies; 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); "ksys_*", &opts);
if (!ASSERT_ERR_PTR(link, "fail_5")) if (!ASSERT_ERR_PTR(link, "fail_5"))
goto cleanup; goto cleanup;
......
...@@ -98,3 +98,17 @@ int test_kretprobe(struct pt_regs *ctx) ...@@ -98,3 +98,17 @@ int test_kretprobe(struct pt_regs *ctx)
kprobe_multi_check(ctx, true); kprobe_multi_check(ctx, true);
return 0; 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;
}
...@@ -5,9 +5,12 @@ ...@@ -5,9 +5,12 @@
#include <linux/bpf.h> #include <linux/bpf.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#include "bpf_misc.h"
int kprobe_res = 0; int kprobe_res = 0;
int kprobe2_res = 0;
int kretprobe_res = 0; int kretprobe_res = 0;
int kretprobe2_res = 0;
int uprobe_res = 0; int uprobe_res = 0;
int uretprobe_res = 0; int uretprobe_res = 0;
int uprobe_byname_res = 0; int uprobe_byname_res = 0;
...@@ -15,20 +18,34 @@ int uretprobe_byname_res = 0; ...@@ -15,20 +18,34 @@ int uretprobe_byname_res = 0;
int uprobe_byname2_res = 0; int uprobe_byname2_res = 0;
int uretprobe_byname2_res = 0; int uretprobe_byname2_res = 0;
SEC("kprobe/sys_nanosleep") SEC("kprobe")
int handle_kprobe(struct pt_regs *ctx) int handle_kprobe(struct pt_regs *ctx)
{ {
kprobe_res = 1; kprobe_res = 1;
return 0; return 0;
} }
SEC("kretprobe/sys_nanosleep") SEC("kprobe/" SYS_PREFIX "sys_nanosleep")
int BPF_KRETPROBE(handle_kretprobe) int BPF_KPROBE(handle_kprobe_auto)
{
kprobe2_res = 11;
return 0;
}
SEC("kretprobe")
int handle_kretprobe(struct pt_regs *ctx)
{ {
kretprobe_res = 2; kretprobe_res = 2;
return 0; return 0;
} }
SEC("kretprobe/" SYS_PREFIX "sys_nanosleep")
int BPF_KRETPROBE(handle_kretprobe_auto)
{
kretprobe2_res = 22;
return 0;
}
SEC("uprobe") SEC("uprobe")
int handle_uprobe(struct pt_regs *ctx) int handle_uprobe(struct pt_regs *ctx)
{ {
......
...@@ -64,7 +64,7 @@ int BPF_PROG(handle_fentry, ...@@ -64,7 +64,7 @@ int BPF_PROG(handle_fentry,
__u32 fentry_manual_read_sz = 0; __u32 fentry_manual_read_sz = 0;
SEC("fentry/placeholder") SEC("fentry")
int BPF_PROG(handle_fentry_manual, int BPF_PROG(handle_fentry_manual,
struct file *file, struct kobject *kobj, struct file *file, struct kobject *kobj,
struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len) struct bin_attribute *bin_attr, char *buf, loff_t off, size_t len)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册