From patchwork Mon Aug 8 12:57:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12938764 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 070CAC00140 for ; Mon, 8 Aug 2022 12:58:48 +0000 (UTC) Received: from localhost ([::1]:59112 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oL2LH-0002EN-UY for qemu-devel@archiver.kernel.org; Mon, 08 Aug 2022 08:58:47 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52480) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jo-0007kK-H8 for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:16 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:41736) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jl-0001fB-Hm for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659963432; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zjIkCOrgZJsNDwC0p4zt66uvkq6oMqFfMYuPDOdWkN8=; b=TTce/J3+aSz3orY6rWWA8BaDce752jFqRqsetWgaxaJPYDk+rXEPzBXUOtCwF6s2TqZFHj SYPL7BIKTe/x48X1cZqHF9ckxJ0IfItkI6/UvNg+e+QiCy+mNMEVK/+jATlBFy6bz/iisg BdeC6mpeV70XISiWVeJD/1UP9hjT7xM= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-571-mTA3HluPOsuKDCRSpYgHXg-1; Mon, 08 Aug 2022 08:57:11 -0400 X-MC-Unique: mTA3HluPOsuKDCRSpYgHXg-1 Received: by mail-ed1-f71.google.com with SMTP id l19-20020a056402255300b0043df64f9a0fso5629503edb.16 for ; Mon, 08 Aug 2022 05:57:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=zjIkCOrgZJsNDwC0p4zt66uvkq6oMqFfMYuPDOdWkN8=; b=wJ0hw64B70A7vsHd9DXPoQ45sOhh0WVuUyUJQBdlG2Pd8Ut49sLSFZXNxRyg4hXlcB WbpXIkgag/4pe65g90+GQi4Ldx5Yzi3uSA2UrgbyvxWkcqDK0BozVfWTp7lEVmgIWlBz udURmyCI5Z7r0rIrAaeWDAismzASXxLXrS1Fl4zrwVVHEi4jTyFv4NjHB8b7Y4xy9nNF wzRbakeWbIP2k+UDsOwj8D0EzPwEgSLxhWa6cfaQ6xSxM4pJIqUkJXkJ1CDKXxQxmO1L uucp/WHHkbps16PDArTiPrsk29ABth3R+lDqrsyzTO870A2Kq76czcukR3IDSEb4IULp s13Q== X-Gm-Message-State: ACgBeo10IVK2NmluDCUF8Nsz9IOs6LHlg8W0eXbxHZMjkatLpUPdIF3b zXcrnBuo5QkLvn9D8ws0A384jnQ88XjBPoCpDx7fjqYp0ygLqixgJJ5NnH6t6tjdJpaJ4hLsyhp GYjoSpdeqlE9TI4uQkwlJt9flF2juIJSijqtFmDRwQK9va9BWcIRhXXadY4zq3G8ia1E= X-Received: by 2002:a05:6402:241e:b0:440:2c03:cdc8 with SMTP id t30-20020a056402241e00b004402c03cdc8mr12858379eda.237.1659963430128; Mon, 08 Aug 2022 05:57:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR4B5xYqF+41di3pxL99/8vW53gx6GaXfhxURrKcXs7rqNO3EXNTr9qWtzQn4qsMxkeyWRmEDg== X-Received: by 2002:a05:6402:241e:b0:440:2c03:cdc8 with SMTP id t30-20020a056402241e00b004402c03cdc8mr12858360eda.237.1659963429757; Mon, 08 Aug 2022 05:57:09 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id iy20-20020a170907819400b00730cc173c6asm4924378ejc.43.2022.08.08.05.57.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 05:57:09 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Mark Cave-Ayland Subject: [PULL 1/5] scsi-disk: fix overflow when block size is not a multiple of BDRV_SECTOR_SIZE Date: Mon, 8 Aug 2022 14:57:02 +0200 Message-Id: <20220808125706.60511-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220808125706.60511-1-pbonzini@redhat.com> References: <20220808125706.60511-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Mark Cave-Ayland In scsi_disk_emulate_write_same() the number of host sectors to transfer is calculated as (s->qdev.blocksize / BDRV_SECTOR_SIZE) which is then used to copy data in block size chunks to the iov buffer. Since the loop copying the data to the iov buffer uses a fixed increment of s->qdev.blocksize then using a block size that isn't a multiple of BDRV_SECTOR_SIZE introduces a rounding error in the iov buffer size calculation such that the iov buffer copy overflows the space allocated. Update the iov buffer copy for() loop so that it will use the smallest of either the current block size or the remaining transfer count to prevent the overflow. Signed-off-by: Mark Cave-Ayland Message-Id: <20220730122656.253448-2-mark.cave-ayland@ilande.co.uk> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index f5cdb9ad4b..3027ac3b1e 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -1849,7 +1849,7 @@ static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf) uint32_t nb_sectors = scsi_data_cdb_xfer(r->req.cmd.buf); WriteSameCBData *data; uint8_t *buf; - int i; + int i, l; /* Fail if PBDATA=1 or LBDATA=1 or ANCHOR=1. */ if (nb_sectors == 0 || (req->cmd.buf[1] & 0x16)) { @@ -1891,8 +1891,9 @@ static void scsi_disk_emulate_write_same(SCSIDiskReq *r, uint8_t *inbuf) data->iov.iov_len); qemu_iovec_init_external(&data->qiov, &data->iov, 1); - for (i = 0; i < data->iov.iov_len; i += s->qdev.blocksize) { - memcpy(&buf[i], inbuf, s->qdev.blocksize); + for (i = 0; i < data->iov.iov_len; i += l) { + l = MIN(s->qdev.blocksize, data->iov.iov_len - i); + memcpy(&buf[i], inbuf, l); } scsi_req_ref(&r->req); From patchwork Mon Aug 8 12:57:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12938765 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 136A9C00140 for ; Mon, 8 Aug 2022 12:58:53 +0000 (UTC) Received: from localhost ([::1]:59344 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oL2LM-0002Nh-6v for qemu-devel@archiver.kernel.org; Mon, 08 Aug 2022 08:58:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52498) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jq-0007mt-I5 for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:46543) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jn-0001fP-62 for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659963434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QOBfz/cMVhH0j5sprwl3Y1QI29rhhQHeizSuzql1dcY=; b=Vsrm8LGSUcOmr2pAHMyyvfld8tvZTYrfcRtVzDfe2Qg7g3X83ISoVl5B0Sv7F1X/4ux1MR v5D+wiND+iipEgiTWRpbyeH9k8eHA7rXJdBuXQizSrOaZtPYX7Z80ZxN7BKSmpJPY0w4CK wuoZOCDWRoKOldBueviJlibT5JHgLxo= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-663-Ly_mWlyHMjWfyPVqn99Wxw-1; Mon, 08 Aug 2022 08:57:13 -0400 X-MC-Unique: Ly_mWlyHMjWfyPVqn99Wxw-1 Received: by mail-ej1-f70.google.com with SMTP id sa33-20020a1709076d2100b0073101bdd612so1688598ejc.14 for ; Mon, 08 Aug 2022 05:57:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=QOBfz/cMVhH0j5sprwl3Y1QI29rhhQHeizSuzql1dcY=; b=wjL1mO31ZhhyUdjHBaA8Hnc64StO7kAMIVj4ElcrVk8v8DVTMsPUXRzeg63K1LdoQn 9zkKZEENG+oHqMAmQZR8OQhdZ6UAAF6/5P1y1WX1skyfFw/5XTCrBsF5UDjYxjMYaHU8 YkpZ8rD2e/EdSJLNaLsxUc9fZXrhKAJ2W9eGuaehKOo7QPwL2XqLTbYHHLa5pEijEdEM oblvnrhT9uCOWCWZ9S1PxV+mzRRydboGtjmbNjeYnCMA0RskGedEJCYGdo4PXmMI7wWY hgtUWcN5M4h2fxfmjfH9l91tNdIKCrBgKKTDiAcau8bpoEwM3ApXRlRZJS7g9QOjm2Qo YYgw== X-Gm-Message-State: ACgBeo2X6urjQQ3yUoQSYuYUhbSzdMBMsDIKFN7KEQyJRFpgxJsJKt30 dT3ey9meeXWUBFjJ8Lj6EPxPLPmx3b287WmSHYDNmwRVlxKRK2ZXlhGHwu5KEtNSK64G9Syuq9N OmIWW3TnKZNVeuxvDEFZ4em0hNf8U6PlX5b0a6rIJWHHeyETVdBzhxdO69qU9jrxO6oE= X-Received: by 2002:a17:907:97cd:b0:731:5d0:4434 with SMTP id js13-20020a17090797cd00b0073105d04434mr9360763ejc.603.1659963431787; Mon, 08 Aug 2022 05:57:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR4/lSvnBl+RSeWTYz3dynJCBIKrtBZ2H6MCL3Uhgd4yGDiKjj93ak02165zqyTttDhrX//yXw== X-Received: by 2002:a17:907:97cd:b0:731:5d0:4434 with SMTP id js13-20020a17090797cd00b0073105d04434mr9360749ejc.603.1659963431433; Mon, 08 Aug 2022 05:57:11 -0700 (PDT) Received: from goa-sendmail ([93.56.169.144]) by smtp.gmail.com with ESMTPSA id en19-20020a056402529300b0043a87e6196esm4518820edb.6.2022.08.08.05.57.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 05:57:10 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Mark Cave-Ayland Subject: [PULL 2/5] scsi-disk: ensure block size is non-zero and changes limited to bits 8-15 Date: Mon, 8 Aug 2022 14:57:03 +0200 Message-Id: <20220808125706.60511-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220808125706.60511-1-pbonzini@redhat.com> References: <20220808125706.60511-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Mark Cave-Ayland The existing code assumes that the block size can be generated from p[1] << 8 in multiple places which ignores the top and bottom 8 bits. If the block size is allowed to be set to an arbitrary value then this causes a mismatch between the value written by the guest in the block descriptor and the value subsequently read back using READ CAPACITY causing the guest to generate requests that can crash QEMU. For now restrict block size changes to bits 8-15 and also ignore requests to set the block size to 0 which causes the SCSI emulation to crash in at least one place with a divide by zero error. Fixes: 356c4c441e ("scsi-disk: allow MODE SELECT block descriptor to set the block size") Closes: https://gitlab.com/qemu-project/qemu/-/issues/1112 Signed-off-by: Mark Cave-Ayland Message-Id: <20220730122656.253448-3-mark.cave-ayland@ilande.co.uk> Signed-off-by: Paolo Bonzini --- hw/scsi/scsi-disk.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 3027ac3b1e..efee6739f9 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -1591,7 +1591,7 @@ static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf) int cmd = r->req.cmd.buf[0]; int len = r->req.cmd.xfer; int hdr_len = (cmd == MODE_SELECT ? 4 : 8); - int bd_len; + int bd_len, bs; int pass; if ((r->req.cmd.buf[1] & 0x11) != 0x10) { @@ -1617,9 +1617,19 @@ static void scsi_disk_emulate_mode_select(SCSIDiskReq *r, uint8_t *inbuf) } /* Allow changing the block size */ - if (bd_len && p[6] != (s->qdev.blocksize >> 8)) { - s->qdev.blocksize = p[6] << 8; - trace_scsi_disk_mode_select_set_blocksize(s->qdev.blocksize); + if (bd_len) { + bs = p[5] << 16 | p[6] << 8 | p[7]; + + /* + * Since the existing code only checks/updates bits 8-15 of the block + * size, restrict ourselves to the same requirement for now to ensure + * that a block size set by a block descriptor and then read back by + * a subsequent SCSI command will be the same + */ + if (bs && !(bs & ~0xff00) && bs != s->qdev.blocksize) { + s->qdev.blocksize = bs; + trace_scsi_disk_mode_select_set_blocksize(s->qdev.blocksize); + } } len -= bd_len; From patchwork Mon Aug 8 12:57:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12938766 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79568C25B0C for ; Mon, 8 Aug 2022 12:58:53 +0000 (UTC) Received: from localhost ([::1]:59448 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oL2LM-0002Rv-Bb for qemu-devel@archiver.kernel.org; Mon, 08 Aug 2022 08:58:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52524) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jq-0007ou-ER for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:27064) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jo-0001fv-Pu for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659963435; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=THIwJaCekNLy/BZoWOKoQ+/b0DlzgUSt+fJQ+kqHo50=; b=XDkPjvd4uu6hdd9h5Gv9yfHqBfN2W+JHf9N+CFF/XfA41I34Zt5PZtmmxBAYffWZqYpfoY mbBcFSMfWZ1FVc5TJYD4xnpEztWD7tsQbgexFMylURvMP9JOniJHELektuHQk24NTQE8hF x4+KtFGVwJZa3nt7NVnA8sWwcHrPitM= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-647-_knzFyw8OmadiXQKp4-4gA-1; Mon, 08 Aug 2022 08:57:14 -0400 X-MC-Unique: _knzFyw8OmadiXQKp4-4gA-1 Received: by mail-ed1-f69.google.com with SMTP id m18-20020a056402511200b0043d601a8035so5710500edd.20 for ; Mon, 08 Aug 2022 05:57:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=THIwJaCekNLy/BZoWOKoQ+/b0DlzgUSt+fJQ+kqHo50=; b=gM0Af/5+Q9XpjhimjnjHzgr7/8u4fLw07WyIjfFSkInOJUWVhvauvpn75Qc3guz6uv wAW2I6SXgh0gHn3vs2ownqZ79xPMsraWXvPt+XWr2WlvFOCgTrNLRl0ZYBvErMkIlq81 CGKmDYyF2v+UR44E4/G8YhQDZgMkYlQq5DBW6G2W5Fi6IxJluGlnxZ5AQ86FgjiKJzcw pwgpHOwOUhfkTQGAg/XY8RnOV52NKk0jaLrGQb+y2HfWaWeYE0DWOWMmzvmK6dORUEa0 McYGCPj6KvHX+CRC9vqXczbl0756mZv5eVjBjuDas9kXoa/4ntP888+Zrtc6vAzW7a4N PMlQ== X-Gm-Message-State: ACgBeo2EPVwMJeld+9lyfi84H+W2skSpNiSLOLZAVPeQTzXzZEtC8Q4H dckQI1fDvM791O0F6ry1QMbydpnaNVJUBPEZ7162PNncdCq/cTGZRB6AC2z889EZIAyU0J9FXWe jyZBG85hNt/P/aBXH0AWGMHcYXdAzF5e+sq6g6ZWwHG4CCHXxafVbrp99HxbxF9011q4= X-Received: by 2002:a17:907:3111:b0:730:6535:b3fb with SMTP id wl17-20020a170907311100b007306535b3fbmr13121930ejb.490.1659963433341; Mon, 08 Aug 2022 05:57:13 -0700 (PDT) X-Google-Smtp-Source: AA6agR4lu3GtP9fccDjRxASg/M+c5GgAOmtG7yFd/QGgrlIj9inJ4/i5aDb6JGNv8sQ26p4V4ApJLw== X-Received: by 2002:a17:907:3111:b0:730:6535:b3fb with SMTP id wl17-20020a170907311100b007306535b3fbmr13121914ejb.490.1659963433109; Mon, 08 Aug 2022 05:57:13 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id bo7-20020a0564020b2700b0043bdc47803csm4601912edb.30.2022.08.08.05.57.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 05:57:12 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Markus Armbruster Subject: [PULL 3/5] vl: fix [memory] section with -readconfig Date: Mon, 8 Aug 2022 14:57:04 +0200 Message-Id: <20220808125706.60511-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220808125706.60511-1-pbonzini@redhat.com> References: <20220808125706.60511-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" The -M memory.* options do not have magic applied to them like the -m option, namely no "M" (for mebibytes) is tacked at the end of a suffixless value for "-M memory.size". This magic is performed by parse_memory_options, and we have to do it for both "-m" and the [memory] section of a config file. Storing [memory] sections directly to machine_opts_dict changed the meaning of [memory] size = "1024" in a -readconfig file from 1024MiB to 8KiB (1024 Bytes rounded up to 8KiB silently). To avoid this, the [memory] section has to be changed back to QemuOpts (combining [memory] and "-m" will work fine thanks to .merge_lists being true). Change parse_memory_options() so that, similar to the older function set_memory_options(), it operates after command line parsing is done; and also call it where set_memory_options() used to be. Note, the parsing code uses exit(1) instead of exit(EXIT_FAILURE) to match neighboring code. Reported-by: Markus Armbruster Reviewed-by: Markus Armbruster Fixes: ce9d03fb3f ("machine: add mem compound property", 2022-05-12) Signed-off-by: Paolo Bonzini --- softmmu/vl.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/softmmu/vl.c b/softmmu/vl.c index aabd82e09a..45e919de9f 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -1947,16 +1947,15 @@ static void qemu_resolve_machine_memdev(void) } } -static void parse_memory_options(const char *arg) +static void parse_memory_options(void) { - QemuOpts *opts; + QemuOpts *opts = qemu_find_opts_singleton("memory"); QDict *dict, *prop; const char *mem_str; + Location loc; - opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), arg, true); - if (!opts) { - exit(EXIT_FAILURE); - } + loc_push_none(&loc); + qemu_opts_loc_restore(opts); prop = qdict_new(); @@ -1987,6 +1986,7 @@ static void parse_memory_options(const char *arg) qdict_put(dict, "memory", prop); keyval_merge(machine_opts_dict, dict, &error_fatal); qobject_unref(dict); + loc_pop(&loc); } static void qemu_create_machine(QDict *qdict) @@ -2053,8 +2053,7 @@ static bool is_qemuopts_group(const char *group) if (g_str_equal(group, "object") || g_str_equal(group, "machine") || g_str_equal(group, "smp-opts") || - g_str_equal(group, "boot-opts") || - g_str_equal(group, "memory")) { + g_str_equal(group, "boot-opts")) { return false; } return true; @@ -2078,8 +2077,6 @@ static void qemu_record_config_group(const char *group, QDict *dict, machine_merge_property("smp", dict, &error_fatal); } else if (g_str_equal(group, "boot-opts")) { machine_merge_property("boot", dict, &error_fatal); - } else if (g_str_equal(group, "memory")) { - machine_merge_property("memory", dict, &error_fatal); } else { abort(); } @@ -2882,7 +2879,10 @@ void qemu_init(int argc, char **argv, char **envp) exit(0); break; case QEMU_OPTION_m: - parse_memory_options(optarg); + opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true); + if (opts == NULL) { + exit(1); + } break; #ifdef CONFIG_TPM case QEMU_OPTION_tpmdev: @@ -3515,6 +3515,9 @@ void qemu_init(int argc, char **argv, char **envp) configure_rtc(qemu_find_opts_singleton("rtc")); + /* Transfer QemuOpts options into machine options */ + parse_memory_options(); + qemu_create_machine(machine_opts_dict); suspend_mux_open(); From patchwork Mon Aug 8 12:57:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12938771 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B1A48C25B0C for ; Mon, 8 Aug 2022 13:03:05 +0000 (UTC) Received: from localhost ([::1]:39796 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oL2PQ-0008Pz-KK for qemu-devel@archiver.kernel.org; Mon, 08 Aug 2022 09:03:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jr-0007sl-Qs for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:19 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:56041) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jq-0001gE-9C for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659963437; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=VUcJSPihr1CxmcurGisfq4kv3QpUdIwCLFpiSeuBEig=; b=WAuCMZwE4osiNUniiZ1MqpTgX37oNPSncdHvSxhzogFItQSLWi+B3L3wTTai2iuEndW7Mc YXa174XN9NX9K+oJL4VDheo3foDeKddLHiwH9A+T3qnfCWTOJllalZ87h7LF1Dfnsh9ssu 9xHTLB5tc5nLjOf1PX5bnKxY0P0Uv1E= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-oyvtYfukPw-PJx64uvkTVQ-1; Mon, 08 Aug 2022 08:57:16 -0400 X-MC-Unique: oyvtYfukPw-PJx64uvkTVQ-1 Received: by mail-ed1-f72.google.com with SMTP id m22-20020a056402431600b0043d6a88130aso5548051edc.18 for ; Mon, 08 Aug 2022 05:57:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=VUcJSPihr1CxmcurGisfq4kv3QpUdIwCLFpiSeuBEig=; b=clod4IC8K4TWdaxYIFUtMrQlzoVaubqiikiVvfGBI3V6SJQK6b3HDJTLWyqhPLPHkF 6llv1PkYgSGAV3tSiZrKphWGI16B+KIvYCaDuz+OCk0eeUyggsOjwaG1htHQZFDgnghj cY+s8irpOYq/emWDQxTV5pZ5cXkcqPdzXPNEm7FFEUo8APL11ntrFlTbZgrnfqofLz2n JVN9vA/zzHPGdsRD45ajrzNa8gdYncjGNv4EbgSjOhZC+zda+GZ4UDFIuLKw1ixPaImo +p5ov/qyKaojAwN+2YYxFfSqInuPvj5Gg97XDvan5en1goNdGEGRQlGwJuZAqjnTUz3J NV/g== X-Gm-Message-State: ACgBeo1m8xc2OtkQgIES+n4bXZH40OerPY9a3CFV4lXxniq+Tzku5kS4 Oqvg3N8s2snmCEzVawHbkBCpnaWCYuSEO9Uova91ogS8cr0ywI9NV/4DSDR1AiQNy6YTwOqfts1 QaSc7LaF6maYsPkDiOb4XJA16hR5KJ/ThX/ayl3MNRM0bzFgvZllimdVOYslR+62NpsY= X-Received: by 2002:a05:6402:15a:b0:431:71b9:86f3 with SMTP id s26-20020a056402015a00b0043171b986f3mr17698378edu.249.1659963435023; Mon, 08 Aug 2022 05:57:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR7tTSyTnaXwLkQ9Pza6P5H5E+1UTr+39Kevo8oDomRp89GOaZeWo0cAqniHWM8uYziXSHyY9g== X-Received: by 2002:a05:6402:15a:b0:431:71b9:86f3 with SMTP id s26-20020a056402015a00b0043171b986f3mr17698358edu.249.1659963434712; Mon, 08 Aug 2022 05:57:14 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id sd27-20020a170906ce3b00b00722e50dab2csm4964939ejb.109.2022.08.08.05.57.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 05:57:14 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: Markus Armbruster Subject: [PULL 4/5] vl: remove dead code in parse_memory_options() Date: Mon, 8 Aug 2022 14:57:05 +0200 Message-Id: <20220808125706.60511-5-pbonzini@redhat.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220808125706.60511-1-pbonzini@redhat.com> References: <20220808125706.60511-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" mem_str will never be an empty string, because qemu_opt_get_size() fails if it encounters one: $ ./qemu-system-x86_64 -m size= qemu-system-x86_64: -m size=: Parameter size expects a non-negative number below 2^64 Optional suffix k, M, G, T, P or E means kilo-, mega-, giga-, tera-, peta- and exabytes, respectively. Suggested-by: Markus Armbruster Reviewed-by: Markus Armbruster Signed-off-by: Paolo Bonzini --- softmmu/vl.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/softmmu/vl.c b/softmmu/vl.c index 45e919de9f..706bd7cff7 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -1960,13 +1960,8 @@ static void parse_memory_options(void) prop = qdict_new(); if (qemu_opt_get_size(opts, "size", 0) != 0) { - mem_str = qemu_opt_get(opts, "size"); - if (!*mem_str) { - error_report("missing 'size' option value"); - exit(EXIT_FAILURE); - } - /* Fix up legacy suffix-less format */ + mem_str = qemu_opt_get(opts, "size"); if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) { g_autofree char *mib_str = g_strdup_printf("%sM", mem_str); qdict_put_str(prop, "size", mib_str); From patchwork Mon Aug 8 12:57:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12938770 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 393F8C00140 for ; Mon, 8 Aug 2022 13:03:04 +0000 (UTC) Received: from localhost ([::1]:39586 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oL2PP-0008Hj-2c for qemu-devel@archiver.kernel.org; Mon, 08 Aug 2022 09:03:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52552) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jt-0007uQ-K7 for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:24757) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oL2Jr-0001gd-Ra for qemu-devel@nongnu.org; Mon, 08 Aug 2022 08:57:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659963439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=r55Q/nWnujAQEEI0G38aUER1jlX16gFcQi+soe3UOkA=; b=TqXFKZwK5jYm8Z3Xqq6iHgjY6gos8MeI8Kwi7uOlL9qDP7ZYTOpCNCb74hFczY9mLTD2Wl KvriEQFvxJmwqUjAT0zm2KzuFemDBxN/FLcIAwx4K1hhPRRvSWAgsnnyvwJZn7BgO3sKQo mD/CJvj4a6u1KXNmY1tRaK/zuZScOQM= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-451-aHVh-Jv7OduXs3jS0pPGXA-1; Mon, 08 Aug 2022 08:57:18 -0400 X-MC-Unique: aHVh-Jv7OduXs3jS0pPGXA-1 Received: by mail-ej1-f69.google.com with SMTP id s4-20020a170906500400b006feaccb3a0eso2367960ejj.11 for ; Mon, 08 Aug 2022 05:57:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=r55Q/nWnujAQEEI0G38aUER1jlX16gFcQi+soe3UOkA=; b=TgfqH6eaQx3LNn2OBxoksaEib56DBUxz1RWVHf0exrWelomYDLyWFdltZPOkQZOIiZ ny8RqO4ykiw6ReZqV3WnMnKQ2yrMAGfox518wfS796Fw4nCr2thXm3GnWU1VHp7XtiNs 8IReW43IMRH1T1nuxz1R3+11s5mKxu8fPAryB8RokbKG8dcLn37Bg28m5XvAWUM+vgtX 6nUtU3vU3v6wdUwjP5ubrRackrB28oL+ekXdZCTI/X1M5EsnLBBZDL9TQCVNo2FlqH7n ++V5knCOpjA6jwLM057ERAA7A+0pf77vdiWLQRC7hHA3VtZ4k5gw/zpugH5NEdBGDGNF WHUw== X-Gm-Message-State: ACgBeo3l5/b91Enb4arAj6TR6T5bOkf+MECdhH096bkOatWMhbnpzX5o OHRXX4i34G7EpwKp+IljQb2b6cDYzkrGHXghPliS3fMBt6iebArBFaUXL4bQrrhJWsjr5R6f4I5 DK3JPdP/iOuc8P30NAIbd/Z5CfGdZ47o3B6Cz9h5XXNnUQu6cbyxP7E2L3T2YTHQDqMQ= X-Received: by 2002:a17:906:8a57:b0:730:8b50:610a with SMTP id gx23-20020a1709068a5700b007308b50610amr13608881ejc.557.1659963436610; Mon, 08 Aug 2022 05:57:16 -0700 (PDT) X-Google-Smtp-Source: AA6agR6xEkiFEaHsVejkEVi+tmUZWXOOqCf+QlQtOoD+gfK5Ca3X2RQLDSSB8klqh7T4oPr+53NBrA== X-Received: by 2002:a17:906:8a57:b0:730:8b50:610a with SMTP id gx23-20020a1709068a5700b007308b50610amr13608867ejc.557.1659963436295; Mon, 08 Aug 2022 05:57:16 -0700 (PDT) Received: from goa-sendmail ([2001:b07:6468:f312:5e2c:eb9a:a8b6:fd3e]) by smtp.gmail.com with ESMTPSA id w6-20020a056402070600b0043e35ae2610sm4546615edx.27.2022.08.08.05.57.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Aug 2022 05:57:15 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= Subject: [PULL 5/5] tests/qtest: add scenario for -readconfig handling Date: Mon, 8 Aug 2022 14:57:06 +0200 Message-Id: <20220808125706.60511-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220808125706.60511-1-pbonzini@redhat.com> References: <20220808125706.60511-1-pbonzini@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, PP_MIME_FAKE_ASCII_TEXT=0.999, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" From: Daniel P. Berrangé This test of -readconfig validates the last three regressions we have fixed with -readconfig: * Interpretation of memory size units as MiB not bytes * Allow use of [spice] * Allow use of [object] Signed-off-by: Daniel P. Berrangé Message-Id: <20220805115529.124544-2-berrange@redhat.com> Signed-off-by: Paolo Bonzini --- tests/qtest/meson.build | 1 + tests/qtest/readconfig-test.c | 195 ++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 tests/qtest/readconfig-test.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index 3a474010e4..be4b30dea2 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -26,6 +26,7 @@ qtests_generic = [ 'qom-test', 'test-hmp', 'qos-test', + 'readconfig-test', ] if config_host.has_key('CONFIG_MODULES') qtests_generic += [ 'modules-test' ] diff --git a/tests/qtest/readconfig-test.c b/tests/qtest/readconfig-test.c new file mode 100644 index 0000000000..2e604d7c2d --- /dev/null +++ b/tests/qtest/readconfig-test.c @@ -0,0 +1,195 @@ +/* + * Validate -readconfig + * + * Copyright (c) 2022 Red Hat, Inc. + * + * This work is licensed under the terms of the GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ + +#include "qemu/osdep.h" +#include "libqtest.h" +#include "qapi/error.h" +#include "qapi/qapi-visit-machine.h" +#include "qapi/qapi-visit-qom.h" +#include "qapi/qapi-visit-ui.h" +#include "qapi/qmp/qdict.h" +#include "qapi/qmp/qlist.h" +#include "qapi/qobject-input-visitor.h" +#include "qapi/qmp/qstring.h" +#include "qemu/units.h" + +static QTestState *qtest_init_with_config(const char *cfgdata) +{ + GError *error = NULL; + g_autofree char *args = NULL; + int cfgfd = -1; + g_autofree char *cfgpath = NULL; + QTestState *qts; + ssize_t ret; + + cfgfd = g_file_open_tmp("readconfig-test-XXXXXX", &cfgpath, &error); + g_assert_no_error(error); + g_assert_cmpint(cfgfd, >=, 0); + + ret = qemu_write_full(cfgfd, cfgdata, strlen(cfgdata)); + if (ret < 0) { + unlink(cfgpath); + } + g_assert_cmpint(ret, ==, strlen(cfgdata)); + + close(cfgfd); + + args = g_strdup_printf("-nodefaults -machine none -readconfig %s", cfgpath); + + qts = qtest_init(args); + + unlink(cfgpath); + + return qts; +} + +static void test_x86_memdev_resp(QObject *res) +{ + Visitor *v; + g_autoptr(MemdevList) memdevs = NULL; + Memdev *memdev; + + g_assert(res); + v = qobject_input_visitor_new(res); + visit_type_MemdevList(v, NULL, &memdevs, &error_abort); + + g_assert(memdevs); + g_assert(memdevs->value); + g_assert(!memdevs->next); + + memdev = memdevs->value; + g_assert_cmpstr(memdev->id, ==, "ram"); + g_assert_cmpint(memdev->size, ==, 200 * MiB); + + visit_free(v); +} + +static void test_x86_memdev(void) +{ + QDict *resp; + QTestState *qts; + const char *cfgdata = + "[memory]\n" + "size = \"200\""; + + qts = qtest_init_with_config(cfgdata); + /* Test valid command */ + resp = qtest_qmp(qts, "{ 'execute': 'query-memdev' }"); + test_x86_memdev_resp(qdict_get(resp, "return")); + qobject_unref(resp); + + qtest_quit(qts); +} + + +#ifdef CONFIG_SPICE +static void test_spice_resp(QObject *res) +{ + Visitor *v; + g_autoptr(SpiceInfo) spice = NULL; + + g_assert(res); + v = qobject_input_visitor_new(res); + visit_type_SpiceInfo(v, "spcie", &spice, &error_abort); + + g_assert(spice); + g_assert(spice->enabled); + + visit_free(v); +} + +static void test_spice(void) +{ + QDict *resp; + QTestState *qts; + const char *cfgdata = + "[spice]\n" + "disable-ticketing = \"on\"\n" + "unix = \"on\"\n"; + + qts = qtest_init_with_config(cfgdata); + /* Test valid command */ + resp = qtest_qmp(qts, "{ 'execute': 'query-spice' }"); + test_spice_resp(qdict_get(resp, "return")); + qobject_unref(resp); + + qtest_quit(qts); +} +#endif + +static void test_object_rng_resp(QObject *res) +{ + Visitor *v; + g_autoptr(ObjectPropertyInfoList) objs = NULL; + ObjectPropertyInfoList *tmp; + ObjectPropertyInfo *obj; + bool seen_rng = false; + + g_assert(res); + v = qobject_input_visitor_new(res); + visit_type_ObjectPropertyInfoList(v, NULL, &objs, &error_abort); + + g_assert(objs); + tmp = objs; + while (tmp) { + g_assert(tmp->value); + + obj = tmp->value; + if (g_str_equal(obj->name, "rng0") && + g_str_equal(obj->type, "child")) { + seen_rng = true; + } + + tmp = tmp->next; + } + + g_assert(seen_rng); + + visit_free(v); +} + +static void test_object_rng(void) +{ + QDict *resp; + QTestState *qts; + const char *cfgdata = + "[object]\n" + "qom-type = \"rng-builtin\"\n" + "id = \"rng0\"\n"; + + qts = qtest_init_with_config(cfgdata); + /* Test valid command */ + resp = qtest_qmp(qts, + "{ 'execute': 'qom-list'," + " 'arguments': {'path': '/objects' }}"); + test_object_rng_resp(qdict_get(resp, "return")); + qobject_unref(resp); + + qtest_quit(qts); +} + +int main(int argc, char *argv[]) +{ + const char *arch; + g_test_init(&argc, &argv, NULL); + + arch = qtest_get_arch(); + + if (g_str_equal(arch, "i386") || + g_str_equal(arch, "x86_64")) { + qtest_add_func("readconfig/x86/memdev", test_x86_memdev); + } +#ifdef CONFIG_SPICE + qtest_add_func("readconfig/spice", test_spice); +#endif + + qtest_add_func("readconfig/object-rng", test_object_rng); + + return g_test_run(); +}