Skip to contentSkip to conversation

Fix some crashes when using -object

PATCH In review Eduardo Habkost 7 participants 12 messages Oct 8, 2020 – Oct 12, 2020 CAN network layer

lore
Share & tools
lore
#0 lore raw

cover Fix some crashes when using -object

Fix two crashes when using -object with no extra arguments, and add a acceptance test case to detect similar crashes in the future.

Eduardo Habkost (3):
authz-list-file: Fix crash when filename is not set
can-host-socketcan: Fix crash when 'if' option is not set tests/acceptance: Test case for detecting -object crashes

 authz/listfile.c                  |  5 ++++
 net/can/can_socketcan.c           |  5 ++++
 tests/acceptance/object_option.py | 49 +++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+)
 create mode 100644 tests/acceptance/object_option.py
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/3 authz-list-file: Fix crash when filename is not set

Fix the following crash:

  $ qemu-system-x86_64 -object authz-list-file,id=obj0
  qemu-system-x86_64: -object authz-list-file,id=obj0: GLib: g_file_get_contents: assertion 'filename != NULL' failed
  Segmentation fault (core dumped)

---
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: [email protected]

---
 authz/listfile.c | 5 +++++
 1 file changed, 5 insertions(+)
authz/listfile.c +5 -0
index cd6163aa40..aaf930453d 100644
@@ -122,6 +122,11 @@ qauthz_list_file_complete(UserCreatable *uc, Error **errp)
     QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(uc);
     gchar *dir = NULL, *file = NULL;
 
+    if (!fauthz->filename) {
+        error_setg(errp, "filename not provided");
+        return;
+    }
+
     fauthz->list = qauthz_list_file_load(fauthz, errp);
 
     if (!fauthz->refresh) {
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 1/3] authz-list-file: Fix crash when filename is not set

Quoted 10 lines · Eduardo Habkost

Fix the following crash:

  $ qemu-system-x86_64 -object authz-list-file,id=obj0
  qemu-system-x86_64: -object authz-list-file,id=obj0: GLib: g_file_get_contents: assertion 'filename != NULL' failed
  Segmentation fault (core dumped)

---
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: [email protected]

Out of curiosity, are those notes intended for git-publish? (If so, I didn't realize it would read them).

Quoted 9 lines
---
 authz/listfile.c | 5 +++++
 1 file changed, 5 insertions(+)
authz/listfile.c +0 -0
index cd6163aa40..aaf930453d 100644

authz/listfile.c qauthz_list_file_complete(UserCreatable *uc, Error **errp) · line 126

Quoted 5 lines
authz/listfile.c +2 -0
@@ -122,6 +122,11 @@ qauthz_list_file_complete(UserCreatable *uc, Error **errp)
     QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(uc);
     gchar *dir = NULL, *file = NULL;
 
+    if (!fauthz->filename) {
+        error_setg(errp, "filename not provided");

Nitpick: all other similar error messages start capitalized.

- Cleber.

Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] authz-list-file: Fix crash when filename is not set

In reply to Eduardo Habkost
Quoted 6 lines · Eduardo Habkost

Fix the following crash:

  $ qemu-system-x86_64 -object authz-list-file,id=obj0
  qemu-system-x86_64: -object authz-list-file,id=obj0: GLib: g_file_get_contents: assertion 'filename != NULL' failed
  Segmentation fault (core dumped)
Quoted 7 lines

---
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: [email protected]

---
 authz/listfile.c | 5 +++++
 1 file changed, 5 insertions(+)

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] authz-list-file: Fix crash when filename is not set

In reply to Eduardo Habkost
Quoted 21 lines · Eduardo Habkost

Fix the following crash:

   $ qemu-system-x86_64 -object authz-list-file,id=obj0
   qemu-system-x86_64: -object authz-list-file,id=obj0: GLib: g_file_get_contents: assertion 'filename != NULL' failed
   Segmentation fault (core dumped)

---
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: [email protected]

---
  authz/listfile.c | 5 +++++
  1 file changed, 5 insertions(+)
authz/listfile.c +0 -0
index cd6163aa40..aaf930453d 100644
@@ -122,6 +122,11 @@ qauthz_list_file_complete(UserCreatable *uc, Error **errp)
      QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(uc);

authz/listfile.c qauthz_list_file_complete(UserCreatable *uc, Error **errp) · line 132

Quoted 10 lines
authz/listfile.c +5 -0
      gchar *dir = NULL, *file = NULL;
 
