From nobody Sun May 5 05:38:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487779684119216.86412575007023; Wed, 22 Feb 2017 08:08:04 -0800 (PST) Received: from localhost ([::1]:53332 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZSS-0003Gp-M7 for importer@patchew.org; Wed, 22 Feb 2017 11:08:00 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34821) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZMD-0005KO-Hw for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgZM9-0002ro-Je for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:33 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:34489 helo=mx0a-001b2d01.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cgZM9-0002rZ-DK for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:29 -0500 Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1MFwfYe062740 for ; Wed, 22 Feb 2017 11:01:27 -0500 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0b-001b2d01.pphosted.com with ESMTP id 28s9r44qge-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 22 Feb 2017 11:01:26 -0500 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 22 Feb 2017 16:01:25 -0000 Received: from d06dlp02.portsmouth.uk.ibm.com (9.149.20.14) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 22 Feb 2017 16:01:23 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp02.portsmouth.uk.ibm.com (Postfix) with ESMTP id 642702190066; Wed, 22 Feb 2017 16:00:24 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v1MG1MdK4915540; Wed, 22 Feb 2017 16:01:22 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2D8EDAE063; Wed, 22 Feb 2017 14:59:13 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 03206AE05D; Wed, 22 Feb 2017 14:59:13 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Wed, 22 Feb 2017 14:59:12 +0000 (GMT) From: Halil Pasic To: qemu-devel@nongnu.org, "Dr. David Alan Gilbert" Date: Wed, 22 Feb 2017 17:01:15 +0100 X-Mailer: git-send-email 2.8.4 In-Reply-To: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> References: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17022216-0028-0000-0000-000002B00C3F X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17022216-0029-0000-0000-00002278B243 Message-Id: <20170222160119.52771-2-pasic@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-22_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702220150 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.158.5 Subject: [Qemu-devel] [PATCH v2 1/5] migration/vmstate: renames in (load|save)_state X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amit Shah , Halil Pasic , Juan Quintela Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The vmstate_(load|save)_state start out with an a void *opaque pointing to some struct, and manipulate one or more elements of one field within that struct. First the field within the struct is pinpointed as opaque + offset, then if this is a pointer the pointer is dereferenced to obtain a pointer to the first element of the vmstate field. Pointers to further elements if any are calculated as first_element + i * element_size (where i is the zero based index of the element in question). Currently base_addr and addr is used as a variable name for the pointer to the first element and the pointer to the current element being processed. This is suboptimal because base_addr is somewhat counter-intuitive (because obtained as base + offset) and both base_addr and addr not very descriptive (that we have a pointer should be clear from the fact that it is declared as a pointer). Let make things easier to understand by renaming base_addr to first_elem and addr to curr_elem. This has the additional benefit of harmonizing with other names within the scope (n_elems, vmstate_n_elems). Signed-off-by: Halil Pasic Reviewed-by: Dr. David Alan Gilbert --- This patch roots in the difficulties I faced when trying to figure out the code in question. In the meanwhile I'm quite fine with the current names because I have it already figured out (after a couple of months not looking on this code however, who knows). Thus my main motivation is making things easier for the next new guy, but if the old guys say not worth it I can very well accept that too. --- migration/vmstate.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/migration/vmstate.c b/migration/vmstate.c index b4d8ae9..36efa80 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -116,21 +116,21 @@ int vmstate_load_state(QEMUFile *f, const VMStateDesc= ription *vmsd, field->field_exists(opaque, version_id)) || (!field->field_exists && field->version_id <=3D version_id)) { - void *base_addr =3D vmstate_base_addr(opaque, field, true); + void *first_elem =3D vmstate_base_addr(opaque, field, true); int i, n_elems =3D vmstate_n_elems(opaque, field); int size =3D vmstate_size(opaque, field); =20 for (i =3D 0; i < n_elems; i++) { - void *addr =3D base_addr + size * i; + void *curr_elem =3D first_elem + size * i; =20 if (field->flags & VMS_ARRAY_OF_POINTER) { - addr =3D *(void **)addr; + curr_elem =3D *(void **)curr_elem; } if (field->flags & VMS_STRUCT) { - ret =3D vmstate_load_state(f, field->vmsd, addr, + ret =3D vmstate_load_state(f, field->vmsd, curr_elem, field->vmsd->version_id); } else { - ret =3D field->info->get(f, addr, size, field); + ret =3D field->info->get(f, curr_elem, size, field); } if (ret >=3D 0) { ret =3D qemu_file_get_error(f); @@ -321,7 +321,7 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescr= iption *vmsd, while (field->name) { if (!field->field_exists || field->field_exists(opaque, vmsd->version_id)) { - void *base_addr =3D vmstate_base_addr(opaque, field, false); + void *first_elem =3D vmstate_base_addr(opaque, field, false); int i, n_elems =3D vmstate_n_elems(opaque, field); int size =3D vmstate_size(opaque, field); int64_t old_offset, written_bytes; @@ -329,18 +329,18 @@ void vmstate_save_state(QEMUFile *f, const VMStateDes= cription *vmsd, =20 trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems= ); for (i =3D 0; i < n_elems; i++) { - void *addr =3D base_addr + size * i; + void *curr_elem =3D first_elem + size * i; =20 vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems= ); old_offset =3D qemu_ftell_fast(f); - if (field->flags & VMS_ARRAY_OF_POINTER) { - addr =3D *(void **)addr; + assert(curr_elem); + curr_elem =3D *(void **)curr_elem; } if (field->flags & VMS_STRUCT) { - vmstate_save_state(f, field->vmsd, addr, vmdesc_loop); + vmstate_save_state(f, field->vmsd, curr_elem, vmdesc_l= oop); } else { - field->info->put(f, addr, size, field, vmdesc_loop); + field->info->put(f, curr_elem, size, field, vmdesc_loo= p); } =20 written_bytes =3D qemu_ftell_fast(f) - old_offset; --=20 2.8.4 From nobody Sun May 5 05:38:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 148777966998419.952768773007847; Wed, 22 Feb 2017 08:07:49 -0800 (PST) Received: from localhost ([::1]:53331 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZSE-000362-KN for importer@patchew.org; Wed, 22 Feb 2017 11:07:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34871) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZMG-0005La-GC for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:37 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgZMC-0002tQ-Jo for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:36 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:42867) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cgZMC-0002sh-AN for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:32 -0500 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1MFwirD059148 for ; Wed, 22 Feb 2017 11:01:30 -0500 Received: from e06smtp08.uk.ibm.com (e06smtp08.uk.ibm.com [195.75.94.104]) by mx0a-001b2d01.pphosted.com with ESMTP id 28rq70rthm-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 22 Feb 2017 11:01:29 -0500 Received: from localhost by e06smtp08.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 22 Feb 2017 16:01:27 -0000 Received: from d06dlp03.portsmouth.uk.ibm.com (9.149.20.15) by e06smtp08.uk.ibm.com (192.168.101.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 22 Feb 2017 16:01:24 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by d06dlp03.portsmouth.uk.ibm.com (Postfix) with ESMTP id 563B11B08067; Wed, 22 Feb 2017 16:04:23 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v1MG1NSU1966564; Wed, 22 Feb 2017 16:01:23 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7511CAE06A; Wed, 22 Feb 2017 14:59:14 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 418ECAE070; Wed, 22 Feb 2017 14:59:14 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Wed, 22 Feb 2017 14:59:14 +0000 (GMT) From: Halil Pasic To: qemu-devel@nongnu.org, "Dr. David Alan Gilbert" Date: Wed, 22 Feb 2017 17:01:16 +0100 X-Mailer: git-send-email 2.8.4 In-Reply-To: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> References: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17022216-0032-0000-0000-00000722E774 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17022216-0033-0000-0000-000023870A7A Message-Id: <20170222160119.52771-3-pasic@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-22_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702220150 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v2 2/5] migration/vmstate: split up vmstate_base_addr X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amit Shah , Halil Pasic , Juan Quintela Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently vmstate_base_addr does several things: it pinpoints the field within the struct, possibly allocates memory and possibly does the first pointer dereference. Obviously allocation is needed only for load. Let us split up the functionality in vmstate_base_addr and move the address manipulations (that is everything but the allocation logic) to load and save so it becomes more obvious what is actually going on. Like this all the address calculations (and the handling of the flags controlling these) is in one place and the sequence is more obvious. The newly introduced function vmstate_handle_alloc also fixes the allocation for the unused VMS_VBUFFER|VMS_MULTIPLY|VMS_ALLOC scenario and is substantially simpler than the original vmstate_base_addr. In load and save some asserts are added so it's easier to debug situations where we would end up with a null pointer dereference. Signed-off-by: Halil Pasic Reviewed-by: Dr. David Alan Gilbert --- migration/vmstate.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/migration/vmstate.c b/migration/vmstate.c index 36efa80..836a7a4 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -52,29 +52,15 @@ static int vmstate_size(void *opaque, VMStateField *fie= ld) return size; } =20 -static void *vmstate_base_addr(void *opaque, VMStateField *field, bool all= oc) +static void vmstate_handle_alloc(void *ptr, VMStateField *field, void *opa= que) { - void *base_addr =3D opaque + field->offset; - - if (field->flags & VMS_POINTER) { - if (alloc && (field->flags & VMS_ALLOC)) { - gsize size =3D 0; - if (field->flags & VMS_VBUFFER) { - size =3D vmstate_size(opaque, field); - } else { - int n_elems =3D vmstate_n_elems(opaque, field); - if (n_elems) { - size =3D n_elems * field->size; - } - } - if (size) { - *(void **)base_addr =3D g_malloc(size); - } + if (field->flags & VMS_POINTER && field->flags & VMS_ALLOC) { + gsize size =3D vmstate_size(opaque, field); + size *=3D vmstate_n_elems(opaque, field); + if (size) { + *(void **)ptr =3D g_malloc(size); } - base_addr =3D *(void **)base_addr; } - - return base_addr; } =20 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, @@ -116,10 +102,15 @@ int vmstate_load_state(QEMUFile *f, const VMStateDesc= ription *vmsd, field->field_exists(opaque, version_id)) || (!field->field_exists && field->version_id <=3D version_id)) { - void *first_elem =3D vmstate_base_addr(opaque, field, true); + void *first_elem =3D opaque + field->offset; int i, n_elems =3D vmstate_n_elems(opaque, field); int size =3D vmstate_size(opaque, field); =20 + vmstate_handle_alloc(first_elem, field, opaque); + if (field->flags & VMS_POINTER) { + first_elem =3D *(void **)first_elem; + assert(first_elem || !n_elems); + } for (i =3D 0; i < n_elems; i++) { void *curr_elem =3D first_elem + size * i; =20 @@ -321,13 +312,17 @@ void vmstate_save_state(QEMUFile *f, const VMStateDes= cription *vmsd, while (field->name) { if (!field->field_exists || field->field_exists(opaque, vmsd->version_id)) { - void *first_elem =3D vmstate_base_addr(opaque, field, false); + void *first_elem =3D opaque + field->offset; int i, n_elems =3D vmstate_n_elems(opaque, field); int size =3D vmstate_size(opaque, field); int64_t old_offset, written_bytes; QJSON *vmdesc_loop =3D vmdesc; =20 trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems= ); + if (field->flags & VMS_POINTER) { + first_elem =3D *(void **)first_elem; + assert(first_elem || !n_elems); + } for (i =3D 0; i < n_elems; i++) { void *curr_elem =3D first_elem + size * i; =20 --=20 2.8.4 From nobody Sun May 5 05:38:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487779433929318.28217795710987; Wed, 22 Feb 2017 08:03:53 -0800 (PST) Received: from localhost ([::1]:53311 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZOQ-00075R-Dv for importer@patchew.org; Wed, 22 Feb 2017 11:03:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34881) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZMH-0005MH-Ej for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgZMD-0002tr-DV for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:37 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:59366) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cgZMD-0002tG-4g for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:33 -0500 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1MFwdWu033935 for ; Wed, 22 Feb 2017 11:01:32 -0500 Received: from e06smtp08.uk.ibm.com (e06smtp08.uk.ibm.com [195.75.94.104]) by mx0a-001b2d01.pphosted.com with ESMTP id 28scvp39qs-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 22 Feb 2017 11:01:31 -0500 Received: from localhost by e06smtp08.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 22 Feb 2017 16:01:28 -0000 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp08.uk.ibm.com (192.168.101.138) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 22 Feb 2017 16:01:25 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id B1B9F17D805A; Wed, 22 Feb 2017 16:04:35 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v1MG1PMu2490686; Wed, 22 Feb 2017 16:01:25 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C82F8AE063; Wed, 22 Feb 2017 14:59:15 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 89618AE064; Wed, 22 Feb 2017 14:59:15 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Wed, 22 Feb 2017 14:59:15 +0000 (GMT) From: Halil Pasic To: qemu-devel@nongnu.org, "Dr. David Alan Gilbert" Date: Wed, 22 Feb 2017 17:01:17 +0100 X-Mailer: git-send-email 2.8.4 In-Reply-To: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> References: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17022216-0032-0000-0000-00000722E778 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17022216-0033-0000-0000-000023870A81 Message-Id: <20170222160119.52771-4-pasic@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-22_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702220150 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v2 3/5] migration/vmstate: fix array of ptr with nullptrs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amit Shah , Halil Pasic , Juan Quintela Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Make VMS_ARRAY_OF_POINTER cope with null pointers. Previously the reward for trying to migrate an array with some null pointers in it was an illegal memory access, that is a swift and painless death of the process. Let's make vmstate cope with this scenario. The general approach is, when we encounter a null pointer (element), instead of following the pointer to save/load the data behind it, we save/load a placeholder. This way we can detect if we expected a null pointer at the load side but not null data was saved instead. Signed-off-by: Halil Pasic Reviewed-by: Guenther Hutzl Reviewed-by: Dr. David Alan Gilbert --- We will need this to load/save some on demand created state in the (s390x) channel subsystem (see ChannelSubSys.css in hw/s390x/css.c for an example). --- include/migration/vmstate.h | 4 ++++ migration/vmstate.c | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 63e7b02..f2dbf84 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -253,6 +253,10 @@ extern const VMStateInfo vmstate_info_uint16; extern const VMStateInfo vmstate_info_uint32; extern const VMStateInfo vmstate_info_uint64; =20 +/** Put this in the stream when migrating a null pointer.*/ +#define VMS_NULLPTR_MARKER (0x30U) /* '0' */ +extern const VMStateInfo vmstate_info_nullptr; + extern const VMStateInfo vmstate_info_float64; extern const VMStateInfo vmstate_info_cpudouble; =20 diff --git a/migration/vmstate.c b/migration/vmstate.c index 836a7a4..78b3cd4 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -117,7 +117,11 @@ int vmstate_load_state(QEMUFile *f, const VMStateDescr= iption *vmsd, if (field->flags & VMS_ARRAY_OF_POINTER) { curr_elem =3D *(void **)curr_elem; } - if (field->flags & VMS_STRUCT) { + if (!curr_elem) { + /* if null pointer check placeholder and do not follow= */ + assert(field->flags & VMS_ARRAY_OF_POINTER); + ret =3D vmstate_info_nullptr.get(f, curr_elem, size, N= ULL); + } else if (field->flags & VMS_STRUCT) { ret =3D vmstate_load_state(f, field->vmsd, curr_elem, field->vmsd->version_id); } else { @@ -332,7 +336,11 @@ void vmstate_save_state(QEMUFile *f, const VMStateDesc= ription *vmsd, assert(curr_elem); curr_elem =3D *(void **)curr_elem; } - if (field->flags & VMS_STRUCT) { + if (!curr_elem) { + /* if null pointer write placeholder and do not follow= */ + assert(field->flags & VMS_ARRAY_OF_POINTER); + vmstate_info_nullptr.put(f, curr_elem, size, NULL, NUL= L); + } else if (field->flags & VMS_STRUCT) { vmstate_save_state(f, field->vmsd, curr_elem, vmdesc_l= oop); } else { field->info->put(f, curr_elem, size, field, vmdesc_loo= p); @@ -747,6 +755,34 @@ const VMStateInfo vmstate_info_uint64 =3D { .put =3D put_uint64, }; =20 +static int get_nullptr(QEMUFile *f, void *pv, size_t size, VMStateField *f= ield) + +{ + if (qemu_get_byte(f) =3D=3D VMS_NULLPTR_MARKER) { + return 0; + } + error_report("vmstate: get_nullptr expected VMS_NULLPTR_MARKER"); + return -EINVAL; +} + +static int put_nullptr(QEMUFile *f, void *pv, size_t size, + VMStateField *field, QJSON *vmdesc) + +{ + if (pv =3D=3D NULL) { + qemu_put_byte(f, VMS_NULLPTR_MARKER); + return 0; + } + error_report("vmstate: put_nullptr must be called with pv =3D=3D NULL"= ); + return -EINVAL; +} + +const VMStateInfo vmstate_info_nullptr =3D { + .name =3D "uint64", + .get =3D get_nullptr, + .put =3D put_nullptr, +}; + /* 64 bit unsigned int. See that the received value is the same than the o= ne in the field */ =20 --=20 2.8.4 From nobody Sun May 5 05:38:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487779671067231.9317731056817; Wed, 22 Feb 2017 08:07:51 -0800 (PST) Received: from localhost ([::1]:53330 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZSC-00035R-Kh for importer@patchew.org; Wed, 22 Feb 2017 11:07:44 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZMH-0005Mg-OC for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:38 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgZMD-0002uE-Se for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:37 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:47407) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cgZMD-0002tY-JE for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:33 -0500 Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1MFweb4115932 for ; Wed, 22 Feb 2017 11:01:32 -0500 Received: from e06smtp15.uk.ibm.com (e06smtp15.uk.ibm.com [195.75.94.111]) by mx0a-001b2d01.pphosted.com with ESMTP id 28sdfp11ex-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 22 Feb 2017 11:01:31 -0500 Received: from localhost by e06smtp15.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 22 Feb 2017 16:01:28 -0000 Received: from d06dlp02.portsmouth.uk.ibm.com (9.149.20.14) by e06smtp15.uk.ibm.com (192.168.101.145) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 22 Feb 2017 16:01:27 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (d06relay09.portsmouth.uk.ibm.com [9.149.109.194]) by d06dlp02.portsmouth.uk.ibm.com (Postfix) with ESMTP id AECCA2190023; Wed, 22 Feb 2017 16:00:28 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v1MG1QRW14877070; Wed, 22 Feb 2017 16:01:26 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 15071AE058; Wed, 22 Feb 2017 14:59:17 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D1B4AAE04D; Wed, 22 Feb 2017 14:59:16 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Wed, 22 Feb 2017 14:59:16 +0000 (GMT) From: Halil Pasic To: qemu-devel@nongnu.org, "Dr. David Alan Gilbert" Date: Wed, 22 Feb 2017 17:01:18 +0100 X-Mailer: git-send-email 2.8.4 In-Reply-To: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> References: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17022216-0020-0000-0000-0000030D2FBB X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17022216-0021-0000-0000-0000407EA08E Message-Id: <20170222160119.52771-5-pasic@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-22_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702220150 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v2 4/5] tests/test-vmstate.c: test array of ptr with null X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amit Shah , Halil Pasic , Juan Quintela Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add test for VMSTATE_ARRAY_OF_POINTER_TO_STRUCT with an array containing some null pointer. Signed-off-by: Halil Pasic Reviewed-by: Dr. David Alan Gilbert --- tests/test-vmstate.c | 74 ++++++++++++++++++++++++++++++++++++++++--------= ---- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index 39f338a..eafef94 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -476,6 +476,8 @@ const VMStateDescription vmsd_tst =3D { } }; =20 +/* test array migration */ + #define AR_SIZE 4 =20 typedef struct { @@ -492,20 +494,22 @@ const VMStateDescription vmsd_arps =3D { VMSTATE_END_OF_LIST() } }; + +static uint8_t wire_arr_ptr_no0[] =3D { + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x02, + 0x00, 0x00, 0x00, 0x03, + QEMU_VM_EOF +}; + static void test_arr_ptr_str_no0_save(void) { TestStructTriv ar[AR_SIZE] =3D {{.i =3D 0}, {.i =3D 1}, {.i =3D 2}, {.= i =3D 3} }; TestArrayOfPtrToStuct sample =3D {.ar =3D {&ar[0], &ar[1], &ar[2], &ar= [3]} }; - uint8_t wire_sample[] =3D { - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x01, - 0x00, 0x00, 0x00, 0x02, - 0x00, 0x00, 0x00, 0x03, - QEMU_VM_EOF - }; =20 save_vmstate(&vmsd_arps, &sample); - compare_vmstate(wire_sample, sizeof(wire_sample)); + compare_vmstate(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)); } =20 static void test_arr_ptr_str_no0_load(void) @@ -514,21 +518,54 @@ static void test_arr_ptr_str_no0_load(void) TestStructTriv ar[AR_SIZE] =3D {}; TestArrayOfPtrToStuct obj =3D {.ar =3D {&ar[0], &ar[1], &ar[2], &ar[3]= } }; int idx; - uint8_t wire_sample[] =3D { - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x01, - 0x00, 0x00, 0x00, 0x02, - 0x00, 0x00, 0x00, 0x03, - QEMU_VM_EOF - }; =20 - save_buffer(wire_sample, sizeof(wire_sample)); + save_buffer(wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0)); + SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1, + wire_arr_ptr_no0, sizeof(wire_arr_ptr_no0))); + for (idx =3D 0; idx < AR_SIZE; ++idx) { + /* compare the target array ar with the ground truth array ar_gt */ + g_assert_cmpint(ar_gt[idx].i, =3D=3D, ar[idx].i); + } +} + +static uint8_t wire_arr_ptr_0[] =3D { + 0x00, 0x00, 0x00, 0x00, + VMS_NULLPTR_MARKER, + 0x00, 0x00, 0x00, 0x02, + 0x00, 0x00, 0x00, 0x03, + QEMU_VM_EOF +}; + +static void test_arr_ptr_str_0_save(void) +{ + TestStructTriv ar[AR_SIZE] =3D {{.i =3D 0}, {.i =3D 1}, {.i =3D 2}, {.= i =3D 3} }; + TestArrayOfPtrToStuct sample =3D {.ar =3D {&ar[0], NULL, &ar[2], &ar[3= ]} }; + + save_vmstate(&vmsd_arps, &sample); + compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); +} + +static void test_arr_ptr_str_0_load(void) +{ + TestStructTriv ar_gt[AR_SIZE] =3D {{.i =3D 0}, {.i =3D 0}, {.i =3D 2},= {.i =3D 3} }; + TestStructTriv ar[AR_SIZE] =3D {}; + TestArrayOfPtrToStuct obj =3D {.ar =3D {&ar[0], NULL, &ar[2], &ar[3]} = }; + int idx; + + save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); SUCCESS(load_vmstate_one(&vmsd_arps, &obj, 1, - wire_sample, sizeof(wire_sample))); + wire_arr_ptr_0, sizeof(wire_arr_ptr_0))); for (idx =3D 0; idx < AR_SIZE; ++idx) { /* compare the target array ar with the ground truth array ar_gt */ g_assert_cmpint(ar_gt[idx].i, =3D=3D, ar[idx].i); } + for (idx =3D 0; idx < AR_SIZE; ++idx) { + if (idx =3D=3D 1) { + g_assert_cmpint((uint64_t)(obj.ar[idx]), =3D=3D, 0); + } else { + g_assert_cmpint((uint64_t)(obj.ar[idx]), !=3D, 0); + } + } } =20 /* test QTAILQ migration */ @@ -781,6 +818,9 @@ int main(int argc, char **argv) test_arr_ptr_str_no0_save); g_test_add_func("/vmstate/array/ptr/str/no0/load", test_arr_ptr_str_no0_load); + g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_sa= ve); + g_test_add_func("/vmstate/array/ptr/str/0/load", + test_arr_ptr_str_0_load); g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q); g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q); g_test_add_func("/vmstate/tmp_struct", test_tmp_struct); --=20 2.8.4 From nobody Sun May 5 05:38:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1487779870653492.8248524416083; Wed, 22 Feb 2017 08:11:10 -0800 (PST) Received: from localhost ([::1]:53351 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZVT-0005eX-3Z for importer@patchew.org; Wed, 22 Feb 2017 11:11:07 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34914) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cgZMJ-0005OC-O4 for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cgZMF-0002vD-RJ for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:39 -0500 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:54721) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cgZMF-0002uq-Hm for qemu-devel@nongnu.org; Wed, 22 Feb 2017 11:01:35 -0500 Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v1MFwmpN114792 for ; Wed, 22 Feb 2017 11:01:34 -0500 Received: from e06smtp14.uk.ibm.com (e06smtp14.uk.ibm.com [195.75.94.110]) by mx0a-001b2d01.pphosted.com with ESMTP id 28rt1kyyq5-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 22 Feb 2017 11:01:33 -0500 Received: from localhost by e06smtp14.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 22 Feb 2017 16:01:31 -0000 Received: from d06dlp01.portsmouth.uk.ibm.com (9.149.20.13) by e06smtp14.uk.ibm.com (192.168.101.144) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 22 Feb 2017 16:01:28 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp01.portsmouth.uk.ibm.com (Postfix) with ESMTP id 331E117D805D; Wed, 22 Feb 2017 16:04:38 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v1MG1RhA20447638; Wed, 22 Feb 2017 16:01:27 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 42046AE057; Wed, 22 Feb 2017 14:59:18 +0000 (GMT) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1D19BAE056; Wed, 22 Feb 2017 14:59:18 +0000 (GMT) Received: from tuxmaker.boeblingen.de.ibm.com (unknown [9.152.85.9]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Wed, 22 Feb 2017 14:59:18 +0000 (GMT) From: Halil Pasic To: qemu-devel@nongnu.org, "Dr. David Alan Gilbert" Date: Wed, 22 Feb 2017 17:01:19 +0100 X-Mailer: git-send-email 2.8.4 In-Reply-To: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> References: <20170222160119.52771-1-pasic@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 X-Content-Scanned: Fidelis XPS MAILER x-cbid: 17022216-0016-0000-0000-0000044418E5 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17022216-0017-0000-0000-0000268ADA70 Message-Id: <20170222160119.52771-6-pasic@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-02-22_10:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1612050000 definitions=main-1702220150 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] [fuzzy] X-Received-From: 148.163.156.1 Subject: [Qemu-devel] [PATCH v2 5/5] tests/test-vmstate.c: test array of ptr to primitive X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amit Shah , Halil Pasic , Juan Quintela Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's have a test for ptr arrays to some primitive type with some not-null and null ptrs intermixed. Signed-off-by: Halil Pasic Reviewed-by: Dr. David Alan Gilbert --- Mainly for the sake of completeness and also to demonstrate that it works since in the previous version I wrongly stated it does not. If guys think we do not need this, I'm happy with just dropping it. --- tests/test-vmstate.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index eafef94..3abdc1a 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -568,6 +568,50 @@ static void test_arr_ptr_str_0_load(void) } } =20 +typedef struct TestArrayOfPtrToInt { + int32_t *ar[AR_SIZE]; +} TestArrayOfPtrToInt; + +const VMStateDescription vmsd_arpp =3D { + .name =3D "test/arps", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_ARRAY_OF_POINTER(ar, TestArrayOfPtrToInt, + AR_SIZE, 0, vmstate_info_int32, int32_t*), + VMSTATE_END_OF_LIST() + } +}; + +static void test_arr_ptr_prim_0_save(void) +{ + int32_t ar[AR_SIZE] =3D {0 , 1, 2, 3}; + TestArrayOfPtrToInt sample =3D {.ar =3D {&ar[0], NULL, &ar[2], &ar[3]= } }; + + save_vmstate(&vmsd_arpp, &sample); + compare_vmstate(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); +} + +static void test_arr_ptr_prim_0_load(void) +{ + int32_t ar_gt[AR_SIZE] =3D {0, 1, 2, 3}; + int32_t ar[AR_SIZE] =3D {3 , 42, 1, 0}; + TestArrayOfPtrToInt obj =3D {.ar =3D {&ar[0], NULL, &ar[2], &ar[3]} }; + int idx; + + save_buffer(wire_arr_ptr_0, sizeof(wire_arr_ptr_0)); + SUCCESS(load_vmstate_one(&vmsd_arpp, &obj, 1, + wire_arr_ptr_0, sizeof(wire_arr_ptr_0))); + for (idx =3D 0; idx < AR_SIZE; ++idx) { + /* compare the target array ar with the ground truth array ar_gt */ + if (idx =3D=3D 1) { + g_assert_cmpint(42, =3D=3D, ar[idx]); + } else { + g_assert_cmpint(ar_gt[idx], =3D=3D, ar[idx]); + } + } +} + /* test QTAILQ migration */ typedef struct TestQtailqElement TestQtailqElement; =20 @@ -821,6 +865,10 @@ int main(int argc, char **argv) g_test_add_func("/vmstate/array/ptr/str/0/save", test_arr_ptr_str_0_sa= ve); g_test_add_func("/vmstate/array/ptr/str/0/load", test_arr_ptr_str_0_load); + g_test_add_func("/vmstate/array/ptr/prim/0/save", + test_arr_ptr_prim_0_save); + g_test_add_func("/vmstate/array/ptr/prim/0/load", + test_arr_ptr_prim_0_load); g_test_add_func("/vmstate/qtailq/save/saveq", test_save_q); g_test_add_func("/vmstate/qtailq/load/loadq", test_load_q); g_test_add_func("/vmstate/tmp_struct", test_tmp_struct); --=20 2.8.4