Skip to contentSkip to conversation

vl: avoid SIGSEGV on invalid [accel] configuration

PATCH In review Paolo Bonzini 4 participants 11 messages Jan 17, 2023 – Jan 31, 2023

lore
Share & tools
lore
#0 lore raw

cover vl: avoid SIGSEGV on invalid [accel] configuration

While QEMU catches invalid -accel command line options:

    $ qemu-system-x86_64 -accel foo=bar
    Accelerators supported in QEMU binary:
    tcg
    xen
    kvm

the same is not true of configuration files, which instead crash. Patch 1 is the trivial fix, but writing a test is a bit more complex: there are no existing testcases where the qtest socket would not even start. So the series does the required refactoring and cleanup before adding tests for both valid and invalid [accel] sections in patch 4.

Paolo Bonzini (4):
vl: catch [accel] entry without accelerator
libqtest: split qtest_spawn_qemu function
libqtest: ensure waitpid() is only called once
readconfig-test: add test for accelerator configuration

 softmmu/vl.c                  |  15 +++-
 tests/qtest/libqtest.c        | 160 +++++++++++++++++++++-------------
 tests/qtest/libqtest.h        |  12 +++
 tests/qtest/readconfig-test.c |  45 ++++++++--
 4 files changed, 156 insertions(+), 76 deletions(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/4 vl: catch [accel] entry without accelerator

While QEMU catches invalid -accel command line options:

    $ qemu-system-x86_64 -accel foo=bar
    Accelerators supported in QEMU binary:
    tcg
    xen
    kvm

the same is not true of configuration files, which instead crash. Avoid a SIGSEGV and return an error instead.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1439

---
 softmmu/vl.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
softmmu/vl.c +11 -4
index 9bd0e52d016a..b6deaee52da4 100644
@@ -2204,14 +2204,18 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
     int ret;
     bool qtest_with_kvm;
 
+    if (!acc) {
+        error_setg(&error_fatal, QERR_MISSING_PARAMETER, "accel");
+        goto bad;
+    }
+
     qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
 
     if (!ac) {
-        *p_init_failed = true;
         if (!qtest_with_kvm) {
             error_report("invalid accelerator %s", acc);
         }
-        return 0;
+        goto bad;
     }
     accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
     object_apply_compat_props(OBJECT(accel));
@@ -2221,14 +2225,17 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
 
     ret = accel_init_machine(accel, current_machine);
     if (ret < 0) {
-        *p_init_failed = true;
         if (!qtest_with_kvm || ret != -ENOENT) {
             error_report("failed to initialize %s: %s", acc, strerror(-ret));
         }
-        return 0;
+        goto bad;
     }
 
     return 1;
+
+bad:
+    *p_init_failed = true;
+    return 0;
 }
 
 static void configure_accelerators(const char *progname)
Signature · 1 line
2.38.1
#2 lore raw

Re: [PATCH 1/4] vl: catch [accel] entry without accelerator

Quoted 23 lines · Paolo Bonzini

While QEMU catches invalid -accel command line options:

     $ qemu-system-x86_64 -accel foo=bar
     Accelerators supported in QEMU binary:
     tcg
     xen
     kvm

the same is not true of configuration files, which instead crash. Avoid a SIGSEGV and return an error instead.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1439

---
  softmmu/vl.c | 15 +++++++++++----
  1 file changed, 11 insertions(+), 4 deletions(-)
softmmu/vl.c +0 -0
index 9bd0e52d016a..b6deaee52da4 100644

softmmu/vl.c static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp) · line 2208

Quoted 5 lines
softmmu/vl.c +2 -0
@@ -2204,14 +2204,18 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
      int ret;
      bool qtest_with_kvm;
 
+    if (!acc) {
+        error_setg(&error_fatal, QERR_MISSING_PARAMETER, "accel");

s/&error_fatal/errp/ ?

Quoted 33 lines
softmmu/vl.c +9 -4
+        goto bad;
+    }
+
      qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
 
      if (!ac) {
-        *p_init_failed = true;
          if (!qtest_with_kvm) {
              error_report("invalid accelerator %s", acc);
          }
-        return 0;
+        goto bad;
      }
      accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
      object_apply_compat_props(OBJECT(accel));
