This piggybacks on the existing "ksyms" test because this test also relies on a __ksym symbol and requires CONFIG_KALLSYMS. Signed-off-by: Florent Revest <revest@xxxxxxxxxx> --- tools/testing/selftests/bpf/config | 1 + .../testing/selftests/bpf/prog_tests/ksyms.c | 46 ++++++++++++++++++- .../bpf/progs/test_kallsyms_lookup.c | 38 +++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_kallsyms_lookup.c diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config index 365bf9771b07..791a46e5d013 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -43,3 +43,4 @@ CONFIG_IMA=y CONFIG_SECURITYFS=y CONFIG_IMA_WRITE_POLICY=y CONFIG_IMA_READ_POLICY=y +CONFIG_KALLSYMS=y diff --git a/tools/testing/selftests/bpf/prog_tests/ksyms.c b/tools/testing/selftests/bpf/prog_tests/ksyms.c index b295969b263b..0478b67a92ae 100644 --- a/tools/testing/selftests/bpf/prog_tests/ksyms.c +++ b/tools/testing/selftests/bpf/prog_tests/ksyms.c @@ -3,11 +3,12 @@ #include <test_progs.h> #include "test_ksyms.skel.h" +#include "test_kallsyms_lookup.skel.h" #include <sys/stat.h> static int duration; -void test_ksyms(void) +void test_ksyms_variables(void) { const char *btf_path = "/sys/kernel/btf/vmlinux"; struct test_ksyms *skel; @@ -59,3 +60,46 @@ void test_ksyms(void) cleanup: test_ksyms__destroy(skel); } + +void test_kallsyms_lookup(void) +{ + struct test_kallsyms_lookup *skel; + int err; + + skel = test_kallsyms_lookup__open_and_load(); + if (CHECK(!skel, "skel_open", "failed to open and load skeleton\n")) + return; + + err = test_kallsyms_lookup__attach(skel); + if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) + goto cleanup; + + /* trigger tracepoint */ + usleep(1); + + CHECK(strcmp(skel->bss->name, "schedule"), "name", + "got \"%s\", exp \"schedule\"\n", skel->bss->name); + CHECK(strcmp(skel->bss->name_truncated, "sched"), "name_truncated", + "got \"%s\", exp \"sched\"\n", skel->bss->name_truncated); + CHECK(strcmp(skel->bss->name_invalid, ""), "name_invalid", + "got \"%s\", exp \"\"\n", skel->bss->name_invalid); + CHECK(strcmp(skel->bss->module_name, ""), "module_name", + "got \"%s\", exp \"\"\n", skel->bss->module_name); + CHECK(skel->bss->schedule_ret != 9, "schedule_ret", + "got %d, exp 0\n", skel->bss->schedule_ret); + CHECK(skel->bss->sched_ret != 9, "sched_ret", + "got %d, exp 0\n", skel->bss->sched_ret); + CHECK(skel->bss->invalid_ret != -EINVAL, "invalid_ret", + "got %d, exp %d\n", skel->bss->invalid_ret, -EINVAL); + +cleanup: + test_kallsyms_lookup__destroy(skel); +} + +void test_ksyms(void) +{ + if (test__start_subtest("ksyms_variables")) + test_ksyms_variables(); + if (test__start_subtest("kallsyms_lookup")) + test_kallsyms_lookup(); +} diff --git a/tools/testing/selftests/bpf/progs/test_kallsyms_lookup.c b/tools/testing/selftests/bpf/progs/test_kallsyms_lookup.c new file mode 100644 index 000000000000..4f15f1527ab4 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_kallsyms_lookup.c @@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2020 Google LLC. */ + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> + +extern const void schedule __ksym; + +#define SYMBOL_NAME_LEN 10 +char name[SYMBOL_NAME_LEN]; +char name_invalid[SYMBOL_NAME_LEN]; + +#define SYMBOL_TRUNCATED_NAME_LEN 6 +char name_truncated[SYMBOL_TRUNCATED_NAME_LEN]; + +#define MODULE_NAME_LEN 64 +char module_name[MODULE_NAME_LEN]; + +long schedule_ret; +long sched_ret; +long invalid_ret; + +SEC("raw_tp/sys_enter") +int handler(const void *ctx) +{ + schedule_ret = bpf_kallsyms_lookup((__u64)&schedule, + name, SYMBOL_NAME_LEN, + module_name, MODULE_NAME_LEN); + invalid_ret = bpf_kallsyms_lookup(0, + name_invalid, SYMBOL_NAME_LEN, + module_name, MODULE_NAME_LEN); + sched_ret = bpf_kallsyms_lookup((__u64)&schedule, name_truncated, + SYMBOL_TRUNCATED_NAME_LEN, + module_name, MODULE_NAME_LEN); + return 0; +} + +char _license[] SEC("license") = "GPL"; -- 2.29.2.454.gaff20da3a2-goog