Linux Trace Development
 help / color / mirror / Atom feed
From: Cao Ruichuang <create0818@163.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-trace-devel@vger.kernel.org,
	Tzvetomir Stoyanov <tz.stoyanov@gmail.com>,
	Emil Thorsoe <ethorsoe@tuxera.com>,
	Cao Ruichuang <create0818@163.com>
Subject: [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF
Date: Mon,  1 Jun 2026 18:37:49 +0800	[thread overview]
Message-ID: <20260601103750.68805-2-create0818@163.com> (raw)
In-Reply-To: <20260601103750.68805-1-create0818@163.com>

test_btf_read() falls back to malloc() and read() when mmap() of
/sys/kernel/btf/vmlinux fails. The fallback currently assumes that one
read(fd, buf, st.st_size) call returns the whole file.

That is not guaranteed. On a Rocky Linux 9.7 system with a readable
/sys/kernel/btf/vmlinux, mmap() fails with ENODEV and a single read()
returns only 4096 bytes from a 5150407-byte file. Reading in a loop
returns the complete file.

Read the BTF file in a loop so the fallback path handles short reads
correctly and avoids passing partial BTF data to tep_load_btf().

Fixes: 5b44859765fc ("libtraceevent utest: Read btf file in utest if mmap fails")
Assisted-by: Codex:gpt-5.5
Signed-off-by: Cao Ruichuang <create0818@163.com>
---
 utest/traceevent-utest.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/utest/traceevent-utest.c b/utest/traceevent-utest.c
index 1c3a4c2..b93d686 100644
--- a/utest/traceevent-utest.c
+++ b/utest/traceevent-utest.c
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -506,6 +507,28 @@ static void test_parse_dma_addr(void)
 			    data, sizeof(data), "dma=0x1234abcd");
 }
 
+static int read_full(int fd, void *buf, size_t size)
+{
+	char *data = buf;
+	size_t total = 0;
+
+	while (total < size) {
+		ssize_t ret;
+
+		ret = read(fd, data + total, size - total);
+		if (ret < 0) {
+			if (errno == EINTR)
+				continue;
+			return -1;
+		}
+		if (!ret)
+			return -1;
+		total += ret;
+	}
+
+	return 0;
+}
+
 static void test_btf_read(void)
 {
 	uint64_t args[] = {0x7ffe7d33f3d0, 0, 0, 0, 0, 0};
@@ -525,6 +548,8 @@ static void test_btf_read(void)
 
 	buf = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
 	if (buf == MAP_FAILED) {
+		int ret;
+
 		malloced = true;
 		buf = malloc(st.st_size);
 		if (buf == NULL) {
@@ -532,7 +557,13 @@ static void test_btf_read(void)
 			close(fd);
 			return;
 		}
-		CU_TEST(read(fd, buf, st.st_size) == st.st_size);
+		ret = read_full(fd, buf, st.st_size);
+		CU_TEST(ret == 0);
+		if (ret < 0) {
+			free(buf);
+			close(fd);
+			return;
+		}
 	}
 	CU_TEST(tep_load_btf(test_tep, buf, st.st_size) == 0);
 
-- 
2.39.5 (Apple Git-154)


  reply	other threads:[~2026-06-01 10:38 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-01 10:37 [PATCH 0/2] libtraceevent: Fix BTF utest host dependencies Cao Ruichuang
2026-06-01 10:37 ` Cao Ruichuang [this message]
2026-06-01 14:11   ` [PATCH 1/2] libtraceevent utest: Handle short reads when reading BTF Steven Rostedt
2026-06-01 10:37 ` [PATCH 2/2] libtraceevent utest: Avoid host-specific getname_flags output Cao Ruichuang

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=20260601103750.68805-2-create0818@163.com \
    --to=create0818@163.com \
    --cc=ethorsoe@tuxera.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=tz.stoyanov@gmail.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