Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add kprobe_multi override test

Adding test that tries to attach program with bpf_override_return
helper to function not within error injection list.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230907200652.926951-2-jolsa@kernel.org

authored by

Jiri Olsa and committed by
Andrii Nakryiko
7182e564 41bc46c1

+50
+37
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
··· 3 3 #include "kprobe_multi.skel.h" 4 4 #include "trace_helpers.h" 5 5 #include "kprobe_multi_empty.skel.h" 6 + #include "kprobe_multi_override.skel.h" 6 7 #include "bpf/libbpf_internal.h" 7 8 #include "bpf/hashmap.h" 8 9 ··· 454 453 } 455 454 } 456 455 456 + void test_attach_override(void) 457 + { 458 + struct kprobe_multi_override *skel = NULL; 459 + struct bpf_link *link = NULL; 460 + 461 + skel = kprobe_multi_override__open_and_load(); 462 + if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load")) 463 + goto cleanup; 464 + 465 + /* The test_override calls bpf_override_return so it should fail 466 + * to attach to bpf_fentry_test1 function, which is not on error 467 + * injection list. 468 + */ 469 + link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override, 470 + "bpf_fentry_test1", NULL); 471 + if (!ASSERT_ERR_PTR(link, "override_attached_bpf_fentry_test1")) { 472 + bpf_link__destroy(link); 473 + goto cleanup; 474 + } 475 + 476 + /* The should_fail_bio function is on error injection list, 477 + * attach should succeed. 478 + */ 479 + link = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override, 480 + "should_fail_bio", NULL); 481 + if (!ASSERT_OK_PTR(link, "override_attached_should_fail_bio")) 482 + goto cleanup; 483 + 484 + bpf_link__destroy(link); 485 + 486 + cleanup: 487 + kprobe_multi_override__destroy(skel); 488 + } 489 + 457 490 void serial_test_kprobe_multi_bench_attach(void) 458 491 { 459 492 if (test__start_subtest("kernel")) ··· 515 480 test_attach_api_syms(); 516 481 if (test__start_subtest("attach_api_fails")) 517 482 test_attach_api_fails(); 483 + if (test__start_subtest("attach_override")) 484 + test_attach_override(); 518 485 }
+13
tools/testing/selftests/bpf/progs/kprobe_multi_override.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + #include <linux/bpf.h> 3 + #include <bpf/bpf_helpers.h> 4 + #include <bpf/bpf_tracing.h> 5 + 6 + char _license[] SEC("license") = "GPL"; 7 + 8 + SEC("kprobe.multi") 9 + int test_override(struct pt_regs *ctx) 10 + { 11 + bpf_override_return(ctx, 123); 12 + return 0; 13 + }