From: Feng Yang <[email protected]>

Extend the test with a sleepable raw tracepoint on sys_enter.  A helper
process pages out the build ID page, issues a getpid() syscall and then
stops itself, so that the recorded stack cannot be overwritten.  The BPF
program filters on the target tgid and syscall id, captures the user
stack with BPF_F_USER_BUILD_ID, and the test asserts that one of the
frames carries uprobe_multi's build ID.  A non-sleepable raw tracepoint
is used as a control: the same frame must be reported as IP, which also
proves that the page was really evicted.

Signed-off-by: Feng Yang <[email protected]>
---
 .../selftests/bpf/prog_tests/build_id.c       | 138 ++++++++++++++++++
 .../selftests/bpf/progs/test_build_id.c       |  38 +++++
 tools/testing/selftests/bpf/uprobe_multi.c    |  37 ++++-
 3 files changed, 210 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/build_id.c 
b/tools/testing/selftests/bpf/prog_tests/build_id.c
index aec9c8d6bc96..885dc3da5900 100644
--- a/tools/testing/selftests/bpf/prog_tests/build_id.c
+++ b/tools/testing/selftests/bpf/prog_tests/build_id.c
@@ -1,5 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+#include <signal.h>
+#include <sys/syscall.h>
 #include <test_progs.h>
 
 #include "test_build_id.skel.h"
@@ -104,6 +106,138 @@ static void subtest_sleepable(void)
        test_build_id__destroy(skel);
 }
 
+static bool stack_has_build_id(const struct bpf_stack_build_id *stack, int 
frame_cnt,
+                              const char *id, int id_sz)
+{
+       int i;
+
+       for (i = 0; i < frame_cnt; i++)
+               if (stack[i].status == BPF_STACK_BUILD_ID_VALID &&
+                   !memcmp(stack[i].build_id, id, id_sz))
+                       return true;
+
+       return false;
+}
+
+/*
+ * Run uprobe_multi, let it page out the build ID page and stop itself on
+ * SIGSTOP, so that the recorded stack cannot be overwritten by a later
+ * syscall while the caller inspects it.  target_tgid is set before the
+ * child is released, so only its syscalls are recorded.
+ */
+static int run_uprobe_multi_stopped(const char *mode, struct test_build_id 
*skel,
+                                   pid_t *pid_out)
+{
+       char c = 'x';
+       int pfd[2], status, err;
+       pid_t pid;
+
+       *pid_out = -1;
+
+       if (pipe(pfd))
+               return -errno;
+
+       pid = fork();
+       if (pid < 0) {
+               err = -errno;
+               close(pfd[0]);
+               close(pfd[1]);
+               return err;
+       }
+       if (pid == 0) {
+               close(pfd[1]);
+               if (read(pfd[0], &c, 1) != 1)
+                       _exit(127);
+               close(pfd[0]);
+               execl("./uprobe_multi", "uprobe_multi", mode, NULL);
+               _exit(127);
+       }
+
+       close(pfd[0]);
+       skel->bss->target_tgid = pid;
+       skel->bss->target_syscall = SYS_getpid;
+       if (write(pfd[1], &c, 1) != 1) {
+               err = -errno;
+               close(pfd[1]);
+               kill(pid, SIGKILL);
+               waitpid(pid, NULL, 0);
+               return err;
+       }
+       close(pfd[1]);
+
+       if (waitpid(pid, &status, WUNTRACED) < 0) {
+               err = -errno;
+               kill(pid, SIGKILL);
+               waitpid(pid, NULL, 0);
+               return err;
+       }
+
+       if (WIFSTOPPED(status)) {
+               *pid_out = pid;
+               return 0;
+       }
+       if (WIFEXITED(status))
+               return WEXITSTATUS(status);
+       return -1;
+}
+
+static void subtest_raw_tp(bool sleepable)
+{
+       struct test_build_id *skel;
+       struct bpf_stack_build_id *stack;
+       int frame_cnt, err, res;
+       pid_t pid = -1;
+
+       skel = test_build_id__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "skel_open"))
+               return;
+
+       if (sleepable) {
+               skel->links.rawtp_sleepable = 
bpf_program__attach(skel->progs.rawtp_sleepable);
+               if (!ASSERT_OK_PTR(skel->links.rawtp_sleepable, 
"attach_rawtp_sleepable"))
+                       goto cleanup;
+       } else {
+               skel->links.rawtp_nofault = 
bpf_program__attach(skel->progs.rawtp_nofault);
+               if (!ASSERT_OK_PTR(skel->links.rawtp_nofault, 
"attach_rawtp_nofault"))
+                       goto cleanup;
+       }
+
+       err = run_uprobe_multi_stopped("uprobe-paged-out-stop", skel, &pid);
+       if (err == 2) {
+               /* could not evict the build ID page, nothing to test */
+               test__skip();
+               goto cleanup;
+       }
+       if (!ASSERT_OK(err, "trigger_uprobe_multi"))
+               goto cleanup;
+
+       if (sleepable) {
+               res = skel->bss->res_rawtp_sleepable;
+               stack = skel->bss->stack_rawtp_sleepable;
+       } else {
+               res = skel->bss->res_rawtp_nofault;
+               stack = skel->bss->stack_rawtp_nofault;
+       }
+       frame_cnt = res / sizeof(struct bpf_stack_build_id);
+       if (env.verbosity >= VERBOSE_NORMAL)
+               print_stack(stack, frame_cnt);
+
+       ASSERT_GT(frame_cnt, 0, "frames");
+       if (sleepable)
+               ASSERT_TRUE(stack_has_build_id(stack, frame_cnt, build_id, 
build_id_sz),
+                           "sleepable_build_id");
+       else
+               ASSERT_FALSE(stack_has_build_id(stack, frame_cnt, build_id, 
build_id_sz),
+                            "nofault_build_id");
+
+cleanup:
+       if (pid > 0) {
+               kill(pid, SIGKILL);
+               waitpid(pid, NULL, 0);
+       }
+       test_build_id__destroy(skel);
+}
+
 void serial_test_build_id(void)
 {
        build_id_sz = read_build_id("uprobe_multi", build_id, sizeof(build_id));
@@ -115,4 +249,8 @@ void serial_test_build_id(void)
                subtest_nofault(true /* resident */);
        if (test__start_subtest("sleepable"))
                subtest_sleepable();
+       if (test__start_subtest("raw-tp-nofault-paged-out"))
+               subtest_raw_tp(false /* non-sleepable */);
+       if (test__start_subtest("raw-tp-sleepable-paged-out"))
+               subtest_raw_tp(true /* sleepable */);
 }
