1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2023 Hengqi Chen */
4 #include <test_progs.h>
5 #include "test_uprobe.skel.h"
7 static FILE *urand_spawn(int *pid)
11 /* urandom_read's stdout is wired into f */
12 f = popen("./urandom_read 1 report-pid", "r");
16 if (fscanf(f, "%d", pid) != 1) {
25 static int urand_trigger(FILE **urand_pipe)
29 /* pclose() waits for child process to exit and returns their exit code */
30 exit_code = pclose(*urand_pipe);
36 void test_uprobe(void)
38 LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
39 struct test_uprobe *skel;
40 FILE *urand_pipe = NULL;
41 int urand_pid = 0, err;
43 skel = test_uprobe__open_and_load();
44 if (!ASSERT_OK_PTR(skel, "skel_open"))
47 urand_pipe = urand_spawn(&urand_pid);
48 if (!ASSERT_OK_PTR(urand_pipe, "urand_spawn"))
51 skel->bss->my_pid = urand_pid;
53 /* Manual attach uprobe to urandlib_api
54 * There are two `urandlib_api` symbols in .dynsym section:
55 * - urandlib_api@LIBURANDOM_READ_1.0.0
56 * - urandlib_api@@LIBURANDOM_READ_2.0.0
57 * Both are global bind and would cause a conflict if user
58 * specify the symbol name without a version suffix
60 uprobe_opts.func_name = "urandlib_api";
61 skel->links.test4 = bpf_program__attach_uprobe_opts(skel->progs.test4,
63 "./liburandom_read.so",
66 if (!ASSERT_ERR_PTR(skel->links.test4, "urandlib_api_attach_conflict"))
69 uprobe_opts.func_name = "urandlib_api@LIBURANDOM_READ_1.0.0";
70 skel->links.test4 = bpf_program__attach_uprobe_opts(skel->progs.test4,
72 "./liburandom_read.so",
75 if (!ASSERT_OK_PTR(skel->links.test4, "urandlib_api_attach_ok"))
78 /* Auto attach 3 u[ret]probes to urandlib_api_sameoffset */
79 err = test_uprobe__attach(skel);
80 if (!ASSERT_OK(err, "skel_attach"))
83 /* trigger urandom_read */
84 ASSERT_OK(urand_trigger(&urand_pipe), "urand_exit_code");
86 ASSERT_EQ(skel->bss->test1_result, 1, "urandlib_api_sameoffset");
87 ASSERT_EQ(skel->bss->test2_result, 1, "urandlib_api_sameoffset@v1");
88 ASSERT_EQ(skel->bss->test3_result, 3, "urandlib_api_sameoffset@@v2");
89 ASSERT_EQ(skel->bss->test4_result, 1, "urandlib_api");
94 test_uprobe__destroy(skel);