Skip to contentSkip to conversation

tests: Clean up around qmp() and hmp()

PATCH In review Markus Armbruster 3 participants 36 messages Jul 21, 2017 – Jul 27, 2017

lore
Share & tools
lore
#16 lore raw

PATCH 5/9 tests/libqos/usb: Clean up string interpolation into QMP input

In reply to Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the previous commit.

The case in usb_test_hotplug() slightly more complicated: it interpolates *into* JSON values. Clean it up by building the values separately, so we can again leave interpolation to qmp().

---
 tests/libqos/usb.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
tests/libqos/usb.c +14 -16
index 0cdfaec..f88d4a6 100644
@@ -40,18 +40,20 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
 void usb_test_hotplug(const char *hcd_id, const int port,
                       void (*port_check)(void))
 {
+    char id[32];
+    char *bus;
     QDict *response;
-    char  *cmd;
 
-    cmd = g_strdup_printf("{'execute': 'device_add',"
-                          " 'arguments': {"
-                          "   'driver': 'usb-tablet',"
-                          "   'port': '%d',"
-                          "   'bus': '%s.0',"
-                          "   'id': 'usbdev%d'"
-                          "}}", port, hcd_id, port);
-    response = qmp(cmd);
-    g_free(cmd);
+    sprintf(id, "usbdev%d", port);
+    bus = g_strdup_printf("%s.0", hcd_id);
+    response = qmp("{'execute': 'device_add',"
+                   " 'arguments': {"
+                   "   'driver': 'usb-tablet',"
+                   "   'port': %s,"
+                   "   'bus': %s,"
+                   "   'id': %s"
+                   " }}", id + 6, bus, id);
+    g_free(bus);
     g_assert(response);
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
@@ -60,12 +62,8 @@ void usb_test_hotplug(const char *hcd_id, const int port,
         port_check();
     }
 
-    cmd = g_strdup_printf("{'execute': 'device_del',"
-                           " 'arguments': {"
-                           "   'id': 'usbdev%d'"
-                           "}}", port);
-    response = qmp(cmd);
-    g_free(cmd);
+    response = qmp("{'execute': 'device_del', 'arguments': { 'id': %s }}",
+                   id);
     g_assert(response);
     g_assert(qdict_haskey(response, "event"));
     g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
Signature · 1 line
2.7.5
#0 lore raw

cover tests: Clean up around qmp() and hmp()

Eric Blake (2):
qtest: Avoid passing raw strings through hmp()
qtest: Document calling conventions

Markus Armbruster (7):
tests: Pass literal format strings directly to qmp_FOO()
tests: Clean up string interpolation into QMP input (simple cases) tests/libqos/usb: Clean up string interpolation into QMP input tests/libqos/pci: Clean up string interpolation into QMP input
tests: Clean up wait for event
tests/libqtest: Clean up how we read the QMP greeting tests/libqtest: Enable compile-time format string checking

 tests/ahci-test.c         |   4 +-
 tests/boot-order-test.c   |   2 +-
 tests/ide-test.c          |   4 +-
 tests/ivshmem-test.c      |  10 +--
 tests/libqos/libqos.c     |  16 +----
 tests/libqos/pci-pc.c     |  15 +----
 tests/libqos/pci.c        |  33 +++++-----
 tests/libqos/pci.h        |   2 +-
 tests/libqos/usb.c        |  30 ++++-----
 tests/libqtest.c          |   4 +-
 tests/libqtest.h          |  62 +++++++++++-------
 tests/postcopy-test.c     |   8 +--
 tests/tco-test.c          |   3 +-
 tests/test-hmp.c          |   4 +-
 tests/test-qga.c          | 160 ++++++++++++++++++++--------------------------
 tests/usb-hcd-uhci-test.c |   6 +-
 tests/usb-hcd-xhci-test.c |  12 +---
 tests/vhost-user-test.c   |   6 +-
 tests/virtio-blk-test.c   |   5 +-
 tests/wdt_ib700-test.c    |  35 +++-------
 20 files changed, 180 insertions(+), 241 deletions(-)
Signature · 1 line
2.7.5
#1 lore raw

PATCH 1/9 qtest: Avoid passing raw strings through hmp()

From: Eric Blake <[email protected]>

The next patch will add __attribute__((__format__)) to hmp(), which in turn causes gcc to warn about non-literal format strings. Rather than risk an arbitrary string containing % being mis-handled, always pass variable strings along with a %s format. It also makes it easier to prove correctness locally, rather than auditing all the source strings.

Message-Id: <[email protected]>

---
 tests/test-hmp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/test-hmp.c +2 -2
index d77b3c8..0af0664 100644
@@ -80,7 +80,7 @@ static void test_commands(void)
         if (verbose) {
             fprintf(stderr, "\t%s\n", hmp_cmds[i]);
         }
-        response = hmp(hmp_cmds[i]);
+        response = hmp("%s", hmp_cmds[i]);
         g_free(response);
     }
 
@@ -103,7 +103,7 @@ static void test_info_commands(void)
         if (verbose) {
             fprintf(stderr, "\t%s\n", info);
         }
-        resp = hmp(info);
+        resp = hmp("%s", info);
         g_free(resp);
         /* And move forward to the next line */
         info = strchr(endp + 1, '\n');
Signature · 1 line
2.7.5
#3 lore raw

PATCH 2/9 qtest: Document calling conventions

In reply to Markus Armbruster

From: Eric Blake <[email protected]>

We have two flavors of vararg usage in qtest; make it clear that qmp() has different semantics than hmp(), and let the compiler enforce that hmp() is used correctly. However, qmp() (and friends) only accept a subset of printf flags look-alikes (namely, those that our JSON parser understands), and what is worse, qmp("true") (the JSON keyword 'true') is different from qmp("%s", "true") (the JSON string '"true"'), so marking those as printf-like would produce more harm from bogus warnings than it helps (we may have made a mistake in previously marking qobject_from_jsonf(), but this patch is not addressing that).

Message-Id: <[email protected]>

---
 tests/libqtest.h | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
tests/libqtest.h +14 -8
index 38bc1e9..ae57282 100644
@@ -50,7 +50,8 @@ void qtest_quit(QTestState *s);
 /**
  * qtest_qmp_discard_response:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu; formats arguments through
+ * json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
@@ -59,7 +60,8 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
 /**
  * qtest_qmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu; formats arguments through
+ * json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').
  *
  * Sends a QMP message to QEMU and returns the response.
  */
@@ -68,7 +70,8 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
 /**
  * qtest_async_qmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu; formats arguments through
+ * json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
@@ -134,7 +137,7 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
 /**
  * qtest_hmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like vsprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  * QMP events are discarded.
@@ -535,7 +538,8 @@ static inline void qtest_end(void)
 
 /**
  * qmp:
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu; formats arguments through
+ * json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').
  *
  * Sends a QMP message to QEMU and returns the response.
  */
@@ -543,7 +547,8 @@ QDict *qmp(const char *fmt, ...);
 
 /**
  * qmp_async:
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu; formats arguments through
+ * json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
@@ -551,7 +556,8 @@ void qmp_async(const char *fmt, ...);
 
 /**
  * qmp_discard_response:
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu; formats arguments through
+ * json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
@@ -592,7 +598,7 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 
 /**
  * hmp:
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like vsprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  *
Signature · 1 line
2.7.5
#7 lore raw

PATCH 3/9 tests: Pass literal format strings directly to qmp_FOO()

In reply to Markus Armbruster

The qmp_FOO() take a printf-like format string. In a few places, we assign a string literal to a variable and pass that instead of simply passing the literal. Clean that up.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---
 tests/ahci-test.c | 4 +---
 tests/ide-test.c  | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
tests/ahci-test.c +1 -3
index 999121b..97622e0 100644
@@ -1350,7 +1350,6 @@ static void test_flush_migrate(void)
     AHCIQState *src, *dst;
     AHCICommand *cmd;
     uint8_t px;
-    const char *s;
     char *uri = g_strdup_printf("unix:%s", mig_socket);
 
     prepare_blkdebug_script(debug_path, "flush_to_disk");
@@ -1386,8 +1385,7 @@ static void test_flush_migrate(void)
     ahci_migrate(src, dst, uri);
 
     /* Complete the command */
