diff mbox series

[1/1] hw/i386: prevent crash when an invalid number of dies is given

Message ID 20201012033507.906096-2-crosa@redhat.com
State New
Headers show
Series hw/i386: prevent crash when an invalid number of dies is given | expand

Commit Message

Cleber Rosa Oct. 12, 2020, 3:35 a.m. UTC
When parsing the topology, the right default value of 1 is given to
dies, but if an invalid number such as 0 is given, QEMU will crash
with a floating point exception.

The alternative approach is to silently set dies to a valid value,
as it's done with cores and threads.

Signed-off-by: Cleber Rosa <crosa@redhat.com>
---
 hw/i386/pc.c                          |  5 +++++
 tests/acceptance/cpu_topology_dies.py | 31 +++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 tests/acceptance/cpu_topology_dies.py

Comments

Willian Rampazzo Oct. 19, 2020, 4:30 p.m. UTC | #1
On Mon, Oct 12, 2020 at 12:35 AM Cleber Rosa <crosa@redhat.com> wrote:
>

> When parsing the topology, the right default value of 1 is given to

> dies, but if an invalid number such as 0 is given, QEMU will crash

> with a floating point exception.

>

> The alternative approach is to silently set dies to a valid value,

> as it's done with cores and threads.

>

> Signed-off-by: Cleber Rosa <crosa@redhat.com>

> ---

>  hw/i386/pc.c                          |  5 +++++

>  tests/acceptance/cpu_topology_dies.py | 31 +++++++++++++++++++++++++++

>  2 files changed, 36 insertions(+)

>  create mode 100644 tests/acceptance/cpu_topology_dies.py

>

> diff --git a/hw/i386/pc.c b/hw/i386/pc.c

> index e87be5d29a..209e44663d 100644

> --- a/hw/i386/pc.c

> +++ b/hw/i386/pc.c

> @@ -713,6 +713,11 @@ void pc_smp_parse(MachineState *ms, QemuOpts *opts)

>          unsigned cores   = qemu_opt_get_number(opts, "cores", 0);

>          unsigned threads = qemu_opt_get_number(opts, "threads", 0);

>

> +        if (dies <= 0) {

> +            error_report("Invalid CPU topology: dies must be 1 or greater");

> +            exit(1);

> +        }

> +

>          /* compute missing values, prefer sockets over cores over threads */

>          if (cpus == 0 || sockets == 0) {

>              cores = cores > 0 ? cores : 1;

> diff --git a/tests/acceptance/cpu_topology_dies.py b/tests/acceptance/cpu_topology_dies.py

> new file mode 100644

> index 0000000000..d73b7b30a2

> --- /dev/null

> +++ b/tests/acceptance/cpu_topology_dies.py

> @@ -0,0 +1,31 @@

> +# Check for crash when using invalid dies value for -smp

> +#

> +# Copyright (c) 2020 Red Hat, Inc.

> +#

> +# Author:

> +#  Cleber Rosa <crosa@redhat.com>

> +#

> +# This work is licensed under the terms of the GNU GPL, version 2 or

> +# later.  See the COPYING file in the top-level directory.

> +from avocado_qemu import Test

> +

> +class CPUTolopogyDies(Test):

> +    """

> +    :avocado: tags=arch:x86_64

> +    :avocado: tags=machine:pc

> +    """

> +    def test_invalid(self):

> +        self.vm.add_args('-S', '-display', 'none', '-smp', '1,dies=0')

> +        self.vm.set_qmp_monitor(enabled=False)

> +        self.vm.launch()

> +        self.vm.wait()

> +        self.assertEquals(self.vm.exitcode(), 1, "QEMU exit code should be 1")

> +        self.assertRegex(self.vm.get_log(),

> +                         r'Invalid CPU topology: dies must be 1 or greater')

> +

> +    def test_valid(self):

> +        self.vm.add_args('-S', '-display', 'none', '-smp', '1,dies=1')

> +        self.vm.launch()

> +        self.vm.command('quit')

> +        self.vm.wait()

> +        self.assertEquals(self.vm.exitcode(), 0, "QEMU exit code should be 0")

> --

> 2.25.4

>


Reviewed-by: Willian Rampazzo <willianr@redhat.com>
diff mbox series

Patch

diff --git a/hw/i386/pc.c b/hw/i386/pc.c
index e87be5d29a..209e44663d 100644
--- a/hw/i386/pc.c
+++ b/hw/i386/pc.c
@@ -713,6 +713,11 @@  void pc_smp_parse(MachineState *ms, QemuOpts *opts)
         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
 
+        if (dies <= 0) {
+            error_report("Invalid CPU topology: dies must be 1 or greater");
+            exit(1);
+        }
+
         /* compute missing values, prefer sockets over cores over threads */
         if (cpus == 0 || sockets == 0) {
             cores = cores > 0 ? cores : 1;
diff --git a/tests/acceptance/cpu_topology_dies.py b/tests/acceptance/cpu_topology_dies.py
new file mode 100644
index 0000000000..d73b7b30a2
--- /dev/null
+++ b/tests/acceptance/cpu_topology_dies.py
@@ -0,0 +1,31 @@ 
+# Check for crash when using invalid dies value for -smp
+#
+# Copyright (c) 2020 Red Hat, Inc.
+#
+# Author:
+#  Cleber Rosa <crosa@redhat.com>
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or
+# later.  See the COPYING file in the top-level directory.
+from avocado_qemu import Test
+
+class CPUTolopogyDies(Test):
+    """
+    :avocado: tags=arch:x86_64
+    :avocado: tags=machine:pc
+    """
+    def test_invalid(self):
+        self.vm.add_args('-S', '-display', 'none', '-smp', '1,dies=0')
+        self.vm.set_qmp_monitor(enabled=False)
+        self.vm.launch()
+        self.vm.wait()
+        self.assertEquals(self.vm.exitcode(), 1, "QEMU exit code should be 1")
+        self.assertRegex(self.vm.get_log(),
+                         r'Invalid CPU topology: dies must be 1 or greater')
+
+    def test_valid(self):
+        self.vm.add_args('-S', '-display', 'none', '-smp', '1,dies=1')
+        self.vm.launch()
+        self.vm.command('quit')
+        self.vm.wait()
+        self.assertEquals(self.vm.exitcode(), 0, "QEMU exit code should be 0")