From nobody Mon Apr 6 18:30:30 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1773842564822974.521159640125; Wed, 18 Mar 2026 07:02:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w2rSu-0002qN-9y; Wed, 18 Mar 2026 10:01:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2rSp-0002n5-5E for qemu-devel@nongnu.org; Wed, 18 Mar 2026 10:01:36 -0400 Received: from mgamail.intel.com ([198.175.65.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2rSm-0005nK-KM for qemu-devel@nongnu.org; Wed, 18 Mar 2026 10:01:34 -0400 Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 07:01:31 -0700 Received: from junjieca-mobl.ccr.corp.intel.com ([10.124.242.111]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 07:01:29 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1773842493; x=1805378493; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=482T7NbGblsuvsjZFboPfT+pkuzemfVXyB8k6UhHv7E=; b=XzILZ3veEcvw5Ra4jKRlOKObcKL398eO/sICqmVShAvtbtx+Y1KGx3ky 9qW8pmYKNbOzuNiymiljebNogS19Jv3hVnTqrxnoI2rubLtbOnUp1A9on vUwGGrjYyXj2DJOuYtHTK4MIr/YXsxet4JQUaZsDY1PAeNZ0VOFBxdEfx zfCcLl/G0se45WtHrx6cu2mBUNIyiqGuyxnZb9H2CjA3ljfsXGzjelyUc vMPzD04oTHP0PwaepT9k5LOc7a8wIQK3NmpYyZs/Q8DH3tEU5pG/5Ec+E Ggp/5KuYU/QmpCkDKDcXqqemqEeXzOJuEeoxJhQnZovKjvvXFOEQa5BTM Q==; X-CSE-ConnectionGUID: gG7LUq2/RGKEshgdSEFdXA== X-CSE-MsgGUID: ZzwBpGNaT660ZAacw9tuoQ== X-IronPort-AV: E=McAfee;i="6800,10657,11733"; a="74925778" X-IronPort-AV: E=Sophos;i="6.23,127,1770624000"; d="scan'208";a="74925778" X-CSE-ConnectionGUID: TYI1HMJWR1aOBe+v7fPZGg== X-CSE-MsgGUID: xQKW/ACnTSisSBBw40XDtQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,127,1770624000"; d="scan'208";a="218205218" From: Junjie Cao To: qemu-devel@nongnu.org Cc: peterx@redhat.com, farosas@suse.de, berrange@redhat.com, junjie.cao@intel.com Subject: [PATCH v2 1/3] io/channel: introduce qio_channel_pread{v, }_all() and preadv_all_eof() Date: Wed, 18 Mar 2026 22:01:11 +0800 Message-ID: <20260318140113.434-2-junjie.cao@intel.com> X-Mailer: git-send-email 2.53.0.windows.2 In-Reply-To: <20260318140113.434-1-junjie.cao@intel.com> References: <20260318140113.434-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=198.175.65.18; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.819, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.903, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1773842567444158500 qio_channel_pread() and qio_channel_preadv() perform a single read and may return a short result. Callers that need all bytes currently have to open-code a retry loop or simply treat a short read as an error. Introduce three new helpers following the existing read_all / readv_all pattern: qio_channel_preadv_all_eof() =E2=80=93 retry loop; returns 1 on success, 0 on clean EOF, -1 on error. qio_channel_preadv_all() =E2=80=93 wraps _eof; treats early EOF as error; returns 0 / -1. qio_channel_pread_all() =E2=80=93 single-buffer convenience wrapper around preadv_all(). These advance the file offset internally after each partial read, so callers only need a single call. All three are marked coroutine_mixed_fn, consistent with the existing _all helpers. Suggested-by: Peter Xu Signed-off-by: Junjie Cao --- include/io/channel.h | 63 ++++++++++++++++++++++++++++++++ io/channel.c | 85 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) diff --git a/include/io/channel.h b/include/io/channel.h index 1b02350437..d5b14c8c77 100644 --- a/include/io/channel.h +++ b/include/io/channel.h @@ -634,6 +634,69 @@ ssize_t qio_channel_preadv(QIOChannel *ioc, const stru= ct iovec *iov, ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, size_t buflen, off_t offset, Error **errp); =20 +/** + * qio_channel_preadv_all_eof: + * @ioc: the channel object + * @iov: the array of memory regions to read data into + * @niov: the length of the @iov array + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads data contiguously from the channel into @iov starting at + * @offset, retrying on short reads until all requested bytes have + * been read. The offset is advanced internally after each partial + * read. + * + * Returns: 1 if all bytes were read, 0 if no data could be read + * before encountering end-of-file, or -1 on error + */ +int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp); + +/** + * qio_channel_preadv_all: + * @ioc: the channel object + * @iov: the array of memory regions to read data into + * @niov: the length of the @iov array + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads data contiguously from the channel into @iov starting at + * @offset, retrying on short reads until all requested bytes have + * been read. Unlike qio_channel_preadv_all_eof(), end-of-file + * before all data has been read is treated as an error. + * + * Returns: 0 if all bytes were read, or -1 on error + */ +int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp); + +/** + * qio_channel_pread_all: + * @ioc: the channel object + * @buf: the memory region to read data into + * @buflen: the number of bytes to read into @buf + * @offset: the starting offset in the channel to read from + * @errp: pointer to a NULL-initialized error object + * + * Reads @buflen bytes contiguously from the channel at @offset + * into @buf, retrying on short reads. End-of-file before all data + * has been read is treated as an error. + * + * Returns: 0 if all bytes were read, or -1 on error + */ +int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc, + void *buf, + size_t buflen, + off_t offset, + Error **errp); + /** * qio_channel_shutdown: * @ioc: the channel object diff --git a/io/channel.c b/io/channel.c index cc02d997a4..e18c40aa0b 100644 --- a/io/channel.c +++ b/io/channel.c @@ -507,6 +507,91 @@ ssize_t qio_channel_pread(QIOChannel *ioc, void *buf, = size_t buflen, return qio_channel_preadv(ioc, &iov, 1, offset, errp); } =20 +int coroutine_mixed_fn qio_channel_preadv_all_eof(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp) +{ + int ret =3D -1; + struct iovec *local_iov =3D g_new(struct iovec, niov); + struct iovec *local_iov_head =3D local_iov; + unsigned int nlocal_iov =3D niov; + bool partial =3D false; + + nlocal_iov =3D iov_copy(local_iov, nlocal_iov, + iov, niov, + 0, iov_size(iov, niov)); + + while (nlocal_iov > 0) { + ssize_t len; + len =3D qio_channel_preadv(ioc, local_iov, nlocal_iov, offset, err= p); + + if (len =3D=3D QIO_CHANNEL_ERR_BLOCK) { + qio_channel_wait_cond(ioc, G_IO_IN); + continue; + } + + if (len =3D=3D 0) { + if (!partial) { + ret =3D 0; + goto cleanup; + } + error_setg(errp, + "Unexpected end-of-file before all data were read"); + goto cleanup; + } + + if (len < 0) { + goto cleanup; + } + + partial =3D true; + offset +=3D len; + iov_discard_front(&local_iov, &nlocal_iov, len); + } + + ret =3D 1; + + cleanup: + g_free(local_iov_head); + return ret; +} + +int coroutine_mixed_fn qio_channel_preadv_all(QIOChannel *ioc, + const struct iovec *iov, + size_t niov, + off_t offset, + Error **errp) +{ + int ret =3D qio_channel_preadv_all_eof(ioc, iov, niov, offset, errp); + + if (ret =3D=3D 0) { + error_setg(errp, + "Unexpected end-of-file before all data were read"); + return -1; + } + if (ret =3D=3D 1) { + return 0; + } + + return ret; +} + +int coroutine_mixed_fn qio_channel_pread_all(QIOChannel *ioc, + void *buf, + size_t buflen, + off_t offset, + Error **errp) +{ + struct iovec iov =3D { + .iov_base =3D buf, + .iov_len =3D buflen + }; + + return qio_channel_preadv_all(ioc, &iov, 1, offset, errp); +} + int qio_channel_shutdown(QIOChannel *ioc, QIOChannelShutdown how, Error **errp) --=20 2.43.0 From nobody Mon Apr 6 18:30:30 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1773842542; cv=none; d=zohomail.com; s=zohoarc; b=bf+U2HzopkKcPFQo0kEOGwZEcJq87SgXB2RFKlGKxH3giSDad+yIJ2lnRvglBdVgitjisKSeUuhoeQjzIik3f8kmX4Ux/FAURJygGih/Sbpv7v5WGZOTxegilNWXfSnmmf2LgXRUKjjEIsOA3NTS23U74Kpow5K1ahqDRfqy8g8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773842542; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=AVj2vhh6Zz3swEJ+R1hAoymN5+uXePZyHA4A3uvpCck=; b=gRFWBn1qs5sg4ApRrcKWMcVXnPvTD4s7Y9XoaohrMK2i44QprZCKwxVXCrH9HqItu7rY8xjxqh49WfB2rw3pRQbCPW8nW2ELCgDwlGU0AwXkRG4qJ29bP1jqnAztUDL37Ae71QcgW1W1EkOSFEn7LXs/nPytbyZSG2TjP6bIDGU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1773842542535719.7836006822321; Wed, 18 Mar 2026 07:02:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w2rSz-0002rh-M5; Wed, 18 Mar 2026 10:01:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2rSy-0002rK-Ce for qemu-devel@nongnu.org; Wed, 18 Mar 2026 10:01:44 -0400 Received: from mgamail.intel.com ([198.175.65.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2rSw-0005oH-T7 for qemu-devel@nongnu.org; Wed, 18 Mar 2026 10:01:44 -0400 Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 07:01:42 -0700 Received: from junjieca-mobl.ccr.corp.intel.com ([10.124.242.111]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 07:01:40 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1773842503; x=1805378503; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=90kUuCitCAIs4715mOIY03O2iNep11NxIP4ar18j9X4=; b=ZBOsFV5HgA8zm//AWWAN5/3fthjfvdOp6yNet530e00iFl+D0V92Uyp0 cosZMmB74NZ5L7Y3vFwNTe0fZqMD/QACeegsZ8hEzWy4+Zv8UliCZdKrT PGIndz/+9KF9rsmov9FxmbU8hyH76IeQfCmPiTnuCR1uwzP4BKChtC0li w/TWLhdv9IciX1xMvf230EWHAYmS/S12WS6r1fkg0dezsVbjYEX39NpMk oPdpD/PauXM+yRDJkEQZuSobAUWbUJT2buV6wPsrxRisJNZJfmpihov68 veTLdjY5//S2O5uG5oeYJkyFwe7ojF9DwxZZzmloIZgwoprhe1iR1Qfu4 A==; X-CSE-ConnectionGUID: GDJ0cuX7RK+f3K/i3vA3KA== X-CSE-MsgGUID: /3ewt5Q7TlqSuQVV9XSdGw== X-IronPort-AV: E=McAfee;i="6800,10657,11733"; a="74925792" X-IronPort-AV: E=Sophos;i="6.23,127,1770624000"; d="scan'208";a="74925792" X-CSE-ConnectionGUID: 30ehs/XVSkawEEgxCN7PdQ== X-CSE-MsgGUID: FytE+OzFTLipGayDVGS1UQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,127,1770624000"; d="scan'208";a="218205230" From: Junjie Cao To: qemu-devel@nongnu.org Cc: peterx@redhat.com, farosas@suse.de, berrange@redhat.com, junjie.cao@intel.com Subject: [PATCH v2 2/3] migration/file: fix type mismatch and NULL deref in multifd_file_recv_data Date: Wed, 18 Mar 2026 22:01:12 +0800 Message-ID: <20260318140113.434-3-junjie.cao@intel.com> X-Mailer: git-send-email 2.53.0.windows.2 In-Reply-To: <20260318140113.434-1-junjie.cao@intel.com> References: <20260318140113.434-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=198.175.65.18; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.819, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.903, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1773842544744154100 Content-Type: text/plain; charset="utf-8" multifd_file_recv_data() stores the return value of qio_channel_pread() (ssize_t) in a size_t variable. On I/O error the -1 return value wraps to SIZE_MAX, producing a nonsensical read size in the error message. More critically, a short read (0 <=3D ret < data->size) is possible when the migration file is truncated. In that case qio_channel_pread() returns a non-negative value without setting *errp. The function then calls error_prepend(errp, ...) which dereferences *errp -- a NULL pointer -- crashing QEMU. Fix both issues by switching to qio_channel_pread_all() introduced in the previous commit, which retries on short reads and treats end-of-file as an error, so the caller no longer needs to check the byte count manually. Add ERRP_GUARD() so that error_prepend() works correctly even when errp is &error_fatal or NULL. Suggested-by: Peter Xu Signed-off-by: Junjie Cao Reviewed-by: Daniel P. Berrang=C3=A9 --- migration/file.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/migration/file.c b/migration/file.c index 5618aced49..b0d3affa4c 100644 --- a/migration/file.c +++ b/migration/file.c @@ -254,16 +254,15 @@ int file_write_ramblock_iov(QIOChannel *ioc, const st= ruct iovec *iov, =20 int multifd_file_recv_data(MultiFDRecvParams *p, Error **errp) { + ERRP_GUARD(); MultiFDRecvData *data =3D p->data; - size_t ret; - - ret =3D qio_channel_pread(p->c, (char *) data->opaque, - data->size, data->file_offset, errp); - if (ret !=3D data->size) { - error_prepend(errp, - "multifd recv (%u): read 0x%zx, expected 0x%zx", - p->id, ret, data->size); - return -1; + int ret; + + ret =3D qio_channel_pread_all(p->c, (char *) data->opaque, + data->size, data->file_offset, errp); + if (ret < 0) { + error_prepend(errp, "multifd recv (%u): ", p->id); + return ret; } =20 return 0; --=20 2.43.0 From nobody Mon Apr 6 18:30:30 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1773842546; cv=none; d=zohomail.com; s=zohoarc; b=bb1iIsmjvzw5klbBapNbiea3bWiCpfTUQOcxQVUVs8wiuFwrYy+9V1zgpJcjqbkBFgMc04Z3BW5pIdw9UHPiUJ6dE/m8arx/zFfMe9cm/GAxkwSjDKOFQTHWKAri58Xe8Oq+sMsJ8QJjz+LxUxodObeZVFFiwZ1D7pI/QguA/tg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773842546; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=DZPGID8CSXr0nI2txknfgW5Rl1fgdIHWANGizAaep2U=; b=MIQ6cWc1Javj+H4ROzZtMQweHGaxqEhsjHZIn8cusGdNT0rhinQRWq7AUoWAvlkwO/Bbeh5VuuviAQ+dSwD43+9HIHR7m7ynVgjI0zWnFZupOz6bfbtsajKVbAunX+/DuanKcbtAMZclU8VN94H38nAAKI3bAV7fPUsh0QpNwb0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1773842546166949.7552154271655; Wed, 18 Mar 2026 07:02:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w2rT7-0002sU-Qj; Wed, 18 Mar 2026 10:01:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2rT5-0002sE-FW for qemu-devel@nongnu.org; Wed, 18 Mar 2026 10:01:52 -0400 Received: from mgamail.intel.com ([198.175.65.18]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w2rT3-0005oH-IS for qemu-devel@nongnu.org; Wed, 18 Mar 2026 10:01:51 -0400 Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 07:01:49 -0700 Received: from junjieca-mobl.ccr.corp.intel.com ([10.124.242.111]) by fmviesa006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Mar 2026 07:01:47 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1773842510; x=1805378510; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JuzEqzQ9/Twxxz2HhTp1rJeEyxn7vmJTdJXw53AO258=; b=DjWCpRFbDMFTbQbB+lligdfxLDCH9EaZWFSVuduzpT+Ec2bO8RisrRU7 FLuLlEEf6OvfVJ1iRzaH4yhYXu33v6+wHMIAMeHW7tUjYrOx6b4VwlUOA aIsUEcPbTJoHXVnp5GImA5axjmZ5BDck26jOG7cXzkpuN7ML1ZKiKLPXB 5CrHIwpChgGE0YjyNQMH2Tc5b3u7A7Aw0cfAEzuO9oRCmlmozO3Pr5l+e ZWFNW2DDw2kint+0LgKhIczI3B5ka0RIudC3jNfcHU6aTX4cbl6QLae69 8QLTbn8m6rMXWbCgJeIor7pIXUdk6do90IVoYa5qSa93h665wkWgHuPnG w==; X-CSE-ConnectionGUID: wLB16NDXQHCShp62k8410Q== X-CSE-MsgGUID: Bce6eUlVR8CILliYCbVkcw== X-IronPort-AV: E=McAfee;i="6800,10657,11733"; a="74925805" X-IronPort-AV: E=Sophos;i="6.23,127,1770624000"; d="scan'208";a="74925805" X-CSE-ConnectionGUID: sEtlEFF8T4yTnDIJK8sh8A== X-CSE-MsgGUID: 3HHZG44bQYuARUvykB1X3A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.23,127,1770624000"; d="scan'208";a="218205246" From: Junjie Cao To: qemu-devel@nongnu.org Cc: peterx@redhat.com, farosas@suse.de, berrange@redhat.com, junjie.cao@intel.com Subject: [PATCH v2 3/3] tests/unit: add pread_all and preadv_all tests for io channel file Date: Wed, 18 Mar 2026 22:01:13 +0800 Message-ID: <20260318140113.434-4-junjie.cao@intel.com> X-Mailer: git-send-email 2.53.0.windows.2 In-Reply-To: <20260318140113.434-1-junjie.cao@intel.com> References: <20260318140113.434-1-junjie.cao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=198.175.65.18; envelope-from=junjie.cao@intel.com; helo=mgamail.intel.com X-Spam_score_int: -26 X-Spam_score: -2.7 X-Spam_bar: -- X-Spam_report: (-2.7 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.819, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.903, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @intel.com) X-ZM-MESSAGEID: 1773842548816154100 Content-Type: text/plain; charset="utf-8" Add five targeted test cases for the new qio_channel_pread{v,}_all() and preadv_all_eof() helpers introduced earlier in this series: preadv_all: read from a non-zero offset into two iovecs pread_all: single-buffer wrapper smoke test preadv_all_eof/clean: clean EOF (offset =3D=3D file length) returns 0 preadv_all_eof/partial: partial data then EOF returns -1 preadv_all/eof-is-error: strict wrapper turns clean EOF into -1 All tests create a 16-byte file with known content and exercise the read helpers against it, verifying return values, error state, and (where applicable) buffer contents. The tests are guarded by CONFIG_PREADV since the underlying preadv support is optional. Signed-off-by: Junjie Cao --- tests/unit/test-io-channel-file.c | 137 ++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/tests/unit/test-io-channel-file.c b/tests/unit/test-io-channel= -file.c index 1977006ce9..97a2a5b5cb 100644 --- a/tests/unit/test-io-channel-file.c +++ b/tests/unit/test-io-channel-file.c @@ -102,6 +102,131 @@ static void test_io_channel_fd(void) } =20 =20 +#ifdef CONFIG_PREADV +#define TEST_PREAD_FILE "tests/test-io-channel-pread.txt" +#define TEST_PREAD_PATTERN "ABCDEFGHIJKLMNOP" /* 16 bytes */ +#define TEST_PREAD_LEN 16 + +static QIOChannel *create_pread_test_file(void) +{ + int fd; + + fd =3D open(TEST_PREAD_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0600); + g_assert_cmpint(fd, >=3D, 0); + g_assert_cmpint(write(fd, TEST_PREAD_PATTERN, TEST_PREAD_LEN), + =3D=3D, TEST_PREAD_LEN); + close(fd); + + return QIO_CHANNEL(qio_channel_file_new_path( + TEST_PREAD_FILE, O_RDONLY, 0, + &error_abort)); +} + +static void test_io_channel_preadv_all(void) +{ + QIOChannel *ioc; + char buf1[4], buf2[4]; + struct iovec iov[2] =3D { + { .iov_base =3D buf1, .iov_len =3D sizeof(buf1) }, + { .iov_base =3D buf2, .iov_len =3D sizeof(buf2) }, + }; + int ret; + + ioc =3D create_pread_test_file(); + + /* Read 8 bytes from offset 4 into two iovecs: "EFGH" + "IJKL" */ + ret =3D qio_channel_preadv_all(ioc, iov, 2, 4, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_cmpmem(buf1, 4, "EFGH", 4); + g_assert_cmpmem(buf2, 4, "IJKL", 4); + + unlink(TEST_PREAD_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_pread_all(void) +{ + QIOChannel *ioc; + char buf[8]; + int ret; + + ioc =3D create_pread_test_file(); + + /* Read 8 bytes from offset 8: "IJKLMNOP" */ + ret =3D qio_channel_pread_all(ioc, buf, sizeof(buf), 8, &error_abort); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_cmpmem(buf, 8, "IJKLMNOP", 8); + + unlink(TEST_PREAD_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_preadv_all_eof_clean(void) +{ + QIOChannel *ioc; + Error *err =3D NULL; + char buf[8]; + struct iovec iov =3D { .iov_base =3D buf, .iov_len =3D sizeof(buf) }; + int ret; + + ioc =3D create_pread_test_file(); + + /* Read from offset =3D=3D file length: clean EOF, expect 0 and no err= or */ + ret =3D qio_channel_preadv_all_eof(ioc, &iov, 1, TEST_PREAD_LEN, &err); + g_assert_cmpint(ret, =3D=3D, 0); + g_assert_null(err); + + unlink(TEST_PREAD_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_preadv_all_eof_partial(void) +{ + QIOChannel *ioc; + Error *err =3D NULL; + char buf[8]; + struct iovec iov =3D { .iov_base =3D buf, .iov_len =3D sizeof(buf) }; + int ret; + + ioc =3D create_pread_test_file(); + + /* + * Read 8 bytes from offset 12: only 4 bytes available before EOF. + * Expect -1 (partial data then EOF is an error) and err set. + */ + ret =3D qio_channel_preadv_all_eof(ioc, &iov, 1, 12, &err); + g_assert_cmpint(ret, =3D=3D, -1); + g_assert_nonnull(err); + error_free(err); + + unlink(TEST_PREAD_FILE); + object_unref(OBJECT(ioc)); +} + +static void test_io_channel_preadv_all_eof_is_error(void) +{ + QIOChannel *ioc; + Error *err =3D NULL; + char buf[8]; + struct iovec iov =3D { .iov_base =3D buf, .iov_len =3D sizeof(buf) }; + int ret; + + ioc =3D create_pread_test_file(); + + /* + * Clean EOF through the strict wrapper: should be translated to -1. + */ + ret =3D qio_channel_preadv_all(ioc, &iov, 1, TEST_PREAD_LEN, &err); + g_assert_cmpint(ret, =3D=3D, -1); + g_assert_nonnull(err); + error_free(err); + + unlink(TEST_PREAD_FILE); + object_unref(OBJECT(ioc)); +} +#endif /* CONFIG_PREADV */ + + #ifndef _WIN32 static void test_io_channel_pipe(bool async) { @@ -147,6 +272,18 @@ int main(int argc, char **argv) g_test_add_func("/io/channel/file", test_io_channel_file); g_test_add_func("/io/channel/file/rdwr", test_io_channel_file_rdwr); g_test_add_func("/io/channel/file/fd", test_io_channel_fd); +#ifdef CONFIG_PREADV + g_test_add_func("/io/channel/file/preadv-all", + test_io_channel_preadv_all); + g_test_add_func("/io/channel/file/pread-all", + test_io_channel_pread_all); + g_test_add_func("/io/channel/file/preadv-all-eof/clean", + test_io_channel_preadv_all_eof_clean); + g_test_add_func("/io/channel/file/preadv-all-eof/partial", + test_io_channel_preadv_all_eof_partial); + g_test_add_func("/io/channel/file/preadv-all/eof-is-error", + test_io_channel_preadv_all_eof_is_error); +#endif #ifndef _WIN32 g_test_add_func("/io/channel/pipe/sync", test_io_channel_pipe_sync); g_test_add_func("/io/channel/pipe/async", test_io_channel_pipe_async); --=20 2.43.0