-    s = "{'execute':'cont' }";
-    qmp_async(s);
+    qmp_async("{'execute':'cont' }");
     qmp_eventwait("RESUME");
     ahci_command_wait(dst, cmd);
     ahci_command_verify(dst, cmd);
tests/ide-test.c +1 -3
index bfd79dd..ea2657d 100644
@@ -624,7 +624,6 @@ static void test_retry_flush(const char *machine)
     QPCIDevice *dev;
     QPCIBar bmdma_bar, ide_bar;
     uint8_t data;
-    const char *s;
 
     prepare_blkdebug_script(debug_path, "flush_to_disk");
 
@@ -652,8 +651,7 @@ static void test_retry_flush(const char *machine)
     qmp_eventwait("STOP");
 
     /* Complete the command */
-    s = "{'execute':'cont' }";
-    qmp_discard_response(s);
+    qmp_discard_response("{'execute':'cont' }");
 
     /* Check registers */
     data = qpci_io_readb(dev, ide_bar, reg_device);
Signature · 1 line
2.7.5
#20 lore raw

PATCH 6/9 tests/libqos/pci: Clean up string interpolation into QMP input

In reply to Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

The case in qpci_plug_device_test() is a bit complicated: it interpolates several JSON object members, not just a value. Clean it up by passing them in as QDict rather than string, so we can leave interpolation to qmp() here and to qobject_from_jsonf() in callers.

---
 tests/ivshmem-test.c    | 10 +++++-----
 tests/libqos/pci.c      | 33 ++++++++++++++++++---------------
 tests/libqos/pci.h      |  2 +-
 tests/virtio-blk-test.c |  5 ++++-
 4 files changed, 28 insertions(+), 22 deletions(-)
tests/ivshmem-test.c +5 -5
index 3776342..38044bb 100644
@@ -14,6 +14,7 @@
 #include "libqos/libqos-pc.h"
 #include "libqos/libqos-spapr.h"
 #include "libqtest.h"
+#include "qapi/qmp/qjson.h"
 #include "qemu-common.h"
 
 #define TMPSHMSIZE (1 << 20)
@@ -419,19 +420,18 @@ static void test_ivshmem_server_irq(void)
 static void test_ivshmem_hotplug(void)
 {
     const char *arch = qtest_get_arch();
-    gchar *opts;
+    QObject *extra_args = qobject_from_jsonf("{ 'shm': '%s', 'size': '1M' }",
+                                             tmpshm);
 
     qtest_start("");
 
-    opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
-
-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
+    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP,
+                          qobject_to_qdict(extra_args));
     if (strcmp(arch, "ppc64") != 0) {
         qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
     }
 
     qtest_end();
-    g_free(opts);
 }
 
 static void test_ivshmem_memdev(void)
tests/libqos/pci.c +18 -15
index 2dcdead..4068305 100644
@@ -14,6 +14,7 @@
 #include "libqos/pci.h"
 
 #include "hw/pci/pci_regs.h"
+#include "qapi/qmp/qjson.h"
 #include "qemu/host-utils.h"
 
 void qpci_device_foreach(QPCIBus *bus, int vendor_id, int device_id,
@@ -392,23 +393,25 @@ QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr)
 }
 
 void qpci_plug_device_test(const char *driver, const char *id,
-                           uint8_t slot, const char *opts)
+                           uint8_t slot, QDict *extra_args)
 {
-    QDict *response;
-    char *cmd;
-
-    cmd = g_strdup_printf("{'execute': 'device_add',"
-                          " 'arguments': {"
-                          "   'driver': '%s',"
-                          "   'addr': '%d',"
-                          "   %s%s"
-                          "   'id': '%s'"
-                          "}}", driver, slot,
-                          opts ? opts : "", opts ? "," : "",
-                          id);
-    response = qmp(cmd);
-    g_free(cmd);
+    char addr[8];
+    QDict *args, *response;
+
+    sprintf(addr, "%d", slot);
+    args = qobject_to_qdict(
+        qobject_from_jsonf("{ 'driver': %s, 'addr': %s, 'id': %s}",
+                           driver, addr, id));
+
+    if (extra_args) {
+        qdict_join(args, extra_args, true);
+        QDECREF(extra_args);
+    }
+
+    response = qmp("{'execute': 'device_add', 'arguments': %p }", args);
+
     g_assert(response);
     g_assert(!qdict_haskey(response, "error"));
+    QDECREF(args);
     QDECREF(response);
 }
tests/libqos/pci.h +1 -1
index ed48061..c981061 100644
@@ -109,6 +109,6 @@ void qpci_iounmap(QPCIDevice *dev, QPCIBar addr);
 QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr);
 
 void qpci_plug_device_test(const char *driver, const char *id,
-                           uint8_t slot, const char *opts);
+                           uint8_t slot, QDict *extra_args);
 void qpci_unplug_acpi_device_test(const char *id, uint8_t slot);
 #endif
tests/virtio-blk-test.c +4 -1
index 0576cb1..64a48f4 100644
@@ -16,6 +16,7 @@
 #include "libqos/virtio-pci.h"
 #include "libqos/virtio-mmio.h"
 #include "libqos/malloc-generic.h"
+#include "qapi/qmp/qjson.h"
 #include "qemu/bswap.h"
 #include "standard-headers/linux/virtio_ids.h"
 #include "standard-headers/linux/virtio_config.h"
@@ -658,12 +659,13 @@ static void pci_hotplug(void)
     QVirtioPCIDevice *dev;
     QOSState *qs;
     const char *arch = qtest_get_arch();
+    QObject *extra_args = qobject_from_jsonf("{ 'drive': 'drive1' }");
 
     qs = pci_test_start();
 
     /* plug secondary disk */
     qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP,
-                          "'drive': 'drive1'");
+                          qobject_to_qdict(extra_args));
 
     dev = virtio_blk_pci_init(qs->pcibus, PCI_SLOT_HP);
     g_assert(dev);
@@ -674,6 +676,7 @@ static void pci_hotplug(void)
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP);
     }
+
     qtest_shutdown(qs);
 }
 
Signature · 1 line
2.7.5
#26 lore raw

PATCH 7/9 tests: Clean up wait for event

In reply to Markus Armbruster

We still use hacks like qmp("") to wait for an event, even though we have qmp_eventwait() since commit 8fe941f, and qmp_eventwait_ref() since commit 7ffe312. Both commits neglected to convert all the existing hacks. Make up what they missed.

Bonus: gets rid of empty format strings. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---
 tests/boot-order-test.c   |  2 +-
 tests/libqos/pci-pc.c     |  6 +-----
 tests/tco-test.c          |  3 +--
 tests/usb-hcd-uhci-test.c |  6 +-----
 tests/usb-hcd-xhci-test.c | 12 ++----------
 tests/wdt_ib700-test.c    | 35 ++++++++++-------------------------
 6 files changed, 16 insertions(+), 48 deletions(-)
tests/boot-order-test.c +1 -1
index fc1e794..9d51683 100644
@@ -43,7 +43,7 @@ static void test_a_boot_order(const char *machine,
      * system_reset only requests reset.  We get a RESET event after
      * the actual reset completes.  Need to wait for that.
      */
-    qmp_discard_response("");   /* HACK: wait for event */
+    qmp_eventwait("RESET");
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_reboot);
     qtest_quit(global_qtest);
tests/libqos/pci-pc.c +1 -5
index d40aa9d..e4be809 100644
@@ -168,9 +168,5 @@ void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
 
     outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
 
-    response = qmp("");
-    g_assert(response);
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
+    qmp_eventwait("DEVICE_DELETED");
 }
