Skip to content

Commit 5a40e8b

Browse files
alan-maguireNobody
authored and
Nobody
committed
selftests/bpf: uprobe tests should verify param/return values
uprobe/uretprobe tests don't do any validation of arguments/return values, and without this we can't be sure we are attached to the right function, or that we are indeed attached to a uprobe or uretprobe. To fix this record argument and return value for auto-attached functions and ensure these match expectations. Also need to filter by pid to ensure we do not pick up stray malloc()s since auto-attach traces libc system-wide. Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alan Maguire <[email protected]>
1 parent 29e3e46 commit 5a40e8b

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
#include "test_uprobe_autoattach.skel.h"
66

77
/* uprobe attach point */
8-
static void autoattach_trigger_func(void)
8+
static noinline int autoattach_trigger_func(int arg)
99
{
1010
asm volatile ("");
11+
return arg + 1;
1112
}
1213

1314
void test_uprobe_autoattach(void)
1415
{
1516
struct test_uprobe_autoattach *skel;
17+
int trigger_val = 100, trigger_ret;
18+
size_t malloc_sz = 1;
1619
char *mem;
1720

1821
skel = test_uprobe_autoattach__open_and_load();
@@ -22,17 +25,25 @@ void test_uprobe_autoattach(void)
2225
if (!ASSERT_OK(test_uprobe_autoattach__attach(skel), "skel_attach"))
2326
goto cleanup;
2427

28+
skel->bss->test_pid = getpid();
29+
2530
/* trigger & validate uprobe & uretprobe */
26-
autoattach_trigger_func();
31+
trigger_ret = autoattach_trigger_func(trigger_val);
32+
33+
skel->bss->test_pid = getpid();
2734

2835
/* trigger & validate shared library u[ret]probes attached by name */
29-
mem = malloc(1);
36+
mem = malloc(malloc_sz);
3037
free(mem);
3138

32-
ASSERT_EQ(skel->bss->uprobe_byname_res, 1, "check_uprobe_byname_res");
33-
ASSERT_EQ(skel->bss->uretprobe_byname_res, 2, "check_uretprobe_byname_res");
34-
ASSERT_EQ(skel->bss->uprobe_byname2_res, 3, "check_uprobe_byname2_res");
35-
ASSERT_EQ(skel->bss->uretprobe_byname2_res, 4, "check_uretprobe_byname2_res");
39+
ASSERT_EQ(skel->bss->uprobe_byname_parm1, trigger_val, "check_uprobe_byname_parm1");
40+
ASSERT_EQ(skel->bss->uprobe_byname_ran, 1, "check_uprobe_byname_ran");
41+
ASSERT_EQ(skel->bss->uretprobe_byname_rc, trigger_ret, "check_uretprobe_byname_rc");
42+
ASSERT_EQ(skel->bss->uretprobe_byname_ran, 2, "check_uretprobe_byname_ran");
43+
ASSERT_EQ(skel->bss->uprobe_byname2_parm1, malloc_sz, "check_uprobe_byname2_parm1");
44+
ASSERT_EQ(skel->bss->uprobe_byname2_ran, 3, "check_uprobe_byname2_ran");
45+
ASSERT_EQ(skel->bss->uretprobe_byname2_rc, mem, "check_uretprobe_byname2_rc");
46+
ASSERT_EQ(skel->bss->uretprobe_byname2_ran, 4, "check_uretprobe_byname2_ran");
3647
cleanup:
3748
test_uprobe_autoattach__destroy(skel);
3849
}

tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2022, Oracle and/or its affiliates. */
33

4-
#include <linux/ptrace.h>
5-
#include <linux/bpf.h>
4+
#include "vmlinux.h"
5+
6+
#include <bpf/bpf_core_read.h>
67
#include <bpf/bpf_helpers.h>
78
#include <bpf/bpf_tracing.h>
89

9-
int uprobe_byname_res = 0;
10-
int uretprobe_byname_res = 0;
11-
int uprobe_byname2_res = 0;
12-
int uretprobe_byname2_res = 0;
10+
int uprobe_byname_parm1 = 0;
11+
int uprobe_byname_ran = 0;
12+
int uretprobe_byname_rc = 0;
13+
int uretprobe_byname_ran = 0;
14+
size_t uprobe_byname2_parm1 = 0;
15+
int uprobe_byname2_ran = 0;
16+
char *uretprobe_byname2_rc = NULL;
17+
int uretprobe_byname2_ran = 0;
18+
19+
int test_pid;
1320

1421
/* This program cannot auto-attach, but that should not stop other
1522
* programs from attaching.
@@ -23,29 +30,43 @@ int handle_uprobe_noautoattach(struct pt_regs *ctx)
2330
SEC("uprobe//proc/self/exe:autoattach_trigger_func")
2431
int handle_uprobe_byname(struct pt_regs *ctx)
2532
{
26-
uprobe_byname_res = 1;
33+
uprobe_byname_parm1 = PT_REGS_PARM1_CORE(ctx);
34+
uprobe_byname_ran = 1;
2735
return 0;
2836
}
2937

3038
SEC("uretprobe//proc/self/exe:autoattach_trigger_func")
3139
int handle_uretprobe_byname(struct pt_regs *ctx)
3240
{
33-
uretprobe_byname_res = 2;
41+
uretprobe_byname_rc = PT_REGS_RC_CORE(ctx);
42+
uretprobe_byname_ran = 2;
3443
return 0;
3544
}
3645

3746

3847
SEC("uprobe/libc.so.6:malloc")
3948
int handle_uprobe_byname2(struct pt_regs *ctx)
4049
{
41-
uprobe_byname2_res = 3;
50+
int pid = bpf_get_current_pid_tgid() >> 32;
51+
52+
/* ignore irrelevant invocations */
53+
if (test_pid != pid)
54+
return 0;
55+
uprobe_byname2_parm1 = PT_REGS_PARM1_CORE(ctx);
56+
uprobe_byname2_ran = 3;
4257
return 0;
4358
}
4459

45-
SEC("uretprobe/libc.so.6:free")
60+
SEC("uretprobe/libc.so.6:malloc")
4661
int handle_uretprobe_byname2(struct pt_regs *ctx)
4762
{
48-
uretprobe_byname2_res = 4;
63+
int pid = bpf_get_current_pid_tgid() >> 32;
64+
65+
/* ignore irrelevant invocations */
66+
if (test_pid != pid)
67+
return 0;
68+
uretprobe_byname2_rc = (char *)PT_REGS_RC_CORE(ctx);
69+
uretprobe_byname2_ran = 4;
4970
return 0;
5071
}
5172

0 commit comments

Comments
 (0)