bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
	daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com,
	eddyz87@gmail.com, memxor@gmail.com
Cc: Mykyta Yatsenko <yatsenko@meta.com>
Subject: [RFC PATCH v1 10/10] selftests/bpf: add file dynptr tests
Date: Fri,  3 Oct 2025 17:04:16 +0100	[thread overview]
Message-ID: <20251003160416.585080-11-mykyta.yatsenko5@gmail.com> (raw)
In-Reply-To: <20251003160416.585080-1-mykyta.yatsenko5@gmail.com>

From: Mykyta Yatsenko <yatsenko@meta.com>

Introducing selftests for validating file-backed dynptr works as
expected.
 * validate implementation supports dynptr slice and read operations
 * validate destructors should be paired with initializers

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
---
 .../selftests/bpf/prog_tests/file_reader.c    |  81 ++++++
 .../testing/selftests/bpf/progs/file_reader.c | 241 ++++++++++++++++++
 .../selftests/bpf/progs/file_reader_fail.c    |  57 +++++
 3 files changed, 379 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/file_reader.c
 create mode 100644 tools/testing/selftests/bpf/progs/file_reader.c
 create mode 100644 tools/testing/selftests/bpf/progs/file_reader_fail.c

diff --git a/tools/testing/selftests/bpf/prog_tests/file_reader.c b/tools/testing/selftests/bpf/prog_tests/file_reader.c
new file mode 100644
index 000000000000..b314ad4326f1
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/file_reader.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <test_progs.h>
+#include <network_helpers.h>
+#include "file_reader.skel.h"
+#include "file_reader_fail.skel.h"
+
+__thread int tls_counter;
+const char *user_ptr = "hello world";
+char file_contents[256000];
+
+enum file_reader_test {
+	VALIDATE_LARGE_FILE = 1,
+	SEARCH_ELF = 2,
+};
+
+static int initialize_file_contents(void)
+{
+	int fd;
+	ssize_t n;
+	int err = 0;
+
+	fd = open("/proc/self/exe", O_RDONLY);
+	if (!ASSERT_GT(fd, 0, "Open /proc/self/exe\n"))
+		return 1;
+
+	n = read(fd, file_contents, sizeof(file_contents));
+	if (!ASSERT_EQ(n, sizeof(file_contents), "Read /proc/self/exe\n"))
+		err = 1;
+
+	posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
+	close(fd);
+	return err;
+}
+
+static void run_test(enum file_reader_test test_type)
+{
+	struct file_reader *skel;
+	int err;
+	char data[256];
+	LIBBPF_OPTS(bpf_test_run_opts, opts, .data_in = &data, .repeat = 1,
+		    .data_size_in = sizeof(data));
+
+	skel = file_reader__open();
+	if (!ASSERT_OK_PTR(skel, "file_reader__open"))
+		return;
+
+	skel->bss->user_ptr = (void *)user_ptr;
+	skel->bss->user_buf = file_contents;
+	skel->rodata->user_buf_sz = sizeof(file_contents);
+	skel->rodata->test_type = test_type;
+
+	err = file_reader__load(skel);
+	if (!ASSERT_OK(err, "file_reader__load"))
+		return;
+
+	err = initialize_file_contents();
+	if (!ASSERT_OK(err, "initialize file contents"))
+		goto cleanup;
+
+	err = file_reader__attach(skel);
+	if (!ASSERT_OK(err, "file_reader__attach"))
+		goto cleanup;
+
+	getpid();
+
+	ASSERT_EQ(skel->bss->err, 0, "err");
+cleanup:
+	file_reader__destroy(skel);
+}
+
+void test_file_reader(void)
+{
+	if (test__start_subtest("test_large_file"))
+		run_test(VALIDATE_LARGE_FILE);
+	if (test__start_subtest("test_search_elf"))
+		run_test(SEARCH_ELF);
+
+	RUN_TESTS(file_reader_fail);
+}
diff --git a/tools/testing/selftests/bpf/progs/file_reader.c b/tools/testing/selftests/bpf/progs/file_reader.c
new file mode 100644
index 000000000000..9dd9a68f3563
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/file_reader.c
@@ -0,0 +1,241 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <string.h>
+#include <stdbool.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+#define ELFMAG "\177ELF"
+#define SELFMAG 4
+#define ET_NONE 0
+#define ET_REL 1
+#define ET_EXEC 2
+#define ET_DYN 3
+#define ET_CORE 4
+#define ET_LOPROC 0xff00
+#define ET_HIPROC 0xffff
+#define EI_CLASS 4
+#define ELFCLASS32 1
+#define ELFCLASS64 2
+#define STT_TLS 6
+
+#define ELF_ST_BIND(x) ((x) >> 4)
+#define ELF_ST_TYPE(x) ((x) & 0xf)
+#define ELF32_ST_BIND(x) ELF_ST_BIND(x)
+#define ELF32_ST_TYPE(x) ELF_ST_TYPE(x)
+#define ELF64_ST_BIND(x) ELF_ST_BIND(x)
+#define ELF64_ST_TYPE(x) ELF_ST_TYPE(x)
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, int);
+	__type(value, struct elem);
+} arrmap SEC(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_RINGBUF);
+	__uint(max_entries, 10000000);
+} ringbuf SEC(".maps");
+
+struct elem {
+	struct file *file;
+	struct bpf_task_work tw;
+};
+
+enum file_reader_test {
+	VALIDATE_LARGE_FILE = 1,
+	SEARCH_ELF = 2,
+};
+
+int err;
+void *user_ptr;
+char buf[1024];
+char *user_buf;
+volatile const __u32 user_buf_sz;
+volatile const __s32 test_type = -1;
+
+static int process_vma(struct task_struct *task, struct vm_area_struct *vma, void *data);
+static int search_elf(struct file *file);
+static int validate_large_file_read(struct file *file);
+static int task_work_callback(struct bpf_map *map, void *key, void *value);
+
+SEC("raw_tp/sys_enter")
+int on_getpid(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+	struct elem *work;
+	int key = 0;
+
+	work = bpf_map_lookup_elem(&arrmap, &key);
+	if (!work) {
+		err = 1;
+		return 0;
+	}
+	bpf_task_work_schedule_signal(task, &work->tw, &arrmap, task_work_callback, NULL);
+	return 0;
+}
+
+static int task_work_callback(struct bpf_map *map, void *key, void *value)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+
+	bpf_find_vma(task, (unsigned long)user_ptr, process_vma, NULL, 0);
+	return 0;
+}
+
+static int process_vma(struct task_struct *task, struct vm_area_struct *vma, void *data)
+{
+	switch (test_type) {
+	case VALIDATE_LARGE_FILE:
+		err = validate_large_file_read(vma->vm_file);
+		break;
+	case SEARCH_ELF:
+		err = search_elf(vma->vm_file);
+		break;
+	default:
+		err = 1;
+	}
+	return err;
+}
+
+static int validate_large_file_read(struct file *file)
+{
+	struct bpf_dynptr dynptr;
+	int err, i;
+	char *rbuf1 = NULL, *rbuf2 = NULL;
+
+	if (!file) {
+		err = 1;
+		return 1;
+	}
+
+	err = bpf_dynptr_from_file(file, 0, &dynptr);
+	if (err)
+		goto cleanup_file;
+
+	rbuf1 = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0);
+	if (!rbuf1)
+		goto cleanup_file;
+
+	rbuf2 = bpf_ringbuf_reserve(&ringbuf, user_buf_sz, 0);
+	if (!rbuf2)
+		goto cleanup_all;
+
+	bpf_dynptr_read(rbuf1, user_buf_sz, &dynptr, 0, 0);
+	bpf_copy_from_user(rbuf2, user_buf_sz, user_buf);
+	/* Verify file contents read from BPF is the same as the one read from userspace */
+	bpf_for(i, 0, user_buf_sz) {
+		if (i >= 256000 || rbuf1[i] != rbuf2[i]) {
+			err = 1;
+			break;
+		}
+	}
+
+cleanup_all:
+	if (rbuf1)
+		bpf_ringbuf_discard(rbuf1, 0);
+	if (rbuf2)
+		bpf_ringbuf_discard(rbuf2, 0);
+cleanup_file:
+	bpf_dynptr_file_discard(&dynptr);
+	return err ? 1 : 0;
+}
+
+/* Finds thread local variable `tls_counter` in this executable's ELF */
+static int search_elf(struct file *file)
+{
+	Elf64_Ehdr ehdr;
+	Elf64_Shdr shdrs;
+	Elf64_Shdr symtab, strtab, tmp;
+	const Elf64_Sym *symbol;
+	int count, off, i, e_shnum, e_shoff, e_shentsize, sections = 0;
+	const char *string;
+	struct bpf_dynptr dynptr;
+	const __u32 slen = 11;
+	static const char *needle = "tls_counter";
+
+	if (!file) {
+		err = 1;
+		return 1;
+	}
+
+	err = bpf_dynptr_from_file(file, 0, &dynptr);
+	if (err)
+		goto fail;
+
+	err = bpf_dynptr_read(&ehdr, sizeof(ehdr), &dynptr, 0, 0);
+	if (err)
+		goto fail;
+
+	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0)
+		goto fail;
+
+	if (ehdr.e_type != ET_EXEC && ehdr.e_type != ET_DYN)
+		goto fail;
+
+	if (ehdr.e_ident[EI_CLASS] != ELFCLASS64)
+		goto fail;
+
+	e_shnum = ehdr.e_shnum;
+	e_shoff = ehdr.e_shoff;
+	e_shentsize = ehdr.e_shentsize;
+
+	err = bpf_dynptr_read(&shdrs, sizeof(shdrs), &dynptr,
+			      e_shoff + e_shentsize * ehdr.e_shstrndx, 0);
+	if (err)
+		goto fail;
+
+	off = shdrs.sh_offset;
+
+	__builtin_memset(&symtab, 0, sizeof(symtab));
+	__builtin_memset(&strtab, 0, sizeof(strtab));
+	bpf_for(i, 0, e_shnum)
+	{
+		err = bpf_dynptr_read(&tmp, sizeof(Elf64_Shdr), &dynptr, e_shoff + e_shentsize * i,
+				      0);
+		if (err)
+			goto fail;
+
+		string = bpf_dynptr_slice(&dynptr, off + tmp.sh_name, buf, slen);
+		if (!string)
+			goto fail;
+
+		if (bpf_strncmp(string, slen, ".symtab") == 0) {
+			symtab = tmp;
+			++sections;
+		} else if (bpf_strncmp(string, slen, ".strtab") == 0) {
+			strtab = tmp;
+			++sections;
+		}
+		if (sections == 2)
+			break;
+	}
+	if (sections != 2)
+		goto fail;
+
+	count = symtab.sh_size / sizeof(Elf64_Sym);
+	bpf_for(i, 0, count)
+	{
+		symbol = bpf_dynptr_slice(&dynptr, symtab.sh_offset + sizeof(Elf64_Sym) * i, buf,
+					  sizeof(Elf64_Sym));
+		if (!symbol)
+			goto fail;
+		if (symbol->st_name == 0 || ELF64_ST_TYPE(symbol->st_info) != STT_TLS)
+			continue;
+		string = bpf_dynptr_slice(&dynptr, strtab.sh_offset + symbol->st_name, buf, slen);
+		if (!string)
+			goto fail;
+		if (bpf_strncmp(string, slen, needle) == 0)
+			goto success;
+	}
+fail:
+	err = 1;
+success:
+	bpf_dynptr_file_discard(&dynptr);
+	return err ? 1 : 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/file_reader_fail.c b/tools/testing/selftests/bpf/progs/file_reader_fail.c
new file mode 100644
index 000000000000..449c4f9a1c74
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/file_reader_fail.c
@@ -0,0 +1,57 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <string.h>
+#include <stdbool.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+int err;
+void *user_ptr;
+
+char buf[256];
+
+static long process_vma_unreleased_ref(struct task_struct *task, struct vm_area_struct *vma,
+				       void *data)
+{
+	struct bpf_dynptr dynptr;
+
+	if (!vma->vm_file)
+		return 1;
+
+	err = bpf_dynptr_from_file(vma->vm_file, 0, &dynptr);
+	return err ? 1 : 0;
+}
+
+SEC("fentry.s/" SYS_PREFIX "sys_nanosleep")
+__failure __msg("Unreleased reference id=") int on_nanosleep_unreleased_ref(void *ctx)
+{
+	struct task_struct *task = bpf_get_current_task_btf();
+
+	bpf_find_vma(task, (unsigned long)user_ptr, process_vma_unreleased_ref, NULL, 0);
+	return 0;
+}
+
+SEC("xdp")
+__failure __msg("Expected a dynptr of type file as arg #0")
+int xdp_wrong_dynptr_type(struct xdp_md *xdp)
+{
+	struct bpf_dynptr dynptr;
+
+	bpf_dynptr_from_xdp(xdp, 0, &dynptr);
+	bpf_dynptr_file_discard(&dynptr);
+	return 0;
+}
+
+SEC("xdp")
+__failure __msg("Expected an initialized dynptr as arg #0")
+int xdp_no_dynptr_type(struct xdp_md *xdp)
+{
+	struct bpf_dynptr dynptr;
+
+	bpf_dynptr_file_discard(&dynptr);
+	return 0;
+}
-- 
2.51.0


  parent reply	other threads:[~2025-10-03 16:04 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-03 16:04 [RFC PATCH v1 00/10] bpf: Introduce file dynptr Mykyta Yatsenko
2025-10-03 16:04 ` [RFC PATCH v1 01/10] selftests/bpf: remove unnecessary kfunc prototypes Mykyta Yatsenko
2025-10-03 17:46   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 02/10] bpf: widen dynptr size/offset to 64 bit Mykyta Yatsenko
2025-10-03 18:16   ` Andrii Nakryiko
2025-10-03 18:40   ` Eduard Zingerman
2025-10-03 18:53     ` Mykyta Yatsenko
2025-10-03 16:04 ` [RFC PATCH v1 03/10] lib: extract freader into a separate files Mykyta Yatsenko
2025-10-03 18:16   ` Andrii Nakryiko
2025-10-03 20:04   ` Alexei Starovoitov
2025-10-03 16:04 ` [RFC PATCH v1 04/10] lib/freader: support reading more than 2 folios Mykyta Yatsenko
2025-10-03 18:16   ` Andrii Nakryiko
2025-10-03 18:29     ` Mykyta Yatsenko
2025-10-03 18:46       ` Andrii Nakryiko
2025-10-03 16:04 ` [RFC PATCH v1 05/10] bpf: verifier: centralize const dynptr check in unmark_stack_slots_dynptr() Mykyta Yatsenko
2025-10-03 18:18   ` Andrii Nakryiko
2025-10-03 19:02   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 06/10] bpf: add plumbing for file-backed dynptr Mykyta Yatsenko
2025-10-03 18:38   ` Andrii Nakryiko
2025-10-03 20:55   ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 07/10] bpf: add kfuncs and helpers support for file dynptrs Mykyta Yatsenko
2025-10-03 18:38   ` Andrii Nakryiko
2025-10-03 18:59     ` Mykyta Yatsenko
2025-10-03 21:35   ` Eduard Zingerman
2025-10-08  0:25     ` Mykyta Yatsenko
2025-10-03 16:04 ` [RFC PATCH v1 08/10] bpf: verifier: refactor kfunc specialization Mykyta Yatsenko
2025-10-03 22:08   ` Eduard Zingerman
2025-10-08  0:35     ` Mykyta Yatsenko
2025-10-08 18:27     ` Mykyta Yatsenko
2025-10-08 19:15       ` Eduard Zingerman
2025-10-03 16:04 ` [RFC PATCH v1 09/10] bpf: dispatch to sleepable file dynptr Mykyta Yatsenko
2025-10-03 18:45   ` Andrii Nakryiko
2025-10-03 20:10   ` Alexei Starovoitov
2025-10-03 22:17   ` Eduard Zingerman
2025-10-03 16:04 ` Mykyta Yatsenko [this message]
2025-10-03 20:02   ` [RFC PATCH v1 10/10] selftests/bpf: add file dynptr tests Andrii Nakryiko
2025-10-06 11:50     ` Mykyta Yatsenko
2025-10-06 16:15       ` Andrii Nakryiko
2025-10-03 22:24   ` Eduard Zingerman
2025-10-06 11:54     ` Mykyta Yatsenko
2025-10-08  0:39     ` Mykyta Yatsenko

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=20251003160416.585080-11-mykyta.yatsenko5@gmail.com \
    --to=mykyta.yatsenko5@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kafai@meta.com \
    --cc=kernel-team@meta.com \
    --cc=memxor@gmail.com \
    --cc=yatsenko@meta.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;
as well as URLs for NNTP newsgroup(s).