selftests/bpf: Provide weak definitions for cross-test functions

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>
This commit is contained in:
Ricardo B. Marlière
2026-06-02 10:02:59 -03:00
committed by Alexei Starovoitov
parent f813a4d687
commit b85e63cb65
5 changed files with 30 additions and 30 deletions

View File

@@ -252,10 +252,17 @@ static void kprobe_multi_attach_api_subtest(void)
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 @@ static void tracing_subtest(struct test_bpf_cookie *skel)
close(fmod_ret_fd);
}
int stack_mprotect(void);
static void lsm_subtest(struct test_bpf_cookie *skel)
{
__u64 cookie;

View File

@@ -202,8 +202,6 @@ static void subtest_task_iters(void)
iters_task__destroy(skel);
}
extern int stack_mprotect(void);
static void subtest_css_task_iters(void)
{
struct iters_css_task *skel = NULL;

View 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;

View 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;
}

View 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 */