tests/tco-test.c +1 -2
index c4c264e..f2ed6ed 100644
@@ -237,9 +237,8 @@ static void test_tco_max_timeout(void)
 
 static QDict *get_watchdog_action(void)
 {
-    QDict *ev = qmp("");
+    QDict *ev = qmp_eventwait_ref("WATCHDOG");
     QDict *data;
-    g_assert(!strcmp(qdict_get_str(ev, "event"), "WATCHDOG"));
 
     data = qdict_get_qdict(ev, "data");
     QINCREF(data);
tests/usb-hcd-uhci-test.c +1 -5
index 5b500fe..0fb7f8d 100644
@@ -68,11 +68,7 @@ static void test_usb_storage_hotplug(void)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
 
-    response = qmp("");
-    g_assert(response);
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
+    qmp_eventwait("DEVICE_DELETED");
 }
 
 int main(int argc, char **argv)
tests/usb-hcd-xhci-test.c +2 -10
index 031764d..c05a339 100644
@@ -57,11 +57,7 @@ static void test_usb_uas_hotplug(void)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
 
-    response = qmp("");
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
-
+    qmp_eventwait("DEVICE_DELETED");
 
     response = qmp("{'execute': 'device_del',"
                            " 'arguments': {"
@@ -71,11 +67,7 @@ static void test_usb_uas_hotplug(void)
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
 
-    response = qmp("");
-    g_assert(response);
-    g_assert(qdict_haskey(response, "event"));
-    g_assert(!strcmp(qdict_get_str(response, "event"), "DEVICE_DELETED"));
-    QDECREF(response);
+    qmp_eventwait("DEVICE_DELETED");
 }
 
 int main(int argc, char **argv)
tests/wdt_ib700-test.c +10 -25
index 49f4f0c..4fc8eea 100644
@@ -18,26 +18,10 @@ static void qmp_check_no_event(void)
     QDECREF(resp);
 }
 
-static QDict *qmp_get_event(const char *name)
-{
-    QDict *event = qmp("");
-    QDict *data;
-    g_assert(qdict_haskey(event, "event"));
-    g_assert(!strcmp(qdict_get_str(event, "event"), name));
-
-    if (qdict_haskey(event, "data")) {
-        data = qdict_get_qdict(event, "data");
-        QINCREF(data);
-    } else {
-        data = NULL;
-    }
-
-    QDECREF(event);
-    return data;
-}
-
 static QDict *ib700_program_and_wait(QTestState *s)
 {
+    QDict *event, *data;
+
     clock_step(NANOSECONDS_PER_SECOND * 40);
     qmp_check_no_event();
 
@@ -61,7 +45,11 @@ static QDict *ib700_program_and_wait(QTestState *s)
     clock_step(3 * NANOSECONDS_PER_SECOND);
     qmp_check_no_event();
     clock_step(2 * NANOSECONDS_PER_SECOND);
-    return qmp_get_event("WATCHDOG");
+    event = qmp_eventwait_ref("WATCHDOG");
+    data = qdict_get_qdict(event, "data");
+    QINCREF(data);
+    QDECREF(event);
+    return data;
 }
 
 
@@ -73,8 +61,7 @@ static void ib700_pause(void)
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "pause"));
     QDECREF(d);
-    d = qmp_get_event("STOP");
-    QDECREF(d);
+    qmp_eventwait("STOP");
     qtest_end();
 }
 
@@ -86,8 +73,7 @@ static void ib700_reset(void)
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
-    d = qmp_get_event("RESET");
-    QDECREF(d);
+    qmp_eventwait("RESET");
     qtest_end();
 }
 
@@ -99,8 +85,7 @@ static void ib700_shutdown(void)
     d = ib700_program_and_wait(s);
     g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
     QDECREF(d);
-    d = qmp_get_event("SHUTDOWN");
-    QDECREF(d);
+    qmp_eventwait("SHUTDOWN");
     qtest_end();
 }
 
Signature · 1 line
2.7.5
#29 lore raw

PATCH 8/9 tests/libqtest: Clean up how we read the QMP greeting

In reply to Markus Armbruster

qtest_init() still uses the qtest_qmp_discard_response(s, "") hack to receive the greeting, even though we have qtest_qmp_receive() since commit 66e0c7b. Put it to use.

Bonus: gets rid of an empty format string. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---
 tests/libqtest.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/libqtest.c +3 -1
