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@xxxxxxxxxx> --- .../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