diff --git a/tools/testing/selftests/bpf/progs/test_build_id.c 
b/tools/testing/selftests/bpf/progs/test_build_id.c
index 32ce59f9aa27..a6d6712888dc 100644
--- a/tools/testing/selftests/bpf/progs/test_build_id.c
+++ b/tools/testing/selftests/bpf/progs/test_build_id.c
@@ -3,6 +3,7 @@
 
 #include "vmlinux.h"
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 
 struct bpf_stack_build_id stack_sleepable[128];
 int res_sleepable;
@@ -28,4 +29,41 @@ int uprobe_sleepable(struct pt_regs *ctx)
        return 0;
 }
 
+struct bpf_stack_build_id stack_rawtp_sleepable[128];
+int res_rawtp_sleepable;
+
+struct bpf_stack_build_id stack_rawtp_nofault[128];
+int res_rawtp_nofault;
+
+int target_tgid;
+int target_syscall;
+
+SEC("raw_tp/sys_enter")
+int BPF_PROG(rawtp_nofault, struct pt_regs *regs, long id)
+{
+       if ((u32)(bpf_get_current_pid_tgid() >> 32) != (u32)target_tgid ||
+           id != target_syscall)
+               return 0;
+
+       res_rawtp_nofault = bpf_get_stack(ctx, stack_rawtp_nofault,
+                                         sizeof(stack_rawtp_nofault),
+                                         BPF_F_USER_STACK | 
BPF_F_USER_BUILD_ID);
+
+       return 0;
+}
+
+SEC("raw_tp.s/sys_enter")
+int BPF_PROG(rawtp_sleepable, struct pt_regs *regs, long id)
+{
+       if ((u32)(bpf_get_current_pid_tgid() >> 32) != (u32)target_tgid ||
+           id != target_syscall)
+               return 0;
+
+       res_rawtp_sleepable = bpf_get_stack(ctx, stack_rawtp_sleepable,
+                                           sizeof(stack_rawtp_sleepable),
+                                           BPF_F_USER_STACK | 
BPF_F_USER_BUILD_ID);
+
+       return 0;
+}
+
 char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/uprobe_multi.c 
b/tools/testing/selftests/bpf/uprobe_multi.c
index 0af330b6c364..ef44d86962c1 100644
--- a/tools/testing/selftests/bpf/uprobe_multi.c
+++ b/tools/testing/selftests/bpf/uprobe_multi.c
@@ -4,7 +4,9 @@
 #include <string.h>
 #include <stdbool.h>
 #include <stdint.h>
+#include <signal.h>
 #include <sys/mman.h>
+#include <sys/syscall.h>
 #include <unistd.h>
 #include <sdt.h>
 
@@ -95,12 +97,12 @@ static int usdt(void)
 extern char build_id_start[];
 extern char build_id_end[];
 
-int __attribute__((weak)) trigger_uprobe(bool build_id_resident)
+/* Returns 2 if the build ID page could not be evicted. */
+static int prep_build_id_page(bool build_id_resident)
 {
        int page_sz = sysconf(_SC_PAGESIZE);
-       void *addr;
-
        unsigned char vec[1];
+       void *addr;
        int poll = 0;
 
        /* page-align build ID start */
@@ -123,12 +125,39 @@ int __attribute__((weak)) trigger_uprobe(bool 
build_id_resident)
                        poll++;
                        usleep(100);
                } while (poll < 500);
+               if (vec[0] & 1)
+                       return 2;
        }
+       return 0;
+}
+
+int __attribute__((weak)) trigger_uprobe(bool build_id_resident)
+{
+       (void)prep_build_id_page(build_id_resident);
+
        (void)uprobe();
 
        return 0;
 }
 
+int __attribute__((weak)) trigger_stop(void)
+{
+       int err = prep_build_id_page(false /* page-out build ID */);
+
+       if (err)
+               return err;
+
+       /*
+        * Make one syscall that the BPF program under test filters on, then
+        * stop this process so that the recorded stack cannot be overwritten
+        * by another syscall while the caller inspects it.
+        */
+       (void)syscall(SYS_getpid);
+       raise(SIGSTOP);
+
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        if (argc != 2)
@@ -142,6 +171,8 @@ int main(int argc, char **argv)
                return trigger_uprobe(false /* page-out build ID */);
        if (!strcmp("uprobe-paged-in", argv[1]))
                return trigger_uprobe(true /* page-in build ID */);
+       if (!strcmp("uprobe-paged-out-stop", argv[1]))
+               return trigger_stop();
 
 error:
        fprintf(stderr,
-- 
2.25.1


Reply via email to