@@ -2221,14 +2225,17 @@ static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
 
      ret = accel_init_machine(accel, current_machine);
      if (ret < 0) {
-        *p_init_failed = true;
          if (!qtest_with_kvm || ret != -ENOENT) {
              error_report("failed to initialize %s: %s", acc, strerror(-ret));
          }
-        return 0;
+        goto bad;
      }
 
      return 1;
+
+bad:
+    *p_init_failed = true;
+    return 0;
  }
#3 lore raw

PATCH 2/4 libqtest: split qtest_spawn_qemu function

In reply to Paolo Bonzini

In order to create a function that allows testing of invalid command lines, extract the parts of qtest_init_without_qmp_handshake that do not require any successful set up of sockets.

---
 tests/qtest/libqtest.c | 103 ++++++++++++++++++++++-------------------
 1 file changed, 55 insertions(+), 48 deletions(-)
tests/qtest/libqtest.c +55 -48
index 5cb38f90da19..4d9cf919b2f7 100644
@@ -342,60 +342,25 @@ static pid_t qtest_create_process(char *cmd)
 }
 #endif /* _WIN32 */
 
-QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
+static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char *fmt, ...)
 {
-    QTestState *s;
-    int sock, qmpsock, i;
-    gchar *socket_path;
-    gchar *qmp_socket_path;
-    gchar *command;
-    const char *qemu_binary = qtest_qemu_binary();
+    va_list ap;
+    QTestState *s = g_new0(QTestState, 1);
     const char *trace = g_getenv("QTEST_TRACE");
     g_autofree char *tracearg = trace ?
         g_strdup_printf("-trace %s ", trace) : g_strdup("");
+    g_autoptr(GString) command = g_string_new("");
 
-    s = g_new(QTestState, 1);
-
-    socket_path = g_strdup_printf("%s/qtest-%d.sock",
-                                  g_get_tmp_dir(), getpid());
-    qmp_socket_path = g_strdup_printf("%s/qtest-%d.qmp",
-                                      g_get_tmp_dir(), getpid());
-
-    /* It's possible that if an earlier test run crashed it might
-     * have left a stale unix socket lying around. Delete any
-     * stale old socket to avoid spurious test failures with
-     * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
-     */
-    unlink(socket_path);
-    unlink(qmp_socket_path);
-
-    socket_init();
-    sock = init_socket(socket_path);
-    qmpsock = init_socket(qmp_socket_path);
-
-    qtest_client_set_rx_handler(s, qtest_client_socket_recv_line);
-    qtest_client_set_tx_handler(s, qtest_client_socket_send);
+    va_start(ap, fmt);
+    g_string_append_printf(command, CMD_EXEC "%s %s",
+                           qtest_qemu_binary(), tracearg);
+    g_string_append_vprintf(command, fmt, ap);
+    va_end(ap);
 
     qtest_add_abrt_handler(kill_qemu_hook_func, s);
 
-    command = g_strdup_printf(CMD_EXEC "%s %s"
-                              "-qtest unix:%s "
-                              "-qtest-log %s "
-                              "-chardev socket,path=%s,id=char0 "
-                              "-mon chardev=char0,mode=control "
-                              "-display none "
-                              "%s"
-                              " -accel qtest",
-                              qemu_binary, tracearg, socket_path,
-                              getenv("QTEST_LOG") ? DEV_STDERR : DEV_NULL,
-                              qmp_socket_path,
-                              extra_args ?: "");
+    g_test_message("starting QEMU: %s", command->str);
 
-    g_test_message("starting QEMU: %s", command);
-
-    s->pending_events = NULL;
-    s->wstatus = 0;
-    s->expected_status = 0;
 #ifndef _WIN32
     s->qemu_pid = fork();
     if (s->qemu_pid == 0) {
@@ -416,14 +381,56 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
         if (!g_setenv("QEMU_AUDIO_DRV", "none", true)) {
             exit(1);
         }
-        execlp("/bin/sh", "sh", "-c", command, NULL);
+        execlp("/bin/sh", "sh", "-c", command->str, NULL);
         exit(1);
     }
 #else
-    s->qemu_pid = qtest_create_process(command);
+    s->qemu_pid = qtest_create_process(command->str);
 #endif /* _WIN32 */
 
-    g_free(command);
+    return s;
+}
+
+QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
+{
+    QTestState *s;
+    int sock, qmpsock, i;
+    gchar *socket_path;
+    gchar *qmp_socket_path;
+
+    socket_path = g_strdup_printf("%s/qtest-%d.sock",
+                                  g_get_tmp_dir(), getpid());
+    qmp_socket_path = g_strdup_printf("%s/qtest-%d.qmp",
+                                      g_get_tmp_dir(), getpid());
+
+    /*
+     * It's possible that if an earlier test run crashed it might
+     * have left a stale unix socket lying around. Delete any
+     * stale old socket to avoid spurious test failures with
+     * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
+     */
+    unlink(socket_path);
+    unlink(qmp_socket_path);
+
+    socket_init();
+    sock = init_socket(socket_path);
+    qmpsock = init_socket(qmp_socket_path);
+
+    s = qtest_spawn_qemu("-qtest unix:%s "
+                         "-qtest-log %s "
+                         "-chardev socket,path=%s,id=char0 "
+                         "-mon chardev=char0,mode=control "
+                         "-display none "
+                         "%s"
+                         " -accel qtest",
+                         socket_path,
+                         getenv("QTEST_LOG") ? DEV_STDERR : DEV_NULL,
+                         qmp_socket_path,
+                         extra_args ?: "");
+
+    qtest_client_set_rx_handler(s, qtest_client_socket_recv_line);
+    qtest_client_set_tx_handler(s, qtest_client_socket_send);
+
     s->fd = socket_accept(sock);
     if (s->fd >= 0) {
         s->qmp_fd = socket_accept(qmpsock);
Signature · 1 line
2.38.1
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] libqtest: split qtest_spawn_qemu function

Quoted 8 lines · Paolo Bonzini

In order to create a function that allows testing of invalid command lines, extract the parts of qtest_init_without_qmp_handshake that do not require any successful set up of sockets.

---
  tests/qtest/libqtest.c | 103 ++++++++++++++++++++++-------------------
  1 file changed, 55 insertions(+), 48 deletions(-)
#5 lore raw

Re: [PATCH 2/4] libqtest: split qtest_spawn_qemu function

In reply to Paolo Bonzini
Quoted 14 lines · Paolo Bonzini

In order to create a function that allows testing of invalid command lines, extract the parts of qtest_init_without_qmp_handshake that do not require any successful set up of sockets.

---
  tests/qtest/libqtest.c | 103 ++++++++++++++++++++++-------------------
  1 file changed, 55 insertions(+), 48 deletions(-)
tests/qtest/libqtest.c +0 -0
index 5cb38f90da19..4d9cf919b2f7 100644

tests/qtest/libqtest.c static pid_t qtest_create_process(char *cmd) · line 345

Quoted 5 lines
tests/qtest/libqtest.c +1 -1
@@ -342,60 +342,25 @@ static pid_t qtest_create_process(char *cmd)
  }
  #endif /* _WIN32 */
 
-QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
+static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char *fmt, ...)

Gluing the "*" to G_GNUC_PRINTF looks weird, could you maybe put that G_GNUC_PRINTF somewhere else?

Thomas

#6 lore raw

PATCH 3/4 libqtest: ensure waitpid() is only called once

In reply to Paolo Bonzini

If a test aborts after qtest_wait_qemu() is called, the SIGABRT hooks are still in place and waitpid() is called again. The second time it is called, the process does not exist anymore and the system call fails.

Move the s->qemu_pid = -1 assignment to qtest_wait_qemu() to make it idempotent, and anyway remove the SIGABRT hook as well to avoid that qtest_check_status() is called twice. Because of the extra call, qtest_remove_abrt_handler() now has to be made idempotent as well.

---
 tests/qtest/libqtest.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)
tests/qtest/libqtest.c +20 -9
index 4d9cf919b2f7..64ba98bc5853 100644
@@ -156,6 +156,7 @@ bool qtest_probe_child(QTestState *s)
         CloseHandle((HANDLE)pid);
 #endif
         s->qemu_pid = -1;
+        qtest_remove_abrt_handler(s);
     }
     return false;
 }
@@ -167,6 +168,8 @@ void qtest_set_expected_status(QTestState *s, int status)
 
 static void qtest_check_status(QTestState *s)
 {
+    assert (s->qemu_pid == -1);
+
     /*
      * Check whether qemu exited with expected exit status; anything else is
      * fishy and should be logged with as much detail as possible.
@@ -200,20 +203,24 @@ static void qtest_check_status(QTestState *s)
 
 void qtest_wait_qemu(QTestState *s)
 {
+    if (s->qemu_pid != -1) {
 #ifndef _WIN32
-    pid_t pid;
+        pid_t pid;
 
-    pid = RETRY_ON_EINTR(waitpid(s->qemu_pid, &s->wstatus, 0));
-    assert(pid == s->qemu_pid);
+        pid = RETRY_ON_EINTR(waitpid(s->qemu_pid, &s->wstatus, 0));
+        g_assert_cmpint(pid, ==, s->qemu_pid);
 #else
-    DWORD ret;
+        DWORD ret;
 
-    ret = WaitForSingleObject((HANDLE)s->qemu_pid, INFINITE);
-    assert(ret == WAIT_OBJECT_0);
-    GetExitCodeProcess((HANDLE)s->qemu_pid, &s->exit_code);
-    CloseHandle((HANDLE)s->qemu_pid);
+        ret = WaitForSingleObject((HANDLE)s->qemu_pid, INFINITE);
+        assert(ret == WAIT_OBJECT_0);
+        GetExitCodeProcess((HANDLE)s->qemu_pid, &s->exit_code);
+        CloseHandle((HANDLE)s->qemu_pid);
 #endif
 
+        s->qemu_pid = -1;
+        qtest_remove_abrt_handler(s);
+    }
     qtest_check_status(s);
 }
 
@@ -227,7 +234,6 @@ void qtest_kill_qemu(QTestState *s)
         TerminateProcess((HANDLE)s->qemu_pid, s->expected_status);
 #endif
         qtest_wait_qemu(s);
-        s->qemu_pid = -1;
         return;
     }
 
@@ -289,6 +295,11 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data)
 void qtest_remove_abrt_handler(void *data)
 {
     GHook *hook = g_hook_find_data(&abrt_hooks, TRUE, data);
+
+    if (!hook) {
+        return;
+    }
+
     g_hook_destroy_link(&abrt_hooks, hook);
 
     /* Uninstall SIGABRT handler on last instance */
Signature · 1 line
2.38.1
Reviewed-by on v1 3/4
#7 lore raw

Re: [PATCH 3/4] libqtest: ensure waitpid() is only called once

Quoted 9 lines · Paolo Bonzini

If a test aborts after qtest_wait_qemu() is called, the SIGABRT hooks are still in place and waitpid() is called again. The second time it is called, the process does not exist anymore and the system call fails.

Move the s->qemu_pid = -1 assignment to qtest_wait_qemu() to make it idempotent, and anyway remove the SIGABRT hook as well to avoid that qtest_check_status() is called twice. Because of the extra call, qtest_remove_abrt_handler() now has to be made idempotent as well.

Quoted 4 lines
---
  tests/qtest/libqtest.c | 29 ++++++++++++++++++++---------
  1 file changed, 20 insertions(+), 9 deletions(-)
#8 lore raw

PATCH 4/4 readconfig-test: add test for accelerator configuration

In reply to Paolo Bonzini

Test that invalid configurations do not cause a SIGSEGV, and cover a valid configuration as well.

---
 tests/qtest/libqtest.c        | 28 +++++++++++++++++-----
 tests/qtest/libqtest.h        | 12 ++++++++++
 tests/qtest/readconfig-test.c | 45 ++++++++++++++++++++++++++++-------
 3 files changed, 70 insertions(+), 15 deletions(-)
tests/qtest/libqtest.c +22 -6
index 64ba98bc5853..53d766fe3fa5 100644
@@ -402,6 +402,26 @@ static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char *fmt, ...)
     return s;
 }
 
