Skip to content

Commit e7cf9a4

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
selftests/bpf: Add uprobe_multi cookie test
Adding test for cookies setup/retrieval in uprobe_link uprobes and making sure bpf_get_attach_cookie works properly. Signed-off-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 85209e8 commit e7cf9a4

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <bpf/btf.h>
1212
#include "test_bpf_cookie.skel.h"
1313
#include "kprobe_multi.skel.h"
14+
#include "uprobe_multi.skel.h"
1415

1516
/* uprobe attach point */
1617
static noinline void trigger_func(void)
@@ -239,6 +240,81 @@ static void kprobe_multi_attach_api_subtest(void)
239240
bpf_link__destroy(link1);
240241
kprobe_multi__destroy(skel);
241242
}
243+
244+
/* defined in prog_tests/uprobe_multi_test.c */
245+
void uprobe_multi_func_1(void);
246+
void uprobe_multi_func_2(void);
247+
void uprobe_multi_func_3(void);
248+
249+
static void uprobe_multi_test_run(struct uprobe_multi *skel)
250+
{
251+
skel->bss->uprobe_multi_func_1_addr = (__u64) uprobe_multi_func_1;
252+
skel->bss->uprobe_multi_func_2_addr = (__u64) uprobe_multi_func_2;
253+
skel->bss->uprobe_multi_func_3_addr = (__u64) uprobe_multi_func_3;
254+
255+
skel->bss->pid = getpid();
256+
skel->bss->test_cookie = true;
257+
258+
uprobe_multi_func_1();
259+
uprobe_multi_func_2();
260+
uprobe_multi_func_3();
261+
262+
ASSERT_EQ(skel->bss->uprobe_multi_func_1_result, 1, "uprobe_multi_func_1_result");
263+
ASSERT_EQ(skel->bss->uprobe_multi_func_2_result, 1, "uprobe_multi_func_2_result");
264+
ASSERT_EQ(skel->bss->uprobe_multi_func_3_result, 1, "uprobe_multi_func_3_result");
265+
266+
ASSERT_EQ(skel->bss->uretprobe_multi_func_1_result, 1, "uretprobe_multi_func_1_result");
267+
ASSERT_EQ(skel->bss->uretprobe_multi_func_2_result, 1, "uretprobe_multi_func_2_result");
268+
ASSERT_EQ(skel->bss->uretprobe_multi_func_3_result, 1, "uretprobe_multi_func_3_result");
269+
}
270+
271+
static void uprobe_multi_attach_api_subtest(void)
272+
{
273+
struct bpf_link *link1 = NULL, *link2 = NULL;
274+
struct uprobe_multi *skel = NULL;
275+
LIBBPF_OPTS(bpf_uprobe_multi_opts, opts);
276+
const char *syms[3] = {
277+
"uprobe_multi_func_1",
278+
"uprobe_multi_func_2",
279+
"uprobe_multi_func_3",
280+
};
281+
__u64 cookies[3];
282+
283+
cookies[0] = 3; /* uprobe_multi_func_1 */
284+
cookies[1] = 1; /* uprobe_multi_func_2 */
285+
cookies[2] = 2; /* uprobe_multi_func_3 */
286+
287+
opts.syms = syms;
288+
opts.cnt = ARRAY_SIZE(syms);
289+
opts.cookies = &cookies[0];
290+
291+
skel = uprobe_multi__open_and_load();
292+
if (!ASSERT_OK_PTR(skel, "uprobe_multi"))
293+
goto cleanup;
294+
295+
link1 = bpf_program__attach_uprobe_multi(skel->progs.uprobe, -1,
296+
"/proc/self/exe", NULL, &opts);
297+
if (!ASSERT_OK_PTR(link1, "bpf_program__attach_uprobe_multi"))
298+
goto cleanup;
299+
300+
cookies[0] = 2; /* uprobe_multi_func_1 */
301+
cookies[1] = 3; /* uprobe_multi_func_2 */
302+
cookies[2] = 1; /* uprobe_multi_func_3 */
303+
304+
opts.retprobe = true;
305+
link2 = bpf_program__attach_uprobe_multi(skel->progs.uretprobe, -1,
306+
"/proc/self/exe", NULL, &opts);
307+
if (!ASSERT_OK_PTR(link2, "bpf_program__attach_uprobe_multi_retprobe"))
308+
goto cleanup;
309+
310+
uprobe_multi_test_run(skel);
311+
312+
cleanup:
313+
bpf_link__destroy(link2);
314+
bpf_link__destroy(link1);
315+
uprobe_multi__destroy(skel);
316+
}
317+
242318
static void uprobe_subtest(struct test_bpf_cookie *skel)
243319
{
244320
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
@@ -515,6 +591,8 @@ void test_bpf_cookie(void)
515591
kprobe_multi_attach_api_subtest();
516592
if (test__start_subtest("uprobe"))
517593
uprobe_subtest(skel);
594+
if (test__start_subtest("multi_uprobe_attach_api"))
595+
uprobe_multi_attach_api_subtest();
518596
if (test__start_subtest("tracepoint"))
519597
tp_subtest(skel);
520598
if (test__start_subtest("perf_event"))

0 commit comments

Comments
 (0)