Skip to content

Commit 6a4ea0d

Browse files
olsajiriAlexei Starovoitov
authored andcommitted
selftests/bpf: Add uprobe context ip register change test
Adding test to check we can change the application execution through instruction pointer change through uprobe program. It's x86_64 specific test. Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Jiri Olsa <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 7f8a05c commit 6a4ea0d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,52 @@ static void regs_common(void)
190190
test_uprobe__destroy(skel);
191191
}
192192

193+
static noinline unsigned long uprobe_regs_change_ip_1(void)
194+
{
195+
return 0xc0ffee;
196+
}
197+
198+
static noinline unsigned long uprobe_regs_change_ip_2(void)
199+
{
200+
return 0xdeadbeef;
201+
}
202+
203+
static void regs_ip(void)
204+
{
205+
LIBBPF_OPTS(bpf_uprobe_opts, uprobe_opts);
206+
struct test_uprobe *skel;
207+
unsigned long ret;
208+
209+
skel = test_uprobe__open_and_load();
210+
if (!ASSERT_OK_PTR(skel, "skel_open"))
211+
return;
212+
213+
skel->bss->my_pid = getpid();
214+
skel->bss->ip = (unsigned long) uprobe_regs_change_ip_2;
215+
216+
uprobe_opts.func_name = "uprobe_regs_change_ip_1";
217+
skel->links.test_regs_change_ip = bpf_program__attach_uprobe_opts(
218+
skel->progs.test_regs_change_ip,
219+
-1,
220+
"/proc/self/exe",
221+
0 /* offset */,
222+
&uprobe_opts);
223+
if (!ASSERT_OK_PTR(skel->links.test_regs_change_ip, "bpf_program__attach_uprobe_opts"))
224+
goto cleanup;
225+
226+
ret = uprobe_regs_change_ip_1();
227+
ASSERT_EQ(ret, 0xdeadbeef, "ret");
228+
229+
cleanup:
230+
test_uprobe__destroy(skel);
231+
}
232+
193233
static void test_uprobe_regs_change(void)
194234
{
195235
if (test__start_subtest("regs_change_common"))
196236
regs_common();
237+
if (test__start_subtest("regs_change_ip"))
238+
regs_ip();
197239
}
198240
#else
199241
static void test_uprobe_regs_change(void) { }

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ int BPF_UPROBE(test_regs_change)
8282
ctx->si = regs.si;
8383
return 0;
8484
}
85+
86+
unsigned long ip;
87+
88+
SEC("uprobe")
89+
int BPF_UPROBE(test_regs_change_ip)
90+
{
91+
pid_t pid = bpf_get_current_pid_tgid() >> 32;
92+
93+
if (pid != my_pid)
94+
return 0;
95+
96+
ctx->ip = ip;
97+
return 0;
98+
}
8599
#endif

0 commit comments

Comments
 (0)