Skip to content

Commit 95fdf6e

Browse files
Alexei Starovoitovanakryiko
authored andcommitted
selftests/bpf: Add test for bpf_ksym_exists().
Add load and run time test for bpf_ksym_exists() and check that the verifier performs dead code elimination for non-existing kfunc. Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Martin KaFai Lau <[email protected]> Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Acked-by: John Fastabend <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 5cbd3fe commit 95fdf6e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ int err, pid;
1717
* TP_PROTO(struct task_struct *p, u64 clone_flags)
1818
*/
1919

20+
struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym __weak;
21+
void invalid_kfunc(void) __ksym __weak;
22+
void bpf_testmod_test_mod_kfunc(int i) __ksym __weak;
23+
2024
static bool is_test_kfunc_task(void)
2125
{
2226
int cur_pid = bpf_get_current_pid_tgid() >> 32;
@@ -26,7 +30,21 @@ static bool is_test_kfunc_task(void)
2630

2731
static int test_acquire_release(struct task_struct *task)
2832
{
29-
struct task_struct *acquired;
33+
struct task_struct *acquired = NULL;
34+
35+
if (!bpf_ksym_exists(bpf_task_acquire)) {
36+
err = 3;
37+
return 0;
38+
}
39+
if (!bpf_ksym_exists(bpf_testmod_test_mod_kfunc)) {
40+
err = 4;
41+
return 0;
42+
}
43+
if (bpf_ksym_exists(invalid_kfunc)) {
44+
/* the verifier's dead code elimination should remove this */
45+
err = 5;
46+
asm volatile ("goto -1"); /* for (;;); */
47+
}
3048

3149
acquired = bpf_task_acquire(task);
3250
bpf_task_release(acquired);

0 commit comments

Comments
 (0)