]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/bpf: Provide weak definitions for cross-test functions
authorRicardo B. Marlière <rbm@suse.com>
Tue, 2 Jun 2026 13:02:59 +0000 (10:02 -0300)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 5 Jun 2026 21:20:58 +0000 (14:20 -0700)
Some test files reference functions defined in other translation units that
may not be compiled when skeletons are missing. Replace forward
declarations of uprobe_multi_func_{1,2,3}() with weak no-op stubs so the
linker resolves them regardless of which objects are present.

The stub bodies are `asm volatile ("")` rather than empty, matching the
shape of the strong definitions in prog_tests/uprobe_multi_test.c. This
keeps the weak and strong sides on the same footing for the optimiser
(noinline + asm-barrier), which is the form upstream already relies on
for these functions.

Move stack_mprotect() from test_lsm.c into testing_helpers.c so it is
always available. The previous weak-stub approach returned 0, which would
cause callers expecting -1/EPERM to fail their assertions
deterministically. Having the real implementation in a shared utility
avoids this problem entirely.

Include <alloca.h> for alloca() so the build does not rely on glibc's
implicit declaration via <stdlib.h>.

Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
Link: https://lore.kernel.org/r/20260602-selftests-bpf_misconfig-v12-10-27f898b3ba26@suse.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
tools/testing/selftests/bpf/prog_tests/iters.c
tools/testing/selftests/bpf/prog_tests/test_lsm.c
tools/testing/selftests/bpf/testing_helpers.c
tools/testing/selftests/bpf/testing_helpers.h

index 35adc3f6d443fbb123cdcd80b785643c9bfeae9e..fa484d00a7a5dd037bd6db4af86d58d0fa3b8353 100644 (file)
@@ -252,10 +252,17 @@ cleanup:
        kprobe_multi__destroy(skel);
 }
 
-/* defined in prog_tests/uprobe_multi_test.c */
-void uprobe_multi_func_1(void);
-void uprobe_multi_func_2(void);
-void uprobe_multi_func_3(void);
+/*
+ * Weak uprobe target stubs. noinline is required because
+ * uprobe_multi_test_run() takes their addresses to configure the BPF
+ * program's attachment points; an inlined function has no stable
+ * address in the binary to probe. The strong definitions in
+ * uprobe_multi_test.c take precedence when that translation unit is
+ * linked.
+ */
+noinline __weak void uprobe_multi_func_1(void) { asm volatile (""); }
+noinline __weak void uprobe_multi_func_2(void) { asm volatile (""); }
+noinline __weak void uprobe_multi_func_3(void) { asm volatile (""); }
 
 static void uprobe_multi_test_run(struct uprobe_multi *skel)
 {
@@ -574,8 +581,6 @@ cleanup:
                close(fmod_ret_fd);
 }
 
-int stack_mprotect(void);
-
 static void lsm_subtest(struct test_bpf_cookie *skel)
 {
        __u64 cookie;
index a539980a2fbe5b7d0718ab37095c24481760dfde..c0b6082f345a25ada3b7f0dbc7f245c3a8129c11 100644 (file)
@@ -202,8 +202,6 @@ cleanup:
        iters_task__destroy(skel);
 }
 
-extern int stack_mprotect(void);
-
 static void subtest_css_task_iters(void)
 {
        struct iters_css_task *skel = NULL;
index bdc4fc06bc5a52c289b626c0caadd9ed9759880c..d7495efd4a5621a6198e5fcbaea30a6628f5abc8 100644 (file)
@@ -5,36 +5,14 @@
  */
 
 #include <test_progs.h>
-#include <sys/mman.h>
 #include <sys/wait.h>
 #include <unistd.h>
-#include <malloc.h>
-#include <stdlib.h>
 
 #include "lsm.skel.h"
 #include "lsm_tailcall.skel.h"
 
 char *CMD_ARGS[] = {"true", NULL};
 
-#define GET_PAGE_ADDR(ADDR, PAGE_SIZE)                                 \
-       (char *)(((unsigned long) (ADDR + PAGE_SIZE)) & ~(PAGE_SIZE-1))
-
-int stack_mprotect(void)
-{
-       void *buf;
-       long sz;
-       int ret;
-
-       sz = sysconf(_SC_PAGESIZE);
-       if (sz < 0)
-               return sz;
-
-       buf = alloca(sz * 3);
-       ret = mprotect(GET_PAGE_ADDR(buf, sz), sz,
-                      PROT_READ | PROT_WRITE | PROT_EXEC);
-       return ret;
-}
-
 int exec_cmd(int *monitored_pid)
 {
        int child_pid, child_status;
index 6fbe1e995660174137cf96a01be761b95687e56a..c970e7793dfcbcbb8ae23258a67b0645a2b8324f 100644 (file)
@@ -5,6 +5,8 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/mman.h>
+#include <alloca.h>
 #include <bpf/bpf.h>
 #include <bpf/libbpf.h>
 #include "disasm.h"
@@ -516,3 +518,19 @@ bool is_jit_enabled(void)
 
        return enabled;
 }
+
+int stack_mprotect(void)
+{
+       void *buf;
+       long sz;
+       int ret;
+
+       sz = sysconf(_SC_PAGESIZE);
+       if (sz < 0)
+               return sz;
+
+       buf = alloca(sz * 3);
+       ret = mprotect((void *)(((unsigned long)(buf + sz)) & ~(sz - 1)), sz,
+                      PROT_READ | PROT_WRITE | PROT_EXEC);
+       return ret;
+}
index 2ca2356a0b58efa5d726352f61d4714d518108fb..2edc6fb7fc52126b8232fa0ccfa95da282e6048a 100644 (file)
@@ -59,5 +59,6 @@ struct bpf_insn;
 int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
 int testing_prog_flags(void);
 bool is_jit_enabled(void);
+int stack_mprotect(void);
 
 #endif /* __TESTING_HELPERS_H */