public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Zheng Zhang <zheng.zhang@email.ucr.edu>,
	Kees Cook <keescook@chromium.org>,
	Sean Christopherson <seanjc@google.com>
Subject: [PATCH 2/7] KVM: selftests: Use pread() to read binary stats header
Date: Tue, 11 Jul 2023 16:01:26 -0700	[thread overview]
Message-ID: <20230711230131.648752-3-seanjc@google.com> (raw)
In-Reply-To: <20230711230131.648752-1-seanjc@google.com>

Use pread() with an explicit offset when reading the header and the header
name for a binary stats fd so that the common helper and the binary stats
test don't subtly rely on the file effectively being untouched, e.g. to
allow multiple reads of the header, name, etc.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 tools/testing/selftests/kvm/include/kvm_util_base.h | 6 ++++--
 tools/testing/selftests/kvm/kvm_binary_stats_test.c | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 07732a157ccd..eb1ff597bcca 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -362,8 +362,10 @@ static inline void read_stats_header(int stats_fd, struct kvm_stats_header *head
 {
 	ssize_t ret;
 
-	ret = read(stats_fd, header, sizeof(*header));
-	TEST_ASSERT(ret == sizeof(*header), "Read stats header");
+	ret = pread(stats_fd, header, sizeof(*header), 0);
+	TEST_ASSERT(ret == sizeof(*header),
+		    "Failed to read '%lu' header bytes, ret = '%ld'",
+		    sizeof(*header), ret);
 }
 
 struct kvm_stats_desc *read_stats_descriptors(int stats_fd,
diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
index a7001e29dc06..eae99d0e8377 100644
--- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c
+++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c
@@ -43,8 +43,10 @@ static void stats_test(int stats_fd)
 	id = malloc(header.name_size);
 	TEST_ASSERT(id, "Allocate memory for id string");
 
-	ret = read(stats_fd, id, header.name_size);
-	TEST_ASSERT(ret == header.name_size, "Read id string");
+	ret = pread(stats_fd, id, header.name_size, sizeof(header));
+	TEST_ASSERT(ret == header.name_size,
+		    "Expected header size '%u', read '%lu' bytes",
+		    header.name_size, ret);
 
 	/* Check id string, that should start with "kvm" */
 	TEST_ASSERT(!strncmp(id, "kvm", 3) && strlen(id) < header.name_size,
-- 
2.41.0.255.g8b1d071c50-goog


  parent reply	other threads:[~2023-07-11 23:01 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-11 23:01 [PATCH 0/7] KVM: Grab KVM references for stats fds Sean Christopherson
2023-07-11 23:01 ` [PATCH 1/7] KVM: Grab a reference to KVM for VM and vCPU stats file descriptors Sean Christopherson
2023-07-12 20:04   ` Kees Cook
2023-07-11 23:01 ` Sean Christopherson [this message]
2023-07-11 23:01 ` [PATCH 3/7] KVM: selftests: Clean up stats fd in common stats_test() helper Sean Christopherson
2023-07-11 23:01 ` [PATCH 4/7] KVM: selftests: Explicitly free vcpus array in binary stats test Sean Christopherson
2023-07-11 23:01 ` [PATCH 5/7] KVM: selftests: Verify userspace can create "redundant" binary stats files Sean Christopherson
2023-07-11 23:01 ` [PATCH 6/7] KVM: selftests: Verify stats fd can be dup()'d and read Sean Christopherson
2023-07-11 23:01 ` [PATCH 7/7] KVM: selftests: Verify stats fd is usable after VM fd has been closed Sean Christopherson
2023-07-26 15:17 ` [PATCH 0/7] KVM: Grab KVM references for stats fds Sean Christopherson
2023-07-29 15:02 ` Paolo Bonzini

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=20230711230131.648752-3-seanjc@google.com \
    --to=seanjc@google.com \
    --cc=keescook@chromium.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=zheng.zhang@email.ucr.edu \
    /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