index 4a5492a..7e5425d 100644
@@ -230,9 +230,11 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
 QTestState *qtest_init(const char *extra_args)
 {
     QTestState *s = qtest_init_without_qmp_handshake(extra_args);
+    QDict *greeting;
 
     /* Read the QMP greeting and then do the handshake */
-    qtest_qmp_discard_response(s, "");
+    greeting = qtest_qmp_receive(s);
+    QDECREF(greeting);
     qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
 
     return s;
Signature · 1 line
2.7.5
#32 lore raw

PATCH 9/9 tests/libqtest: Enable compile-time format string checking

In reply to Markus Armbruster

qtest_qmp() & friends pass their format string and variable arguments to qobject_from_jsonv(). Unlike qobject_from_jsonv(), they aren't decorated with GCC_FMT_ATTR(). Fix that to get compile-time format string checking.

---
 tests/libqtest.h | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
tests/libqtest.h +24 -16
index ae57282..435012d 100644
@@ -55,7 +55,8 @@ void qtest_quit(QTestState *s);
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
-void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
+void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_qmp:
@@ -65,7 +66,8 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
+QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_async_qmp:
@@ -75,7 +77,8 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qtest_async_qmp(QTestState *s, const char *fmt, ...);
+void qtest_async_qmp(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_qmpv_discard_response:
@@ -85,7 +88,8 @@ void qtest_async_qmp(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
-void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
+void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_qmpv:
@@ -95,7 +99,8 @@ void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
+QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_async_qmpv:
@@ -105,7 +110,8 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap);
+void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_receive:
@@ -144,7 +150,8 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *qtest_hmp(QTestState *s, const char *fmt, ...);
+char *qtest_hmp(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_hmpv:
@@ -157,7 +164,8 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...);
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);
+char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_get_irq:
@@ -543,7 +551,7 @@ static inline void qtest_end(void)
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qmp(const char *fmt, ...);
+QDict *qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * qmp_async:
@@ -552,7 +560,7 @@ QDict *qmp(const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qmp_async(const char *fmt, ...);
+void qmp_async(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * qmp_discard_response:
@@ -561,7 +569,7 @@ void qmp_async(const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
-void qmp_discard_response(const char *fmt, ...);
+void qmp_discard_response(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * qmp_receive:
@@ -604,7 +612,7 @@ static inline QDict *qmp_eventwait_ref(const char *event)
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *hmp(const char *fmt, ...);
+char *hmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * get_irq:
@@ -920,10 +928,10 @@ static inline int64_t clock_set(int64_t val)
 }
 
 QDict *qmp_fd_receive(int fd);
-void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
-void qmp_fd_send(int fd, const char *fmt, ...);
-QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
-QDict *qmp_fd(int fd, const char *fmt, ...);
+void qmp_fd_sendv(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
+void qmp_fd_send(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
+QDict *qmp_fdv(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
+QDict *qmp_fd(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_cb_for_every_machine:
Signature · 1 line
2.7.5
#10 lore raw

PATCH 4/9 tests: Clean up string interpolation into QMP input (simple cases)

In reply to Markus Armbruster

When you build QMP input manually like this

    cmd = g_strdup_printf("{ 'execute': 'migrate',"
                          "'arguments': { 'uri': '%s' } }",
                          uri);
    rsp = qmp(cmd);
    g_free(cmd);

you're responsible for escaping the interpolated values for JSON. Not done here, and therefore works only for sufficiently nice @uri. For instance, if @uri contained a single "'", qobject_from_jsonv() would fail, qmp_fd_sendv() would misinterpret the failure as empty input and do nothing, and the test would hang waiting for a response that never comes.

Leaving interpolation into JSON to qmp() is more robust:

    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);

It's also more concise.

Clean up the simple cases where we interpolate exactly a JSON value.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---
 tests/libqos/libqos.c   |  16 +----
 tests/libqos/pci-pc.c   |   9 +--
 tests/postcopy-test.c   |   8 +--
 tests/test-qga.c        | 160 +++++++++++++++++++++---------------------------
 tests/vhost-user-test.c |   6 +-
 5 files changed, 77 insertions(+), 122 deletions(-)
tests/libqos/libqos.c +2 -14
index 6226546..42c5315 100644
@@ -86,20 +86,12 @@ void set_context(QOSState *s)
 
 static QDict *qmp_execute(const char *command)
 {
-    char *fmt;
-    QDict *rsp;
-
-    fmt = g_strdup_printf("{ 'execute': '%s' }", command);
-    rsp = qmp(fmt);
-    g_free(fmt);
-
-    return rsp;
+    return qmp("{ 'execute': %s }", command);
 }
 
 void migrate(QOSState *from, QOSState *to, const char *uri)
 {
     const char *st;
-    char *s;
     QDict *rsp, *sub;
     bool running;
 
@@ -114,11 +106,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
     QDECREF(rsp);
 
     /* Issue the migrate command. */
-    s = g_strdup_printf("{ 'execute': 'migrate',"
-                        "'arguments': { 'uri': '%s' } }",
-                        uri);
-    rsp = qmp(s);
-    g_free(s);
+    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);
 
tests/libqos/pci-pc.c +2 -7
index ded1c54..d40aa9d 100644
@@ -159,14 +159,9 @@ void qpci_free_pc(QPCIBus *bus)
 void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
 {
     QDict *response;
-    char *cmd;
 
-    cmd = g_strdup_printf("{'execute': 'device_del',"
-                          " 'arguments': {"
-                          "   'id': '%s'"
-                          "}}", id);
-    response = qmp(cmd);
-    g_free(cmd);
+    response = qmp("{'execute': 'device_del', 'arguments': {'id': %s}}",
+                   id);
     g_assert(response);
     g_assert(!qdict_haskey(response, "error"));
     QDECREF(response);
tests/postcopy-test.c +2 -6
index 8142f2a..e2ead87 100644
@@ -358,7 +358,7 @@ static void test_migrate(void)
     char *uri = g_strdup_printf("unix:%s/migsocket", tmpfs);
     QTestState *global = global_qtest, *from, *to;
     unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d;
-    gchar *cmd, *cmd_src, *cmd_dst;
+    gchar *cmd_src, *cmd_dst;
     QDict *rsp;
 
     char *bootpath = g_strdup_printf("%s/bootsect", tmpfs);
@@ -445,11 +445,7 @@ static void test_migrate(void)
     /* Wait for the first serial output from the source */
     wait_for_serial("src_serial");
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "'arguments': { 'uri': '%s' } }",
-                          uri);
-    rsp = qmp(cmd);
-    g_free(cmd);
+    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);
 
tests/test-qga.c +70 -90

320 lines not on this page · raw message

tests/vhost-user-test.c +1 -5
index d4da09f..f2a2b6c 100644
@@ -662,11 +662,7 @@ static void test_migrate(void)
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "'arguments': { 'uri': '%s' } }",
-                          uri);
-    rsp = qmp(cmd);
-    g_free(cmd);
+    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
     g_assert(qdict_haskey(rsp, "return"));
     QDECREF(rsp);
 
Signature · 1 line
2.7.5
#4 lore raw

Re: [Qemu-devel] [PATCH 2/9] qtest: Document calling conventions

In reply to Markus Armbruster
Quoted 13 lines · Markus Armbruster

From: Eric Blake <[email protected]>

We have two flavors of vararg usage in qtest; make it clear that qmp() has different semantics than hmp(), and let the compiler enforce that hmp() is used correctly. However, qmp() (and friends) only accept a subset of printf flags look-alikes (namely, those that our JSON parser understands), and what is worse, qmp("true") (the JSON keyword 'true') is different from qmp("%s", "true") (the JSON string '"true"'), so marking those as printf-like would produce more harm from bogus warnings than it helps (we may have made a mistake in previously marking qobject_from_jsonf(), but this patch is not addressing that).

Quoted 3 lines

Message-Id: <[email protected]>

As you pointed out on the other thread,

tests/libqtest.h QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event); · line 143

Quoted 9 lines
tests/libqtest.h +1 -1
@@ -134,7 +137,7 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
 /**
  * qtest_hmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like vsprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  * QMP events are discarded.

I accidentally killed the attribute here,

tests/libqtest.h static inline QDict *qmp_eventwait_ref(const char *event) · line 604

Quoted 9 lines
tests/libqtest.h +1 -1
@@ -592,7 +598,7 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 
 /**
  * hmp:
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like vsprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  *

and here.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 3/9
#8 lore raw

Re: [Qemu-devel] [PATCH 3/9] tests: Pass literal format strings directly to qmp_FOO()

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

The qmp_FOO() take a printf-like format string. In a few places, we assign a string literal to a variable and pass that instead of simply passing the literal. Clean that up.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Quoted 6 lines
---
 tests/ahci-test.c | 4 +---
 tests/ide-test.c  | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#11 lore raw

Re: [Qemu-devel] [PATCH 4/9] tests: Clean up string interpolation into QMP input (simple cases)

In reply to Markus Armbruster
Quoted 30 lines · Markus Armbruster

When you build QMP input manually like this

    cmd = g_strdup_printf("{ 'execute': 'migrate',"
                          "'arguments': { 'uri': '%s' } }",
                          uri);
    rsp = qmp(cmd);
    g_free(cmd);

you're responsible for escaping the interpolated values for JSON. Not done here, and therefore works only for sufficiently nice @uri. For instance, if @uri contained a single "'", qobject_from_jsonv() would fail, qmp_fd_sendv() would misinterpret the failure as empty input and do nothing, and the test would hang waiting for a response that never comes.

Leaving interpolation into JSON to qmp() is more robust:

    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);

It's also more concise.

Clean up the simple cases where we interpolate exactly a JSON value.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---

tests/test-qga.c static void test_qga_sync_delimited(gconstpointer fix) · line 150

Quoted 3 lines
tests/test-qga.c +3 -0
+    qmp_fd_send(fixture->fd,
+                "\xff{'execute': 'guest-sync-delimited',"
+                " 'arguments': {'id': %u } }",

Ouch. Per 2/9:
* json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').

That will need to be %d now. Or, more likely, we need to update the comments in json-lexer.c and in 2/9 (/me goes to read json-lexer.c...)

    /* escape */
    [IN_ESCAPE_LL] = {
        ['d'] = JSON_ESCAPE,
        ['u'] = JSON_ESCAPE,
    },
...
    [IN_ESCAPE] = {
        ['d'] = JSON_ESCAPE,
        ['i'] = JSON_ESCAPE,
        ['p'] = JSON_ESCAPE,
        ['s'] = JSON_ESCAPE,
        ['u'] = JSON_ESCAPE,
        ['f'] = JSON_ESCAPE,
        ['l'] = IN_ESCAPE_L,
        ['I'] = IN_ESCAPE_I,

Aha, that 'd' should be '[du]' in all of the comments.

tests/test-qga.c static void test_qga_file_ops(gconstpointer fix) · line 413

Quoted 11 lines
tests/test-qga.c +3 -4
@@ -411,10 +408,10 @@ static void test_qga_file_ops(gconstpointer fix)
 
     enc = g_base64_encode(helloworld, sizeof(helloworld));
     /* write */
-    cmd = g_strdup_printf("{'execute': 'guest-file-write',"
-                          " 'arguments': { 'handle': %" PRId64 ","
-                          " 'buf-b64': '%s' } }", id, enc);
-    ret = qmp_fd(fixture->fd, cmd);
+    ret = qmp_fd(fixture->fd,
+                 "{'execute': 'guest-file-write',"
+                 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",

Ouch; you are reverting commit 1792d7d0. We tried hard to make json-lexer.c understand lots of different 64-bit format spellings, but we KNOW that we don't support MacOS (see commit 29a6731). We either need to beef up json-lexer.c to understand %qd, or get rid of JSON psuedo-strings, if we expect this to work; otherwise, you should use %lld and long long instead of PRId64 and uint64_t.

Overall, I like the patch, but we need to fix the problems for the next round of this series.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 5/9
#17 lore raw

Re: [Qemu-devel] [PATCH 5/9] tests/libqos/usb: Clean up string interpolation into QMP input

In reply to Markus Armbruster
Quoted 4 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the previous commit.

The case in usb_test_hotplug() slightly more complicated: it

s/()/() is/

Quoted 21 lines

interpolates *into* JSON values. Clean it up by building the values separately, so we can again leave interpolation to qmp().

---
 tests/libqos/usb.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
tests/libqos/usb.c +2 -1
index 0cdfaec..f88d4a6 100644
@@ -40,18 +40,20 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
 void usb_test_hotplug(const char *hcd_id, const int port,
                       void (*port_check)(void))
 {
+    char id[32];
+    char *bus;
     QDict *response;
-    char  *cmd;
 

tests/libqos/usb.c void uhci_port_test(struct qhc *hc, int port, uint16_t expect) · line 47

Quoted 10 lines
tests/libqos/usb.c +1 -9
-    cmd = g_strdup_printf("{'execute': 'device_add',"
-                          " 'arguments': {"
-                          "   'driver': 'usb-tablet',"
-                          "   'port': '%d',"
-                          "   'bus': '%s.0',"
-                          "   'id': 'usbdev%d'"
-                          "}}", port, hcd_id, port);
-    response = qmp(cmd);
-    g_free(cmd);
+    sprintf(id, "usbdev%d", port);

I know this fits, but do any of our compilers issue dumb warnings about possible buffer overflow? I guess we'll deal with that if someone reports a problem.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 6/9
#21 lore raw

Re: [Qemu-devel] [PATCH 6/9] tests/libqos/pci: Clean up string interpolation into QMP input

In reply to Markus Armbruster
Quoted 12 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

The case in qpci_plug_device_test() is a bit complicated: it interpolates several JSON object members, not just a value. Clean it up by passing them in as QDict rather than string, so we can leave interpolation to qmp() here and to qobject_from_jsonf() in callers.

---

tests/libqos/pci.c QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr) · line 412

Quoted 2 lines
tests/libqos/pci.c +2 -0
+    response = qmp("{'execute': 'device_add', 'arguments': %p }", args);
+

I like this; in fact, in my earlier series attempting to remove qobject_from_jsonf(), I heavily relied on %p being useful, to the point that I even added a helper function to make it easier (off-hand, it was something like qmp_execute(const char *command, QDict *arguments))

tests/virtio-blk-test.c static void pci_hotplug(void) · line 680

Quoted 6 lines
tests/virtio-blk-test.c +1 -0
@@ -674,6 +676,7 @@ static void pci_hotplug(void)
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP);
     }
+
     qtest_shutdown(qs);

Spurious whitespace addition?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 7/9
#27 lore raw

Re: [Qemu-devel] [PATCH 7/9] tests: Clean up wait for event

In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster

We still use hacks like qmp("") to wait for an event, even though we have qmp_eventwait() since commit 8fe941f, and qmp_eventwait_ref() since commit 7ffe312. Both commits neglected to convert all the existing hacks. Make up what they missed.

Bonus: gets rid of empty format strings. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 8/9
#30 lore raw

Re: [Qemu-devel] [PATCH 8/9] tests/libqtest: Clean up how we read the QMP greeting

In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster

qtest_init() still uses the qtest_qmp_discard_response(s, "") hack to receive the greeting, even though we have qtest_qmp_receive() since commit 66e0c7b. Put it to use.

Bonus: gets rid of an empty format string. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#33 lore raw

Re: [Qemu-devel] [PATCH 9/9] tests/libqtest: Enable compile-time format string checking

In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

qtest_qmp() & friends pass their format string and variable arguments to qobject_from_jsonv(). Unlike qobject_from_jsonv(), they aren't decorated with GCC_FMT_ATTR(). Fix that to get compile-time format string checking.

---
 tests/libqtest.h | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

Don't know how much of this to merge with my fixed version of 2/9. But matches what I had locally after my version 1, before I ripped it all back out again prior to posting my v2 when dealing with gcc fallout, that you've now corrected.

tests/libqtest.h void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...); · line 70

Quoted 7 lines
tests/libqtest.h +2 -1
@@ -65,7 +66,8 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
+QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);

This would fit on one line; any reason we need the line wrap?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH 2/9] qtest: Document calling conventions

In reply to Eric Blake
Quoted 34 lines · Eric Blake
Quoted 13 lines · Markus Armbruster

From: Eric Blake <[email protected]>

We have two flavors of vararg usage in qtest; make it clear that qmp() has different semantics than hmp(), and let the compiler enforce that hmp() is used correctly. However, qmp() (and friends) only accept a subset of printf flags look-alikes (namely, those that our JSON parser understands), and what is worse, qmp("true") (the JSON keyword 'true') is different from qmp("%s", "true") (the JSON string '"true"'), so marking those as printf-like would produce more harm from bogus warnings than it helps (we may have made a mistake in previously marking qobject_from_jsonf(), but this patch is not addressing that).

Quoted 3 lines

Message-Id: <[email protected]>

As you pointed out on the other thread,

tests/libqtest.h QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event); · line 143

