diff mbox series

[bpf-next,2/2] selftests/bpf: Add bpf_kallsyms_lookup test

Message ID 20201126165748.1748417-2-revest@google.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series [bpf-next,1/2] bpf: Add a bpf_kallsyms_lookup helper | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 1 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: From:/Signed-off-by: email address mismatch: 'From: Florent Revest <revest@chromium.org>' != 'Signed-off-by: Florent Revest <revest@google.com>' WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link
netdev/stable success Stable not CCed

Commit Message

Florent Revest Nov. 26, 2020, 4:57 p.m. UTC
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@google.com>
---
 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

Comments

Andrii Nakryiko Dec. 2, 2020, 12:57 a.m. UTC | #1
On Thu, Nov 26, 2020 at 8:59 AM Florent Revest <revest@chromium.org> wrote:
>
> 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@google.com>
> ---
>  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

it's already a requirement, but good to codify it ;)

> 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)

now it should be static

>  {
>         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)

static

> +{
> +       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);

there is ASSERT_STREQ() that does this nicely and succinctly


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

= 0 or = {} for all the variables

> +
> +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
>
diff mbox series

Patch

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