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

Configure Feed

Select the types of activity you want to include in your feed.

selftests/bpf: Add test for bpf_override_return helper

We do not actually test the bpf_override_return helper functionality
itself at the moment, only the bpf program being able to attach it.

Adding test that override prctl syscall return value on top of
kprobe and kprobe.multi.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Song Liu <song@kernel.org>
Link: https://lore.kernel.org/bpf/20260112121157.854473-2-jolsa@kernel.org

authored by

Jiri Olsa and committed by
Andrii Nakryiko
934d9746 276f3b6d

+71
+44
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 + #include <errno.h> 3 + #include <sys/prctl.h> 2 4 #include <test_progs.h> 3 5 #include "kprobe_multi.skel.h" 4 6 #include "trace_helpers.h" ··· 542 540 kprobe_multi_override__destroy(skel); 543 541 } 544 542 543 + static void test_override(void) 544 + { 545 + struct kprobe_multi_override *skel = NULL; 546 + int err; 547 + 548 + skel = kprobe_multi_override__open_and_load(); 549 + if (!ASSERT_OK_PTR(skel, "kprobe_multi_empty__open_and_load")) 550 + goto cleanup; 551 + 552 + skel->bss->pid = getpid(); 553 + 554 + /* no override */ 555 + err = prctl(0xffff, 0); 556 + ASSERT_EQ(err, -1, "err"); 557 + 558 + /* kprobe.multi override */ 559 + skel->links.test_override = bpf_program__attach_kprobe_multi_opts(skel->progs.test_override, 560 + SYS_PREFIX "sys_prctl", NULL); 561 + if (!ASSERT_OK_PTR(skel->links.test_override, "bpf_program__attach_kprobe_multi_opts")) 562 + goto cleanup; 563 + 564 + err = prctl(0xffff, 0); 565 + ASSERT_EQ(err, 123, "err"); 566 + 567 + bpf_link__destroy(skel->links.test_override); 568 + skel->links.test_override = NULL; 569 + 570 + /* kprobe override */ 571 + skel->links.test_kprobe_override = bpf_program__attach_kprobe(skel->progs.test_kprobe_override, 572 + false, SYS_PREFIX "sys_prctl"); 573 + if (!ASSERT_OK_PTR(skel->links.test_kprobe_override, "bpf_program__attach_kprobe")) 574 + goto cleanup; 575 + 576 + err = prctl(0xffff, 0); 577 + ASSERT_EQ(err, 123, "err"); 578 + 579 + cleanup: 580 + kprobe_multi_override__destroy(skel); 581 + } 582 + 545 583 #ifdef __x86_64__ 546 584 static void test_attach_write_ctx(void) 547 585 { ··· 639 597 test_attach_api_fails(); 640 598 if (test__start_subtest("attach_override")) 641 599 test_attach_override(); 600 + if (test__start_subtest("override")) 601 + test_override(); 642 602 if (test__start_subtest("session")) 643 603 test_session_skel_api(); 644 604 if (test__start_subtest("session_cookie"))
+15
tools/testing/selftests/bpf/progs/kprobe_multi_override.c
··· 5 5 6 6 char _license[] SEC("license") = "GPL"; 7 7 8 + int pid = 0; 9 + 8 10 SEC("kprobe.multi") 9 11 int test_override(struct pt_regs *ctx) 10 12 { 13 + if (bpf_get_current_pid_tgid() >> 32 != pid) 14 + return 0; 15 + 16 + bpf_override_return(ctx, 123); 17 + return 0; 18 + } 19 + 20 + SEC("kprobe") 21 + int test_kprobe_override(struct pt_regs *ctx) 22 + { 23 + if (bpf_get_current_pid_tgid() >> 32 != pid) 24 + return 0; 25 + 11 26 bpf_override_return(ctx, 123); 12 27 return 0; 13 28 }
+12
tools/testing/selftests/bpf/trace_helpers.h
··· 4 4 5 5 #include <bpf/libbpf.h> 6 6 7 + #ifdef __x86_64__ 8 + #define SYS_PREFIX "__x64_" 9 + #elif defined(__s390x__) 10 + #define SYS_PREFIX "__s390x_" 11 + #elif defined(__aarch64__) 12 + #define SYS_PREFIX "__arm64_" 13 + #elif defined(__riscv) 14 + #define SYS_PREFIX "__riscv_" 15 + #else 16 + #define SYS_PREFIX "" 17 + #endif 18 + 7 19 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) 8 20 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1) 9 21