Quoted 9 lines
tests/libqtest.h +1 -1
@@ -134,7 +137,7 @@ QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
 /**
  * qtest_hmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like vsprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  * QMP events are discarded.

I accidentally killed the attribute here,

Quoted 2 lines
(quoted diff) +0 -0
@@ -592,7 +598,7 @@ static inline QDict *qmp_eventwait_ref(const char *event)
 
Quoted 9 lines

tests/libqtest.h static inline QDict *qmp_eventwait_ref(const char *event) · line 604

Quoted 7 lines
tests/libqtest.h +1 -1
 /**
  * hmp:
- * @fmt...: HMP command to send to QEMU
+ * @fmt...: HMP command to send to QEMU, formats arguments like vsprintf().
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
  *

and here.

Putting them back is easier than updating commit message 1/9.

#12 lore raw

Re: [Qemu-devel] [PATCH 4/9] tests: Clean up string interpolation into QMP input (simple cases)

In reply to Eric Blake
Quoted 56 lines · Eric Blake
Quoted 30 lines · Markus Armbruster

When you build QMP input manually like this

    cmd = g_strdup_printf("{ 'execute': 'migrate',"
                          "'arguments': { 'uri': '%s' } }",
                          uri);
    rsp = qmp(cmd);
    g_free(cmd);

you're responsible for escaping the interpolated values for JSON. Not done here, and therefore works only for sufficiently nice @uri. For instance, if @uri contained a single "'", qobject_from_jsonv() would fail, qmp_fd_sendv() would misinterpret the failure as empty input and do nothing, and the test would hang waiting for a response that never comes.

Leaving interpolation into JSON to qmp() is more robust:

    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);

It's also more concise.

Clean up the simple cases where we interpolate exactly a JSON value.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---

tests/test-qga.c static void test_qga_sync_delimited(gconstpointer fix) · line 150

Quoted 3 lines
tests/test-qga.c +3 -0
+    qmp_fd_send(fixture->fd,
+                "\xff{'execute': 'guest-sync-delimited',"
+                " 'arguments': {'id': %u } }",

Ouch. Per 2/9:
* json-lexer.c (only understands '%((l|ll|I64)?d|[ipsf])').

That will need to be %d now. Or, more likely, we need to update the comments in json-lexer.c and in 2/9 (/me goes to read json-lexer.c...)

    /* escape */
    [IN_ESCAPE_LL] = {
        ['d'] = JSON_ESCAPE,
        ['u'] = JSON_ESCAPE,
    },