+    if (!fauthz->filename) {
+        error_setg(errp, "filename not provided");
+        return;
+    }
+
      fauthz->list = qauthz_list_file_load(fauthz, errp);
 
      if (!fauthz->refresh) {
Reviewed-by on v1 1/3
#5 lore raw

Re: [PATCH 1/3] authz-list-file: Fix crash when filename is not set

In reply to Eduardo Habkost
Quoted 8 lines · Eduardo Habkost <[email protected]> 于2020年10月9日周五 上午4:28写道:

Fix the following crash:

  $ qemu-system-x86_64 -object authz-list-file,id=obj0
  qemu-system-x86_64: -object authz-list-file,id=obj0: GLib: g_file_get_contents: assertion 'filename != NULL' failed
  Segmentation fault (core dumped)
Quoted 27 lines

---
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: [email protected]

---
 authz/listfile.c | 5 +++++
 1 file changed, 5 insertions(+)
authz/listfile.c +5 -1
index cd6163aa40..aaf930453d 100644
@@ -122,6 +122,11 @@ qauthz_list_file_complete(UserCreatable *uc, Error **errp)
     QAuthZListFile *fauthz = QAUTHZ_LIST_FILE(uc);
     gchar *dir = NULL, *file = NULL;
 
+    if (!fauthz->filename) {
+        error_setg(errp, "filename not provided");
+        return;
+    }
+
     fauthz->list = qauthz_list_file_load(fauthz, errp);
 
     if (!fauthz->refresh) {
--

2.26.2

#6 lore raw

PATCH 2/3 can-host-socketcan: Fix crash when 'if' option is not set

In reply to Eduardo Habkost

Fix the following crash:

  $ qemu-system-x86_64 -object can-host-socketcan,id=obj0
  Segmentation fault (core dumped)

---
Cc: Pavel Pisa <[email protected]>
Cc: Vikram Garhwal <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: [email protected]

---
 net/can/can_socketcan.c | 5 +++++
 1 file changed, 5 insertions(+)
net/can/can_socketcan.c +5 -0
index 92b1f79385..4b68f60c6b 100644
@@ -194,6 +194,11 @@ static void can_host_socketcan_connect(CanHostState *ch, Error **errp)
     struct sockaddr_can addr;
     struct ifreq ifr;
 
+    if (!c->ifname) {
+        error_setg(errp, "'if' property not set");
+        return;
+    }
+
     /* open socket */
     s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
     if (s < 0) {
Signature · 1 line
2.26.2
Acked-by on v1 2/3
#7 lore raw

Re: [PATCH 2/3] can-host-socketcan: Fix crash when 'if' option is not set

Thanks for catching missing test

Quoted 21 lines · On Thursday 08 of October 2020 22:27:12 Eduardo Habkost wrote:

Fix the following crash:

  $ qemu-system-x86_64 -object can-host-socketcan,id=obj0
  Segmentation fault (core dumped)

---
Cc: Pavel Pisa <[email protected]>
Cc: Vikram Garhwal <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: [email protected]

---
 net/can/can_socketcan.c | 5 +++++
 1 file changed, 5 insertions(+)
net/can/can_socketcan.c +0 -0
index 92b1f79385..4b68f60c6b 100644
@@ -194,6 +194,11 @@ static void can_host_socketcan_connect(CanHostState
 *ch, Error **errp) struct sockaddr_can addr;

net/can/can_socketcan.c static void can_host_socketcan_connect(CanHostState · line 204

Quoted 10 lines
net/can/can_socketcan.c +5 -0
     struct ifreq ifr;
 
+    if (!c->ifname) {
+        error_setg(errp, "'if' property not set");
+        return;
+    }
+
     /* open socket */
     s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
     if (s < 0) {
Signature · 8 lines
                Pavel Pisa
    e-mail:     [email protected]
    Department of Control Engineering FEE CVUT
    Karlovo namesti 13, 121 35, Prague 2
    university: http://dce.fel.cvut.cz/
    personal:   http://cmp.felk.cvut.cz/~pisa
    projects:   https://www.openhub.net/accounts/ppisa
    CAN related:http://canbus.pages.fel.cvut.cz/
Reviewed-by on v1 2/3
#8 lore raw

Re: [PATCH 2/3] can-host-socketcan: Fix crash when 'if' option is not set

In reply to Eduardo Habkost
Quoted 7 lines · Eduardo Habkost <[email protected]> 于2020年10月9日周五 上午4:31写道:

Fix the following crash:

  $ qemu-system-x86_64 -object can-host-socketcan,id=obj0
  Segmentation fault (core dumped)
Quoted 29 lines

---
Cc: Pavel Pisa <[email protected]>
Cc: Vikram Garhwal <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: [email protected]

---
 net/can/can_socketcan.c | 5 +++++
 1 file changed, 5 insertions(+)
net/can/can_socketcan.c +5 -1
index 92b1f79385..4b68f60c6b 100644
@@ -194,6 +194,11 @@ static void can_host_socketcan_connect(CanHostState *ch, Error **errp)
     struct sockaddr_can addr;
     struct ifreq ifr;
 
+    if (!c->ifname) {
+        error_setg(errp, "'if' property not set");
+        return;
+    }
+
     /* open socket */
     s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
     if (s < 0) {
--

2.26.2

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

Re: [PATCH 2/3] can-host-socketcan: Fix crash when 'if' option is not set

In reply to Eduardo Habkost
Quoted 6 lines · Eduardo Habkost

Fix the following crash:

  $ qemu-system-x86_64 -object can-host-socketcan,id=obj0
  Segmentation fault (core dumped)
Quoted 28 lines

---
Cc: Pavel Pisa <[email protected]>
Cc: Vikram Garhwal <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: [email protected]

---
 net/can/can_socketcan.c | 5 +++++
 1 file changed, 5 insertions(+)
net/can/can_socketcan.c +5 -1
index 92b1f79385..4b68f60c6b 100644
@@ -194,6 +194,11 @@ static void can_host_socketcan_connect(CanHostState *ch, Error **errp)
     struct sockaddr_can addr;
     struct ifreq ifr;
 
+    if (!c->ifname) {
+        error_setg(errp, "'if' property not set");
+        return;
+    }
+
     /* open socket */
     s = qemu_socket(PF_CAN, SOCK_RAW, CAN_RAW);
     if (s < 0) {
--

2.26.2

#10 lore raw

PATCH 3/3 tests/acceptance: Test case for detecting -object crashes

In reply to Eduardo Habkost

Add a simple test case that will run QEMU directly (without QMP) just to check for crashes when using -object.

---
Cc: Cleber Rosa <[email protected]>
Cc: "Philippe Mathieu-Daudé" <[email protected]>
Cc: Wainer dos Santos Moschetta <[email protected]>
Cc: [email protected]

---
 tests/acceptance/object_option.py | 49 +++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 tests/acceptance/object_option.py
tests/acceptance/object_option.py new file +49 -0
new file mode 100644
index 0000000000..2b8bd00db1
@@ -0,0 +1,49 @@
+# Copyright (c) 2020 Red Hat, Inc.
+#
+# Author:
+#  Eduardo Habkost <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later.  See the COPYING file in the top-level directory.
+
+import avocado_qemu
+import subprocess
+import shlex
+
+class ObjectOption(avocado_qemu.Test):
+    """Check if ``-object`` option behaves as expected"""
+
+    def run(self, cmd, *args, **kwargs):
+        cmdstr = ' '.join(shlex.quote(c) for c in cmd)
+        self.log.info("Command: %s", cmdstr)
+        return subprocess.run(cmd, encoding='utf-8', *args, **kwargs)
+
+    def devices(self):
+        out = self.run([self.qemu_bin, '-object', 'help'],
+                       check=True, stdout=subprocess.PIPE).stdout
+        lines = out.split('\n')
+        return [l.strip() for l in lines[1:] if l.strip()]
+
+    def test_help(self):
+        """Check if ``-object ...,help`` behaves as expected"""
+        for device in self.devices():
+            self.run([self.qemu_bin, '-object', '%s,help' % (device)],
+                     check=True,
+                     stdout=subprocess.DEVNULL)
+
+    def test_crash(self):
+        """Check for crashes when using ``-object ...``"""
+        for device in self.devices():
+            r = self.run([self.qemu_bin, '-object',
+                                '%s,id=obj0' % (device),
+                                '-monitor', 'stdio'],
+                         input='quit\n',
+                         stdout=subprocess.DEVNULL,
+                         stderr=subprocess.PIPE)
+            if r.returncode not in (0, 1):
+                self.log.warn("QEMU stderr: %s", r.stderr)
+                self.log.warn("QEMU exit code: %d", r.returncode)
+                if r.returncode < 0:
+                    self.fail("QEMU crashed")
+                else:
+                    self.fail("Unexpected exit code")
Signature · 1 line
2.26.2
#11 lore raw

Re: [PATCH 3/3] tests/acceptance: Test case for detecting -object crashes

Quoted 28 lines · Eduardo Habkost

Add a simple test case that will run QEMU directly (without QMP) just to check for crashes when using -object.

---
Cc: Cleber Rosa <[email protected]>
Cc: "Philippe Mathieu-Daudé" <[email protected]>
Cc: Wainer dos Santos Moschetta <[email protected]>
Cc: [email protected]

---
 tests/acceptance/object_option.py | 49 +++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100644 tests/acceptance/object_option.py
tests/acceptance/object_option.py new file +8 -0
new file mode 100644
index 0000000000..2b8bd00db1
@@ -0,0 +1,49 @@
+# Copyright (c) 2020 Red Hat, Inc.
+#
+# Author:
+#  Eduardo Habkost <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later.  See the COPYING file in the top-level directory.
+

tests/acceptance/object_option.py line 18

Quoted 10 lines
tests/acceptance/object_option.py new file +10 -0
+import avocado_qemu
+import subprocess
+import shlex
+
+class ObjectOption(avocado_qemu.Test):
+    """Check if ``-object`` option behaves as expected"""
+
+    def run(self, cmd, *args, **kwargs):
+        cmdstr = ' '.join(shlex.quote(c) for c in cmd)
+        self.log.info("Command: %s", cmdstr)

Maybe "Running command: %s" is clearer?

tests/acceptance/object_option.py line 19

Quoted 1 line
tests/acceptance/object_option.py new file +1 -0
+        return subprocess.run(cmd, encoding='utf-8', *args, **kwargs)

I'd just use universal_newlines=True (equivalent to text, but that's Python 3.7+ only), so the current encoding will be respected. I understand "utf-8" is a safe choice, but IMO, if someone sets a different default, it should be respected, unless there's a clear reason not to do so.

tests/acceptance/object_option.py line 21

Quoted 2 lines
tests/acceptance/object_option.py new file +2 -0
+
+    def devices(self):

Maybe call it get_devices() or make it a property?

tests/acceptance/object_option.py line 26

Quoted 5 lines
tests/acceptance/object_option.py new file +5 -0
+        out = self.run([self.qemu_bin, '-object', 'help'],
+                       check=True, stdout=subprocess.PIPE).stdout
+        lines = out.split('\n')
+        return [l.strip() for l in lines[1:] if l.strip()]
+

In case the CalledProcessError is raised on subprocess.run(), the following test status will be ERROR, which is a condition you were not testing for, something unexpected. Given that you're using check=True, I'd decorate this test with:

@avocado.fail_on(subprocess.CalledProcessError)

tests/acceptance/object_option.py line 35

Quoted 9 lines
tests/acceptance/object_option.py new file +9 -0
+    def test_help(self):
+        """Check if ``-object ...,help`` behaves as expected"""
+        for device in self.devices():
+            self.run([self.qemu_bin, '-object', '%s,help' % (device)],
+                     check=True,
+                     stdout=subprocess.DEVNULL)
+
+    def test_crash(self):
+        """Check for crashes when using ``-object ...``"""

Maybe change the wording here, given that this is really checking that QEMU doesn't crash, right? So something like "Checks that QEMU doesn't crash ..." seems clearer to me.

tests/acceptance/object_option.py line 42

Quoted 7 lines
tests/acceptance/object_option.py new file +7 -0
+        for device in self.devices():
+            r = self.run([self.qemu_bin, '-object',
+                                '%s,id=obj0' % (device),
+                                '-monitor', 'stdio'],
+                         input='quit\n',
+                         stdout=subprocess.DEVNULL,
+                         stderr=subprocess.PIPE)

I know adding command line options to QEMU (specially at this stage) is, at the very least, frowned upon, but I can't help to think that an option similar to Python's "-c" would be very helpful in testing scenarios.

Quoted 10 lines
tests/acceptance/object_option.py new file +7 -0
+            if r.returncode not in (0, 1):
+                self.log.warn("QEMU stderr: %s", r.stderr)
+                self.log.warn("QEMU exit code: %d", r.returncode)
+                if r.returncode < 0:
+                    self.fail("QEMU crashed")
+                else:
+                    self.fail("Unexpected exit code")
Signature · 1 line
2.26.2

This looks fine, but this test is clearly provocative (at least to me), given at it's a bit more barebones than the approached used in "empty_cpu_model.py". It seems to show the need for:

1) a custom test class (similar to avocado_qemu.Test) that can be more suitable to more barebones tests like this

2) a custom QEMU "varianter" implementation we've talked about in the past, with which the test code could be simplified. For instance, setting hypothetical configuration "qemu-varianter=objects" could produce:

[ {"object": "authz-list"},

    {"object": "authz-list-file"},
    ...
    {"object": "tls-creds-x509"} ]

I had not attempted this before, because Avocado was limited to applying variants globally to a job. Starting with version 81.0 (released a month or so ago), this is no longer a limitation.

A simple PoC is here:

https://gitlab.com/cleber.gnu/qemu/-/commit/30f26b662326502c2d82aabca225009ccdebe6aa

Can be run with:

./tests/venv/bin/python job_object_option.py

And the tests themselves would look like this:

https://paste.centos.org/view/4c5f413d

Let me know if any of this makes sense.

Thanks,
- Cleber.