+QTestState *G_GNUC_PRINTF(1, 0) qtest_init_bare(const char *args)
+{
+    QTestState *s = qtest_spawn_qemu("%s", args);
+
+    /*
+     * Stopping QEMU for debugging is not supported on Windows.
+     *
+     * Using DebugActiveProcess() API can suspend the QEMU process,
+     * but gdb cannot attach to the process. Using the undocumented
+     * NtSuspendProcess() can suspend the QEMU process and gdb can
+     * attach to the process, but gdb cannot resume it.
+     */
+#ifndef _WIN32
+    if (getenv("QTEST_STOP")) {
+        kill(s->qemu_pid, SIGSTOP);
+    }
+#endif
+    return s;
+}
+
 QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
 {
     QTestState *s;
@@ -459,12 +479,8 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     }
 
     /*
-     * Stopping QEMU for debugging is not supported on Windows.
-     *
-     * Using DebugActiveProcess() API can suspend the QEMU process,
-     * but gdb cannot attach to the process. Using the undocumented
-     * NtSuspendProcess() can suspend the QEMU process and gdb can
-     * attach to the process, but gdb cannot resume it.
+     * Stopping QEMU for debugging is not supported on Windows;
+     * see qtest_init_bare for more information.
      */
 #ifndef _WIN32
     if (getenv("QTEST_STOP")) {
tests/qtest/libqtest.h +12 -0
index fcf1c3c3b36f..7ca7df26a2c0 100644
@@ -23,6 +23,18 @@
 
 typedef struct QTestState QTestState;
 
+/**
+ * qtest_init_bare:
+ * @extra_args: other arguments to pass to QEMU.  CAUTION: these
+ * arguments are subject to word splitting and shell evaluation.
+ *
+ * Return a QTestState instance without automatically creating any
+ * sockets for QMP and qtest communication.
+ *
+ * Returns: #QTestState instance.
+ */
+QTestState *qtest_init_bare(const char *args);
+
 /**
  * qtest_initf:
  * @fmt: Format for creating other arguments to pass to QEMU, formatted
tests/qtest/readconfig-test.c +36 -9

74 lines not on this page · raw message

Signature · 1 line
2.38.1
#9 lore raw

Re: [PATCH 4/4] readconfig-test: add test for accelerator configuration

Quoted 15 lines · Paolo Bonzini

Test that invalid configurations do not cause a SIGSEGV, and cover a valid configuration as well.

---
  tests/qtest/libqtest.c        | 28 +++++++++++++++++-----
  tests/qtest/libqtest.h        | 12 ++++++++++
  tests/qtest/readconfig-test.c | 45 ++++++++++++++++++++++++++++-------
  3 files changed, 70 insertions(+), 15 deletions(-)
tests/qtest/libqtest.c +0 -0
index 64ba98bc5853..53d766fe3fa5 100644

tests/qtest/libqtest.c static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char *fmt, ...) · line 405

Quoted 4 lines
tests/qtest/libqtest.c +1 -0
@@ -402,6 +402,26 @@ static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char *fmt, ...)
      return s;
  }
 
+QTestState *G_GNUC_PRINTF(1, 0) qtest_init_bare(const char *args)

I think you don't need the G_GNUC_PRINTF here, do you?

Thomas

#10 lore raw

Re: [PATCH 4/4] readconfig-test: add test for accelerator configuration

Quoted 11 lines · Thomas Huth
Quoted 10 lines · Paolo Bonzini

Test that invalid configurations do not cause a SIGSEGV, and cover a valid configuration as well.

---
  tests/qtest/libqtest.c        | 28 +++++++++++++++++----- tests/qtest/libqtest.h        | 12 ++++++++++ tests/qtest/readconfig-test.c | 45 ++++++++++++++++++++++++++++-------

  3 files changed, 70 insertions(+), 15 deletions(-)
Quoted 11 lines

tests/qtest/libqtest.c static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char · line 405

Quoted 9 lines
tests/qtest/libqtest.c +1 -0
index 64ba98bc5853..53d766fe3fa5 100644
@@ -402,6 +402,26 @@ static QTestState *G_GNUC_PRINTF(1, 0) qtest_spawn_qemu(const char 
 *fmt, ...)
       return s;
   }
+QTestState *G_GNUC_PRINTF(1, 0) qtest_init_bare(const char *args)

I think you don't need the G_GNUC_PRINTF here, do you?

Indeed, it is incorrect.

r~