...
    [IN_ESCAPE] = {
        ['d'] = JSON_ESCAPE,
        ['i'] = JSON_ESCAPE,
        ['p'] = JSON_ESCAPE,
        ['s'] = JSON_ESCAPE,
        ['u'] = JSON_ESCAPE,
        ['f'] = JSON_ESCAPE,
        ['l'] = IN_ESCAPE_L,
        ['I'] = IN_ESCAPE_I,
Quoted 1 line

Aha, that 'd' should be '[du]' in all of the comments.

Yes. We really need %u, or else parse_escape() would call qnum_from_int() instead of qnum_from_uint().

I think the doc fix for json-lexer.c can be squashed in. Keeping it separate could simplify commit message writing, though. Your choice.

Quoted 18 lines

tests/test-qga.c static void test_qga_file_ops(gconstpointer fix) · line 413

Quoted 11 lines
tests/test-qga.c +3 -4
@@ -411,10 +408,10 @@ static void test_qga_file_ops(gconstpointer fix)
 
     enc = g_base64_encode(helloworld, sizeof(helloworld));
     /* write */
-    cmd = g_strdup_printf("{'execute': 'guest-file-write',"
-                          " 'arguments': { 'handle': %" PRId64 ","
-                          " 'buf-b64': '%s' } }", id, enc);
-    ret = qmp_fd(fixture->fd, cmd);
+    ret = qmp_fd(fixture->fd,
+                 "{'execute': 'guest-file-write',"
+                 " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }",

Ouch; you are reverting commit 1792d7d0. We tried hard to make json-lexer.c understand lots of different 64-bit format spellings, but we KNOW that we don't support MacOS (see commit 29a6731). We either need to beef up json-lexer.c to understand %qd, or get rid of JSON psuedo-strings, if we expect this to work; otherwise, you should use %lld and long long instead of PRId64 and uint64_t.

I forgot that PRId64 expands into non-standard crap on some systems.

Options:

1. Outlaw use of PRI macros, limit integer length modifiers for conversion specifiers "d" and "u" to "l" and "ll". When PRI macros creep in, the build breaks on hosts where they expand to anything else (hopefully, our CI will catch that). Interpolating int64_t values become a bit awkward.

Required work: fix my patches not to use PRId64, drop support for length modifier "I64" from parse_escape().

2. Support PRId64 and PRIu64, whatever their actual value may be.

a. Support all possible values. This is what we've tried before.
Nasty: fails only at run-time on hosts with sufficiently creative values.

Required work: add support for length modifier "q", to unbreak MacOS.

Optional work: try to turn the run-time failure into a compile- time failure, ideally in configure.

b. Support exactly the host's PRId64 and PRIu64 values.

Work required: replace support of "I64d" and "I64u" by support of PRId64 and PRIu64. Easy enough in parse_escape(), but not in json-lexer.c. We could perhaps have the lexer accept the shortest string that's followed by a conversion specifier as length modifier, then reject invalid length modifiers in a semantic action.

Optional work: try to simplify code that currently works around unavailability of PRId64 and PRIu64.

Preferences?

I like 2b, but I'm not sure I'll like the code implementing it. One way to find out...

Quoted 2 lines

Overall, I like the patch, but we need to fix the problems for the next round of this series.

Yes.

#18 lore raw

Re: [Qemu-devel] [PATCH 5/9] tests/libqos/usb: Clean up string interpolation into QMP input

In reply to Eric Blake
Quoted 7 lines · Eric Blake
Quoted 4 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the previous commit.

The case in usb_test_hotplug() slightly more complicated: it

s/()/() is/

Will fix.

Quoted 21 lines
Quoted 21 lines

interpolates *into* JSON values. Clean it up by building the values separately, so we can again leave interpolation to qmp().

---
 tests/libqos/usb.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
tests/libqos/usb.c +2 -1
index 0cdfaec..f88d4a6 100644
@@ -40,18 +40,20 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
 void usb_test_hotplug(const char *hcd_id, const int port,
                       void (*port_check)(void))
 {
+    char id[32];
+    char *bus;
     QDict *response;
-    char  *cmd;
 
Quoted 14 lines

tests/libqos/usb.c void uhci_port_test(struct qhc *hc, int port, uint16_t expect) · line 47

Quoted 10 lines
tests/libqos/usb.c +1 -9
-    cmd = g_strdup_printf("{'execute': 'device_add',"
-                          " 'arguments': {"
-                          "   'driver': 'usb-tablet',"
-                          "   'port': '%d',"
-                          "   'bus': '%s.0',"
-                          "   'id': 'usbdev%d'"
-                          "}}", port, hcd_id, port);
-    response = qmp(cmd);
-    g_free(cmd);
+    sprintf(id, "usbdev%d", port);

I know this fits, but do any of our compilers issue dumb warnings about possible buffer overflow? I guess we'll deal with that if someone reports a problem.

We do similar things elsewhere, just grep for sprintf.

Quoted 1 line

Thanks!

#22 lore raw

Re: [Qemu-devel] [PATCH 6/9] tests/libqos/pci: Clean up string interpolation into QMP input

In reply to Eric Blake
Quoted 21 lines · Eric Blake
Quoted 12 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

The case in qpci_plug_device_test() is a bit complicated: it interpolates several JSON object members, not just a value. Clean it up by passing them in as QDict rather than string, so we can leave interpolation to qmp() here and to qobject_from_jsonf() in callers.

---

tests/libqos/pci.c QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr) · line 412

Quoted 2 lines
tests/libqos/pci.c +2 -0
+    response = qmp("{'execute': 'device_add', 'arguments': %p }", args);
+

I like this; in fact, in my earlier series attempting to remove qobject_from_jsonf(), I heavily relied on %p being useful, to the point that I even added a helper function to make it easier (off-hand, it was something like qmp_execute(const char *command, QDict *arguments))

Quoted 8 lines

tests/virtio-blk-test.c static void pci_hotplug(void) · line 680

Quoted 6 lines
tests/virtio-blk-test.c +1 -0
@@ -674,6 +676,7 @@ static void pci_hotplug(void)
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         qpci_unplug_acpi_device_test("drv1", PCI_SLOT_HP);
     }
+
     qtest_shutdown(qs);

Spurious whitespace addition?

Is it spurious if the result looks better? ;)

There's a blank line after pci_test_start(), and that makes me want one before the matching qtest_shutdown().

Quoted 1 line

Thanks!

#13 lore raw

Re: [Qemu-devel] [PATCH 4/9] tests: Clean up string interpolation into QMP input (simple cases)

In reply to Markus Armbruster
Quoted 4 lines · Markus Armbruster

I forgot that PRId64 expands into non-standard crap on some systems.

Options:

Quoted 8 lines

1. Outlaw use of PRI macros, limit integer length modifiers for conversion specifiers "d" and "u" to "l" and "ll". When PRI macros creep in, the build breaks on hosts where they expand to anything else (hopefully, our CI will catch that). Interpolating int64_t values become a bit awkward.

Required work: fix my patches not to use PRId64, drop support for length modifier "I64" from parse_escape().

Yuck. (But motivation for my earlier series to nuke qobject_from_jsonf())

Quoted 7 lines

2. Support PRId64 and PRIu64, whatever their actual value may be.

a. Support all possible values. This is what we've tried before.
Nasty: fails only at run-time on hosts with sufficiently creative values.

Quoted 5 lines

Required work: add support for length modifier "q", to unbreak MacOS.

Optional work: try to turn the run-time failure into a compile- time failure, ideally in configure.

Accepts garbage (although -Wformat detection will help avoid the worst of it). You can't check string equality in the preprocessor, and you can't do things like "case PRId64[0]: case 'l':" to force compilation errors due to duplicate labels, but we CAN limit ourselves to the preprocessor and grep that output to see what PRId64 expands to. I'll see if I can come up with a configure check. Still, it may be easier to just fail configure and tell people to report the bug on their odd libc, at which point we update json-lexer.c and configure, than it is to try and reuse configure results in json-lexer.c (since the PRId64 string is not a constant width, it gets hard to code an exact value into our lexer state machine, but easier to code every reported value).

Quoted 9 lines

b. Support exactly the host's PRId64 and PRIu64 values.

Work required: replace support of "I64d" and "I64u" by support of PRId64 and PRIu64. Easy enough in parse_escape(), but not in json-lexer.c. We could perhaps have the lexer accept the shortest string that's followed by a conversion specifier as length modifier, then reject invalid length modifiers in a semantic action.

Interesting idea. I'm playing with it...

Quoted 8 lines

Optional work: try to simplify code that currently works around unavailability of PRId64 and PRIu64.

Preferences?

I like 2b, but I'm not sure I'll like the code implementing it. One way to find out...

In the lexer, widen the state machine to accept up to three unknown characters between % and d. Hmm, there's also the possibility of int64_t being mapped to %jd, in addition to our known culprits of %ld, %lld, %I64d, and %qd.

Quoted 5 lines
Quoted 3 lines

Overall, I like the patch, but we need to fix the problems for the next round of this series.

Yes.

At this point, I think I'll spin the next round. But it's not longer a 2.10 priority, so it may be a few days.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#14 lore raw

Re: [Qemu-devel] [PATCH 4/9] tests: Clean up string interpolation into QMP input (simple cases)

Quoted 11 lines · Eric Blake
Quoted 11 lines

2. Support PRId64 and PRIu64, whatever their actual value may be.

a. Support all possible values. This is what we've tried before.

b. Support exactly the host's PRId64 and PRIu64 values.

Preferences?

I like 2b, but I'm not sure I'll like the code implementing it. One way to find out...

I ended up implementing 2a in the C code, but 2b in the configure check to make sure our C code gets updated if we ever encounter any more oddballs. Of course, I don't have easy access to Mac OS, so I'm relying on others to test the patch for me; but once we get that patch going to our liking, I can rebase the rest of this series on top of it.

https://lists.gnu.org/archive/html/qemu-devel/2017-07/msg07039.html

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#15 lore raw

Re: [Qemu-devel] [PATCH 4/9] tests: Clean up string interpolation into QMP input (simple cases)

In reply to Eric Blake
Quoted 11 lines · Eric Blake
Quoted 10 lines · Markus Armbruster

I forgot that PRId64 expands into non-standard crap on some systems.

Options:

1. Outlaw use of PRI macros, limit integer length modifiers for conversion specifiers "d" and "u" to "l" and "ll". When PRI macros creep in, the build breaks on hosts where they expand to anything else (hopefully, our CI will catch that). Interpolating int64_t values become a bit awkward.

Quoted 9 lines
Quoted 2 lines

Required work: fix my patches not to use PRId64, drop support for length modifier "I64" from parse_escape().

Yuck. (But motivation for my earlier series to nuke qobject_from_jsonf())

Quoted 4 lines

2. Support PRId64 and PRIu64, whatever their actual value may be.

a. Support all possible values. This is what we've tried before.

Clarification: all the values we expect on the hosts we support. Currently "l", "ll", "I64" and "q".

Quoted 5 lines
Quoted 5 lines

Nasty: fails only at run-time on hosts with sufficiently creative values.

Required work: add support for length modifier "q", to unbreak MacOS.

We already support the other three.

Quoted 14 lines
Quoted 2 lines

Optional work: try to turn the run-time failure into a compile- time failure, ideally in configure.

Accepts garbage (although -Wformat detection will help avoid the worst of it). You can't check string equality in the preprocessor, and you can't do things like "case PRId64[0]: case 'l':" to force compilation errors due to duplicate labels, but we CAN limit ourselves to the preprocessor and grep that output to see what PRId64 expands to. I'll see if I can come up with a configure check. Still, it may be easier to just fail configure and tell people to report the bug on their odd libc, at which point we update json-lexer.c and configure, than it is to try and reuse configure results in json-lexer.c (since the PRId64 string is not a constant width, it gets hard to code an exact value into our lexer state machine, but easier to code every reported value).

Auto-configuring our handwritten lexer to recognize PRId64 and PRIu64 would be awkward. Certainly more awkward than substituting an auto-configured string into the input of flex. It's 2b anyway.

Quoted 17 lines
Quoted 8 lines

b. Support exactly the host's PRId64 and PRIu64 values.

Work required: replace support of "I64d" and "I64u" by support of PRId64 and PRIu64. Easy enough in parse_escape(), but not in json-lexer.c. We could perhaps have the lexer accept the shortest string that's followed by a conversion specifier as length modifier, then reject invalid length modifiers in a semantic action.

Interesting idea. I'm playing with it...

Quoted 6 lines

Optional work: try to simplify code that currently works around unavailability of PRId64 and PRIu64.

Preferences?

Quoted 7 lines
Quoted 2 lines

I like 2b, but I'm not sure I'll like the code implementing it. One way to find out...

In the lexer, widen the state machine to accept up to three unknown characters between % and d. Hmm, there's also the possibility of int64_t being mapped to %jd, in addition to our known culprits of %ld, %lld, %I64d, and %qd.

The lexer could accept any length modifier matching a suitable regular expression, leaving rejection of invalid ones to semantic actions.

The only hard restriction on non-standard length modifiers is that the resulting conversion specifications must be syntactically unambiguous, and the ones the standard specifies still mean the same.

Example: length modifier "f" to conversion specifier "d" is impossible, because "%fd" must be parsed as conversion specifier "%f" followed by ordinary character "d".

Example: similarly, length modifiers starting with a flag character, a digit, '.' or '*' are not possible because such a character must be parsed as flag, minimum field width or precision, respectively.

In practice, length modifiers starting with characters used elsewhere in conversion specifications, or containing conversion specifier characters would be too confusing.

Example: length modifier "qp" to conversion specifier "d" could technically be done as long as "q" isn't also made a length modifier to "p". But it would be nuts.

That leaves us with the regular expression [^-+ #0-9*.diouxXfFeEgGaAcspn%][^diouxXfFeEgGaAcspn%]*

I think we can rule out control characters, too.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Overall, I like the patch, but we need to fix the problems for the next round of this series.

Yes.

At this point, I think I'll spin the next round. But it's not longer a 2.10 priority, so it may be a few days.

No need to rush this work into 2.10.

Reviewed-by on v1 1/9
#2 lore raw

Re: [Qemu-devel] [PATCH 1/9] qtest: Avoid passing raw strings through hmp()

In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

From: Eric Blake <[email protected]>

The next patch will add __attribute__((__format__)) to hmp(), which in turn causes gcc to warn about non-literal format strings. Rather than risk an arbitrary string containing % being mis-handled, always pass variable strings along with a %s format. It also makes it easier to prove correctness locally, rather than auditing all the source strings.

Quoted 7 lines

Message-Id: <[email protected]>

---
 tests/test-hmp.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Reviewed-by on v1 2/9
#6 lore raw

Re: [Qemu-devel] [PATCH 2/9] qtest: Document calling conventions

In reply to Markus Armbruster
Quoted 13 lines · Markus Armbruster

From: Eric Blake <[email protected]>

We have two flavors of vararg usage in qtest; make it clear that qmp() has different semantics than hmp(), and let the compiler enforce that hmp() is used correctly. However, qmp() (and friends) only accept a subset of printf flags look-alikes (namely, those that our JSON parser understands), and what is worse, qmp("true") (the JSON keyword 'true') is different from qmp("%s", "true") (the JSON string '"true"'), so marking those as printf-like would produce more harm from bogus warnings than it helps (we may have made a mistake in previously marking qobject_from_jsonf(), but this patch is not addressing that).

Quoted 6 lines

Message-Id: <[email protected]>

---
 tests/libqtest.h | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
Reviewed-by on v1 3/9
#9 lore raw

Re: [Qemu-devel] [PATCH 3/9] tests: Pass literal format strings directly to qmp_FOO()

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

The qmp_FOO() take a printf-like format string. In a few places, we assign a string literal to a variable and pass that instead of simply passing the literal. Clean that up.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Quoted 5 lines
---
 tests/ahci-test.c | 4 +---
 tests/ide-test.c  | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
Reviewed-by on v1 5/9
#19 lore raw

Re: [Qemu-devel] [PATCH 5/9] tests/libqos/usb: Clean up string interpolation into QMP input

In reply to Markus Armbruster
Quoted 33 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the previous commit.

The case in usb_test_hotplug() slightly more complicated: it interpolates *into* JSON values. Clean it up by building the values separately, so we can again leave interpolation to qmp().

---
 tests/libqos/usb.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)
tests/libqos/usb.c +2 -9
index 0cdfaec..f88d4a6 100644
@@ -40,18 +40,20 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
 void usb_test_hotplug(const char *hcd_id, const int port,
                       void (*port_check)(void))
 {
+    char id[32];
+    char *bus;
     QDict *response;
-    char  *cmd;
 
-    cmd = g_strdup_printf("{'execute': 'device_add',"
-                          " 'arguments': {"
-                          "   'driver': 'usb-tablet',"
-                          "   'port': '%d',"
-                          "   'bus': '%s.0',"
-                          "   'id': 'usbdev%d'"
-                          "}}", port, hcd_id, port);
-    response = qmp(cmd);

tests/libqos/usb.c void uhci_port_test(struct qhc *hc, int port, uint16_t expect) · line 55

Quoted 10 lines
tests/libqos/usb.c +9 -1
-    g_free(cmd);
+    sprintf(id, "usbdev%d", port);
+    bus = g_strdup_printf("%s.0", hcd_id);
+    response = qmp("{'execute': 'device_add',"
+                   " 'arguments': {"
+                   "   'driver': 'usb-tablet',"
+                   "   'port': %s,"
+                   "   'bus': %s,"
+                   "   'id': %s"
+                   " }}", id + 6, bus, id);

Or: id + strlen("usbdev")

Reviewed-by on v1 6/9
#23 lore raw

Re: [Qemu-devel] [PATCH 6/9] tests/libqos/pci: Clean up string interpolation into QMP input

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

The case in qpci_plug_device_test() is a bit complicated: it interpolates several JSON object members, not just a value. Clean it up by passing them in as QDict rather than string, so we can leave interpolation to qmp() here and to qobject_from_jsonf() in callers.

Quoted 7 lines
---
 tests/ivshmem-test.c    | 10 +++++-----
 tests/libqos/pci.c      | 33 ++++++++++++++++++---------------
 tests/libqos/pci.h      |  2 +-
 tests/virtio-blk-test.c |  5 ++++-
 4 files changed, 28 insertions(+), 22 deletions(-)
Reviewed-by on v1 7/9
#28 lore raw

Re: [Qemu-devel] [PATCH 7/9] tests: Clean up wait for event

In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

We still use hacks like qmp("") to wait for an event, even though we have qmp_eventwait() since commit 8fe941f, and qmp_eventwait_ref() since commit 7ffe312. Both commits neglected to convert all the existing hacks. Make up what they missed.

Bonus: gets rid of empty format strings. A step towards compile-time format string checking without triggering -Wformat-zero-length.

Quoted 8 lines
---
 tests/boot-order-test.c   |  2 +-
 tests/libqos/pci-pc.c     |  6 +-----
 tests/tco-test.c          |  3 +--
 tests/usb-hcd-uhci-test.c |  6 +-----
 tests/usb-hcd-xhci-test.c | 12 ++----------
 tests/wdt_ib700-test.c    | 35 ++++++++++-------------------------
 6 files changed, 16 insertions(+), 48 deletions(-)

Nice!

Reviewed-by on v1 8/9
#31 lore raw

Re: [Qemu-devel] [PATCH 8/9] tests/libqtest: Clean up how we read the QMP greeting

In reply to Markus Armbruster
Quoted 4 lines · Markus Armbruster

qtest_init() still uses the qtest_qmp_discard_response(s, "") hack to receive the greeting, even though we have qtest_qmp_receive() since commit 66e0c7b. Put it to use.

Quoted 8 lines

Bonus: gets rid of an empty format string. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---
 tests/libqtest.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Reviewed-by on v1 9/9
#35 lore raw

Re: [Qemu-devel] [PATCH 9/9] tests/libqtest: Enable compile-time format string checking

In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

qtest_qmp() & friends pass their format string and variable arguments to qobject_from_jsonv(). Unlike qobject_from_jsonv(), they aren't decorated with GCC_FMT_ATTR(). Fix that to get compile-time format string checking.

---
 tests/libqtest.h | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)
#24 lore raw

Re: [Qemu-devel] [PATCH 6/9] tests/libqos/pci: Clean up string interpolation into QMP input

In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

The case in qpci_plug_device_test() is a bit complicated: it interpolates several JSON object members, not just a value. Clean it up by passing them in as QDict rather than string, so we can leave interpolation to qmp() here and to qobject_from_jsonf() in callers.

---

tests/ivshmem-test.c static void test_ivshmem_server_irq(void) · line 428

Quoted 8 lines
tests/ivshmem-test.c +2 -3
+    QObject *extra_args = qobject_from_jsonf("{ 'shm': '%s', 'size': '1M' }",
+                                             tmpshm);
 
     qtest_start("");
 
-    opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
-
-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);

Wait, 'size':'1M' works? I guess because it's a string, rather than a JSON number. One of our intentional design choices in QMP was to represent everything as bytes, rather than as suffixed numbers, since machine-generated code can easily generate bytes anywhere that humans prefer a suffixed number. But as you are not changing this interface, but merely refactoring how it is tested, it's more of a side comment than something that affects review.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#25 lore raw

Re: [Qemu-devel] [PATCH 6/9] tests/libqos/pci: Clean up string interpolation into QMP input

Quoted 21 lines · Eric Blake
Quoted 11 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

The case in qpci_plug_device_test() is a bit complicated: it interpolates several JSON object members, not just a value. Clean it up by passing them in as QDict rather than string, so we can leave interpolation to qmp() here and to qobject_from_jsonf() in callers.

---

tests/ivshmem-test.c static void test_ivshmem_server_irq(void) · line 428

Quoted 8 lines
tests/ivshmem-test.c +2 -3
+    QObject *extra_args = qobject_from_jsonf("{ 'shm': '%s', 'size': '1M' }",
+                                             tmpshm);
 
     qtest_start("");
 
-    opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
-
-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
Quoted 7 lines

Wait, 'size':'1M' works? I guess because it's a string, rather than a JSON number. One of our intentional design choices in QMP was to represent everything as bytes, rather than as suffixed numbers, since machine-generated code can easily generate bytes anywhere that humans prefer a suffixed number. But as you are not changing this interface, but merely refactoring how it is tested, it's more of a side comment than something that affects review.

Design mistake in legacy device "ivshmem". Fixed in "ivshmem-plain" and "ivshmem-doorbell", see commit 5400c02.

#34 lore raw

Re: [Qemu-devel] [PATCH 9/9] tests/libqtest: Enable compile-time format string checking

In reply to Eric Blake
Quoted 16 lines · Eric Blake
Quoted 9 lines · Markus Armbruster

qtest_qmp() & friends pass their format string and variable arguments to qobject_from_jsonv(). Unlike qobject_from_jsonv(), they aren't decorated with GCC_FMT_ATTR(). Fix that to get compile-time format string checking.

---
 tests/libqtest.h | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

Don't know how much of this to merge with my fixed version of 2/9. But matches what I had locally after my version 1, before I ripped it all back out again prior to posting my v2 when dealing with gcc fallout, that you've now corrected.

Quoted 9 lines

tests/libqtest.h void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...); · line 70

Quoted 7 lines
tests/libqtest.h +2 -1
@@ -65,7 +66,8 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
+QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);

This would fit on one line; any reason we need the line wrap?

Pretty sure I didn't have reasons. But I can make some up after the fact! Putting the attribute on a separate line can be a bit more legible. It can also confuse simple tools to recognize a definition instead of a declaration. Anyway, I don't really care about this line break.