Commit ba7499bc authored by Alan Maguire's avatar Alan Maguire Committed by Andrii Nakryiko

selftests/bpf: Add tests for u[ret]probe attach by name

add tests that verify attaching by name for

1. local functions in a program
2. library functions in a shared object

...succeed for uprobe and uretprobes using new "func_name"
option for bpf_program__attach_uprobe_opts().  Also verify
auto-attach works where uprobe, path to binary and function
name are specified, but fails with -EOPNOTSUPP with a SEC
name that does not specify binary path/function.
Signed-off-by: default avatarAlan Maguire <alan.maguire@oracle.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/1648654000-21758-5-git-send-email-alan.maguire@oracle.com
parent 39f8dc43
...@@ -11,15 +11,22 @@ static void trigger_func(void) ...@@ -11,15 +11,22 @@ static void trigger_func(void)
asm volatile (""); asm volatile ("");
} }
/* attach point for byname uprobe */
static void trigger_func2(void)
{
asm volatile ("");
}
void test_attach_probe(void) void test_attach_probe(void)
{ {
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts); DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
int duration = 0;
struct bpf_link *kprobe_link, *kretprobe_link; struct bpf_link *kprobe_link, *kretprobe_link;
struct bpf_link *uprobe_link, *uretprobe_link; struct bpf_link *uprobe_link, *uretprobe_link;
struct test_attach_probe* skel; struct test_attach_probe* skel;
ssize_t uprobe_offset, ref_ctr_offset; ssize_t uprobe_offset, ref_ctr_offset;
struct bpf_link *uprobe_err_link;
bool legacy; bool legacy;
char *mem;
/* Check if new-style kprobe/uprobe API is supported. /* Check if new-style kprobe/uprobe API is supported.
* Kernels that support new FD-based kprobe and uprobe BPF attachment * Kernels that support new FD-based kprobe and uprobe BPF attachment
...@@ -43,9 +50,9 @@ void test_attach_probe(void) ...@@ -43,9 +50,9 @@ void test_attach_probe(void)
return; return;
skel = test_attach_probe__open_and_load(); skel = test_attach_probe__open_and_load();
if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) if (!ASSERT_OK_PTR(skel, "skel_open"))
return; return;
if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n")) if (!ASSERT_OK_PTR(skel->bss, "check_bss"))
goto cleanup; goto cleanup;
kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe, kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
...@@ -90,25 +97,73 @@ void test_attach_probe(void) ...@@ -90,25 +97,73 @@ void test_attach_probe(void)
goto cleanup; goto cleanup;
skel->links.handle_uretprobe = uretprobe_link; skel->links.handle_uretprobe = uretprobe_link;
/* trigger & validate kprobe && kretprobe */ /* verify auto-attach fails for old-style uprobe definition */
usleep(1); uprobe_err_link = bpf_program__attach(skel->progs.handle_uprobe_byname);
if (!ASSERT_EQ(libbpf_get_error(uprobe_err_link), -EOPNOTSUPP,
"auto-attach should fail for old-style name"))
goto cleanup;
uprobe_opts.func_name = "trigger_func2";
uprobe_opts.retprobe = false;
uprobe_opts.ref_ctr_offset = 0;
skel->links.handle_uprobe_byname =
bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname,
0 /* this pid */,
"/proc/self/exe",
0, &uprobe_opts);
if (!ASSERT_OK_PTR(skel->links.handle_uprobe_byname, "attach_uprobe_byname"))
goto cleanup;
/* verify auto-attach works */
skel->links.handle_uretprobe_byname =
bpf_program__attach(skel->progs.handle_uretprobe_byname);
if (!ASSERT_OK_PTR(skel->links.handle_uretprobe_byname, "attach_uretprobe_byname"))
goto cleanup;
if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res", /* test attach by name for a library function, using the library
"wrong kprobe res: %d\n", skel->bss->kprobe_res)) * as the binary argument. libc.so.6 will be resolved via dlopen()/dlinfo().
*/
uprobe_opts.func_name = "malloc";
uprobe_opts.retprobe = false;
skel->links.handle_uprobe_byname2 =
bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname2,
0 /* this pid */,
"libc.so.6",
0, &uprobe_opts);
if (!ASSERT_OK_PTR(skel->links.handle_uprobe_byname2, "attach_uprobe_byname2"))
goto cleanup; goto cleanup;
if (CHECK(skel->bss->kretprobe_res != 2, "check_kretprobe_res",
"wrong kretprobe res: %d\n", skel->bss->kretprobe_res)) uprobe_opts.func_name = "free";
uprobe_opts.retprobe = true;
skel->links.handle_uretprobe_byname2 =
bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe_byname2,
-1 /* any pid */,
"libc.so.6",
0, &uprobe_opts);
if (!ASSERT_OK_PTR(skel->links.handle_uretprobe_byname2, "attach_uretprobe_byname2"))
goto cleanup; goto cleanup;
/* trigger & validate kprobe && kretprobe */
usleep(1);
/* trigger & validate shared library u[ret]probes attached by name */
mem = malloc(1);
free(mem);
/* trigger & validate uprobe & uretprobe */ /* trigger & validate uprobe & uretprobe */
trigger_func(); trigger_func();
if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res", /* trigger & validate uprobe attached by name */
"wrong uprobe res: %d\n", skel->bss->uprobe_res)) trigger_func2();
goto cleanup;
if (CHECK(skel->bss->uretprobe_res != 4, "check_uretprobe_res", ASSERT_EQ(skel->bss->kprobe_res, 1, "check_kprobe_res");
"wrong uretprobe res: %d\n", skel->bss->uretprobe_res)) ASSERT_EQ(skel->bss->kretprobe_res, 2, "check_kretprobe_res");
goto cleanup; 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");
ASSERT_EQ(skel->bss->uretprobe_byname_res, 6, "check_uretprobe_byname_res");
ASSERT_EQ(skel->bss->uprobe_byname2_res, 7, "check_uprobe_byname2_res");
ASSERT_EQ(skel->bss->uretprobe_byname2_res, 8, "check_uretprobe_byname2_res");
cleanup: cleanup:
test_attach_probe__destroy(skel); test_attach_probe__destroy(skel);
......
...@@ -10,6 +10,10 @@ int kprobe_res = 0; ...@@ -10,6 +10,10 @@ int kprobe_res = 0;
int kretprobe_res = 0; int kretprobe_res = 0;
int uprobe_res = 0; int uprobe_res = 0;
int uretprobe_res = 0; int uretprobe_res = 0;
int uprobe_byname_res = 0;
int uretprobe_byname_res = 0;
int uprobe_byname2_res = 0;
int uretprobe_byname2_res = 0;
SEC("kprobe/sys_nanosleep") SEC("kprobe/sys_nanosleep")
int handle_kprobe(struct pt_regs *ctx) int handle_kprobe(struct pt_regs *ctx)
...@@ -25,18 +29,51 @@ int BPF_KRETPROBE(handle_kretprobe) ...@@ -25,18 +29,51 @@ int BPF_KRETPROBE(handle_kretprobe)
return 0; return 0;
} }
SEC("uprobe/trigger_func") SEC("uprobe")
int handle_uprobe(struct pt_regs *ctx) int handle_uprobe(struct pt_regs *ctx)
{ {
uprobe_res = 3; uprobe_res = 3;
return 0; return 0;
} }
SEC("uretprobe/trigger_func") SEC("uretprobe")
int handle_uretprobe(struct pt_regs *ctx) int handle_uretprobe(struct pt_regs *ctx)
{ {
uretprobe_res = 4; uretprobe_res = 4;
return 0; return 0;
} }
SEC("uprobe")
int handle_uprobe_byname(struct pt_regs *ctx)
{
uprobe_byname_res = 5;
return 0;
}
/* use auto-attach format for section definition. */
SEC("uretprobe//proc/self/exe:trigger_func2")
int handle_uretprobe_byname(struct pt_regs *ctx)
{
uretprobe_byname_res = 6;
return 0;
}
SEC("uprobe")
int handle_uprobe_byname2(struct pt_regs *ctx)
{
unsigned int size = PT_REGS_PARM1(ctx);
/* verify malloc size */
if (size == 1)
uprobe_byname2_res = 7;
return 0;
}
SEC("uretprobe")
int handle_uretprobe_byname2(struct pt_regs *ctx)
{
uretprobe_byname2_res = 8;
return 0;
}
char _license[] SEC("license") = "GPL"; char _license[] SEC("license") = "GPL";
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment