From: Jiri Olsa <jolsa@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>
Cc: bpf@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@chromium.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>
Subject: [PATCH bpf-next 3/3] selftests/bpf: Add build_id_parse kfunc test
Date: Tue, 8 Nov 2022 23:20:27 +0100 [thread overview]
Message-ID: <20221108222027.3409437-4-jolsa@kernel.org> (raw)
In-Reply-To: <20221108222027.3409437-1-jolsa@kernel.org>
Adding test for build_id_parse kfunc.
On bpf side it finds the vma of the test_progs text through
the test function pointer and reads its build id. On user
side we use readelf to get test_progs build id and compare
it with the one from bpf side.
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
.../bpf/prog_tests/vma_build_id_parse.c | 84 +++++++++++++++++++
.../selftests/bpf/progs/vma_build_id_parse.c | 34 ++++++++
2 files changed, 118 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/vma_build_id_parse.c
create mode 100644 tools/testing/selftests/bpf/progs/vma_build_id_parse.c
diff --git a/tools/testing/selftests/bpf/prog_tests/vma_build_id_parse.c b/tools/testing/selftests/bpf/prog_tests/vma_build_id_parse.c
new file mode 100644
index 000000000000..21c6f7771251
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/vma_build_id_parse.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include "vma_build_id_parse.skel.h"
+
+static int read_buildid(char **build_id)
+{
+ char tmp[] = "/tmp/dataXXXXXX";
+ char buf[200];
+ int err, fd;
+ FILE *f;
+
+ fd = mkstemp(tmp);
+ if (fd == -1)
+ return -1;
+ close(fd);
+
+ snprintf(buf, sizeof(buf),
+ "readelf -n ./test_progs 2>/dev/null | grep 'Build ID' | awk '{print $3}' > %s",
+ tmp);
+ err = system(buf);
+ if (!ASSERT_OK(err, "system"))
+ goto out;
+
+ f = fopen(tmp, "r");
+ if (!ASSERT_OK_PTR(f, "fopen")) {
+ err = -1;
+ goto out;
+ }
+ if (fscanf(f, "%ms$*\n", build_id) != 1) {
+ *build_id = NULL;
+ err = -1;
+ }
+ fclose(f);
+out:
+ unlink(tmp);
+ return err;
+}
+
+void test_vma_build_id_parse(void)
+{
+ char bpf_build_id[BPF_BUILD_ID_SIZE*2 + 1] = {}, *build_id;
+ LIBBPF_OPTS(bpf_test_run_opts, topts);
+ struct vma_build_id_parse *skel;
+ int i, err, prog_fd, size;
+
+ skel = vma_build_id_parse__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "vma_build_id_parse__open_and_load"))
+ return;
+
+ skel->bss->target_pid = getpid();
+ skel->bss->addr = (__u64)(uintptr_t)test_vma_build_id_parse;
+
+ err = vma_build_id_parse__attach(skel);
+ if (!ASSERT_OK(err, "vma_build_id_parse__attach"))
+ goto out;
+
+ prog_fd = bpf_program__fd(skel->progs.test1);
+ err = bpf_prog_test_run_opts(prog_fd, &topts);
+ ASSERT_OK(err, "test_run");
+ ASSERT_EQ(topts.retval, 0, "test_run");
+
+ ASSERT_EQ(skel->data->find_addr_ret, 0, "find_addr_ret");
+ ASSERT_GT(skel->data->vma_build_id_parse_ret, 0, "vma_build_id_parse_ret");
+
+ if (!ASSERT_OK(read_buildid(&build_id), "read_buildid"))
+ goto out;
+
+ size = skel->data->vma_build_id_parse_ret;
+ ASSERT_EQ(size, strlen(build_id)/2, "build_id_size");
+
+ /* Convert bpf build id to string, so we can compare it. */
+ for (i = 0; i < size; i++) {
+ sprintf(bpf_build_id + i*2, "%02x",
+ (unsigned char) skel->bss->build_id[i]);
+ }
+ ASSERT_STREQ(bpf_build_id, build_id, "build_ids_match");
+
+ free(build_id);
+out:
+ vma_build_id_parse__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/vma_build_id_parse.c b/tools/testing/selftests/bpf/progs/vma_build_id_parse.c
new file mode 100644
index 000000000000..bc5bc9e1808c
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/vma_build_id_parse.c
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define BPF_BUILD_ID_SIZE 20
+
+__u64 addr = 0;
+pid_t target_pid = 0;
+int find_addr_ret = -1;
+int vma_build_id_parse_ret = -1;
+
+unsigned char build_id[BPF_BUILD_ID_SIZE];
+
+static long check_vma(struct task_struct *task, struct vm_area_struct *vma,
+ void *data)
+{
+ vma_build_id_parse_ret = bpf_vma_build_id_parse(vma, (char *) build_id);
+ return 0;
+}
+
+SEC("fentry/bpf_fentry_test1")
+int BPF_PROG(test1, int a)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+
+ if (task->pid != target_pid)
+ return 0;
+
+ find_addr_ret = bpf_find_vma(task, addr, check_vma, NULL, 0);
+ return 0;
+}
--
2.38.1
prev parent reply other threads:[~2022-11-08 22:23 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-08 22:20 [PATCH bpf-next 0/3] bpf: Add bpf_vma_build_id_parse helper Jiri Olsa
2022-11-08 22:20 ` [PATCH bpf-next 1/3] bpf: Split btf_id/size union in struct bpf_func_proto Jiri Olsa
2022-11-08 22:20 ` [PATCH bpf-next 2/3] bpf: Add bpf_vma_build_id_parse helper Jiri Olsa
2022-11-09 0:42 ` Alexei Starovoitov
2022-11-09 13:35 ` Jiri Olsa
2022-11-08 22:20 ` Jiri Olsa [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221108222027.3409437-4-jolsa@kernel.org \
--to=jolsa@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=sdf@google.com \
--cc=songliubraving@fb.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox