[PATCH bpf-next 3/3] selftests/bpf: Test weak typed ksyms without vmlinux BTF
From: Geyslan Gregório via B4 Relay
Date: Sat Dec 05 2026 - 16:28:24 EST
From: Geyslan Gregório <gg@xxxxxxxxxxx>
Add a subtest that loads an object whose only users of vmlinux BTF are
weak typed ksyms, a variable and two kfuncs, when libbpf finds no
vmlinux BTF. The object is opened with btf_custom_path, which serves its
CO-RE relocation. It has to load, and a test run of its program has to
see the weak typed ksyms as absent and read the same TGID through CO-RE
as bpf_get_current_pid_tgid() returns.
The object also has a typeless ksym, so libbpf reads /proc/kallsyms,
which lists the typed variable. The variable has to stay absent.
A second object adds a strong typed ksym, which needs kernel BTF, so
its load has to fail with -ESRCH.
The test kernel has BTF, so the subtest runs in a child process that
hides /sys/kernel/btf in a mount namespace of its own, and passes a copy
of vmlinux BTF as btf_custom_path. libbpf also reads kernel BTF from a
vmlinux image in /boot or /lib/modules. Where it still finds one with
sysfs hidden, the subtest is skipped.
Signed-off-by: Geyslan Gregório <gg@xxxxxxxxxxx>
---
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c | 138 +++++++++++++++++++++
.../selftests/bpf/progs/test_ksyms_mixed_no_btf.c | 25 ++++
.../selftests/bpf/progs/test_ksyms_weak_no_btf.c | 50 ++++++++
3 files changed, 213 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
index 1bc3089f10fc..d64bb2d7a322 100644
--- a/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
@@ -1,6 +1,10 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Google */
+#define _GNU_SOURCE
+#include <sched.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
#include <test_progs.h>
#include <bpf/libbpf.h>
#include <bpf/btf.h>
@@ -8,6 +12,8 @@
#include "test_ksyms_btf_null_check.skel.h"
#include "test_ksyms_weak.skel.h"
#include "test_ksyms_weak.lskel.h"
+#include "test_ksyms_weak_no_btf.skel.h"
+#include "test_ksyms_mixed_no_btf.skel.h"
#include "test_ksyms_btf_write_check.skel.h"
#include "test_ksyms_strong_typeless.lskel.h"
@@ -139,6 +145,135 @@ static void test_weak_syms_lskel(void)
test_ksyms_weak_lskel__destroy(skel);
}
+enum {
+ NO_BTF_OK,
+ NO_BTF_SETUP,
+ NO_BTF_SKIP,
+ NO_BTF_OPEN,
+ NO_BTF_LOAD,
+ NO_BTF_RUN,
+ NO_BTF_TYPED_VAR,
+ NO_BTF_TYPELESS,
+ NO_BTF_KFUNC,
+ NO_BTF_CORE,
+ NO_BTF_MIXED,
+};
+
+/* Runs in a child, in a mount namespace of its own: with /sys/kernel/btf
+ * hidden, libbpf finds no kernel BTF, as on a kernel built without it.
+ */
+static int weak_syms_no_vmlinux_btf(const char *btf_file)
+{
+ LIBBPF_OPTS(bpf_object_open_opts, opts, .btf_custom_path = btf_file);
+ LIBBPF_OPTS(bpf_test_run_opts, topts);
+ struct test_ksyms_mixed_no_btf *mixed;
+ struct test_ksyms_weak_no_btf *skel;
+ struct btf *btf;
+ int ret;
+
+ if (unshare(CLONE_NEWNS) ||
+ mount("", "/", "", MS_REC | MS_PRIVATE, NULL) ||
+ mount("none", "/sys/kernel/btf", "tmpfs", 0, NULL))
+ return NO_BTF_SETUP;
+
+ /* libbpf also takes kernel BTF from a vmlinux image, if it finds one */
+ btf = btf__load_vmlinux_btf();
+ if (!libbpf_get_error(btf)) {
+ btf__free(btf);
+ return NO_BTF_SKIP;
+ }
+
+ /* a strong typed ksym still needs kernel BTF */
+ mixed = test_ksyms_mixed_no_btf__open_opts(&opts);
+ if (!mixed)
+ return NO_BTF_OPEN;
+ ret = test_ksyms_mixed_no_btf__load(mixed);
+ test_ksyms_mixed_no_btf__destroy(mixed);
+ if (ret != -ESRCH)
+ return NO_BTF_MIXED;
+
+ skel = test_ksyms_weak_no_btf__open_opts(&opts);
+ if (!skel)
+ return NO_BTF_OPEN;
+
+ ret = NO_BTF_LOAD;
+ if (test_ksyms_weak_no_btf__load(skel))
+ goto cleanup;
+
+ /* Run the program in this task. The CO-RE read and the helper both
+ * see the TGID of the initial PID namespace, whatever namespace the
+ * test runs in.
+ */
+ ret = NO_BTF_RUN;
+ if (bpf_prog_test_run_opts(bpf_program__fd(skel->progs.handler), &topts))
+ goto cleanup;
+
+ if (skel->data->out__typed_var != 0)
+ ret = NO_BTF_TYPED_VAR;
+ else if (skel->data->out__typeless == -1)
+ ret = NO_BTF_TYPELESS;
+ else if (skel->data->out__kfunc != 0)
+ ret = NO_BTF_KFUNC;
+ else if (skel->data->out__core_tgid != skel->data->out__tgid)
+ ret = NO_BTF_CORE;
+ else
+ ret = NO_BTF_OK;
+
+cleanup:
+ test_ksyms_weak_no_btf__destroy(skel);
+ return ret;
+}
+
+static void test_weak_syms_no_vmlinux_btf(void)
+{
+ char btf_file[] = "/tmp/ksyms_btf_no_vmlinux.btf.XXXXXX";
+ int fd = -1, status = 0;
+ const void *raw_data;
+ struct btf *btf;
+ __u32 raw_sz;
+ pid_t pid;
+
+ btf = btf__load_vmlinux_btf();
+ if (!ASSERT_OK_PTR(btf, "vmlinux_btf"))
+ return;
+
+ fd = mkstemp(btf_file);
+ if (!ASSERT_OK_FD(fd, "btf_tmp"))
+ goto cleanup;
+
+ raw_data = btf__raw_data(btf, &raw_sz);
+ if (!ASSERT_OK_PTR(raw_data, "raw_data"))
+ goto cleanup;
+ if (!ASSERT_EQ(write(fd, raw_data, raw_sz), raw_sz, "btf_write"))
+ goto cleanup;
+
+ pid = fork();
+ if (!ASSERT_GE(pid, 0, "fork"))
+ goto cleanup;
+ if (pid == 0)
+ _exit(weak_syms_no_vmlinux_btf(btf_file));
+
+ if (!ASSERT_EQ(waitpid(pid, &status, 0), pid, "waitpid"))
+ goto cleanup;
+ if (!ASSERT_TRUE(WIFEXITED(status), "child_exited"))
+ goto cleanup;
+
+ if (WEXITSTATUS(status) == NO_BTF_SKIP) {
+ printf("%s:SKIP:kernel BTF is found outside of /sys/kernel/btf\n",
+ __func__);
+ test__skip();
+ goto cleanup;
+ }
+ ASSERT_EQ(WEXITSTATUS(status), NO_BTF_OK, "weak ksyms without vmlinux BTF");
+
+cleanup:
+ if (fd >= 0) {
+ close(fd);
+ remove(btf_file);
+ }
+ btf__free(btf);
+}
+
static void test_strong_typeless_lskel(void)
{
struct test_ksyms_strong_typeless_lskel *skel;
@@ -199,6 +334,9 @@ void test_ksyms_btf(void)
if (test__start_subtest("weak_ksyms_lskel"))
test_weak_syms_lskel();
+ if (test__start_subtest("weak_ksyms_no_vmlinux_btf"))
+ test_weak_syms_no_vmlinux_btf();
+
if (test__start_subtest("strong_typeless_lskel"))
test_strong_typeless_lskel();
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_mixed_no_btf.c b/tools/testing/selftests/bpf/progs/test_ksyms_mixed_no_btf.c
new file mode 100644
index 000000000000..e9b597a0576e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_mixed_no_btf.c
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A weak and a strong typed ksym. The strong one needs kernel BTF, so the
+ * object must not load on a kernel that libbpf finds no BTF for.
+ */
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+
+int out__typed_vars = -1;
+
+extern const struct rq runqueues __ksym __weak;
+extern const int bpf_prog_active __ksym;
+
+SEC("raw_tp")
+int handler(const void *ctx)
+{
+ out__typed_vars = bpf_ksym_exists(&runqueues) &&
+ bpf_per_cpu_ptr(&bpf_prog_active, 0);
+
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/test_ksyms_weak_no_btf.c b/tools/testing/selftests/bpf/progs/test_ksyms_weak_no_btf.c
new file mode 100644
index 000000000000..709a92854a29
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_ksyms_weak_no_btf.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test weak typed ksyms on a kernel that libbpf finds no BTF for.
+ *
+ * The CO-RE relocation is resolved against btf_custom_path, so the weak
+ * typed ksyms are the only users of kernel BTF. The typeless ksym makes
+ * libbpf read /proc/kallsyms, which lists runqueues too.
+ */
+
+#include "vmlinux.h"
+
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+
+int out__typed_var = -1;
+__u64 out__typeless = -1;
+int out__kfunc = -1;
+int out__core_tgid = -1;
+int out__tgid = -1;
+
+extern const struct rq runqueues __ksym __weak; /* typed */
+extern const void bpf_prog_active __ksym __weak; /* typeless */
+void bpf_rcu_read_lock(void) __ksym __weak;
+void bpf_rcu_read_unlock(void) __ksym __weak;
+
+SEC("raw_tp")
+int handler(const void *ctx)
+{
+ struct task_struct *task = (struct task_struct *)bpf_get_current_task();
+
+ out__typed_var = bpf_ksym_exists(&runqueues) ? 1 : 0;
+ out__typeless = (__u64)&bpf_prog_active;
+
+ if (bpf_ksym_exists(bpf_rcu_read_lock) &&
+ bpf_ksym_exists(bpf_rcu_read_unlock)) {
+ /* dead code when the kfuncs are left unresolved */
+ bpf_rcu_read_lock();
+ bpf_rcu_read_unlock();
+ out__kfunc = 1;
+ } else {
+ out__kfunc = 0;
+ }
+
+ out__core_tgid = BPF_CORE_READ(task, tgid);
+ out__tgid = bpf_get_current_pid_tgid() >> 32;
+
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.56.1