hw/i386: prevent crash when an invalid number of dies is given
cover hw/i386: prevent crash when an invalid number of dies is given
Simple patch (and test) to avoid crashes when and invalid number of dies is given.
Cleber Rosa (1):
hw/i386: prevent crash when an invalid number of dies is given
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
Signature · 1 line
2.25.4
PATCH 1/1 hw/i386: prevent crash when an invalid number of dies is given
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.
--- 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
hw/i386/pc.c +5 -0
@@ -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 ;
tests/acceptance/cpu_topology_dies.py new file +31 -0
@@ -0,0 +1,31 @@ +# Check for crash when using invalid dies value for -smp +# +# Copyright (c) 2020 Red Hat, Inc. +# +# Author: +# Cleber Rosa <[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. +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")
Signature · 1 line
2.25.4
Quoted 69 lines · Cleber Rosa
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 <[email protected]>--- 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.pyhw/i386/pc.c +5
-0@@ -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 ;tests/acceptance/cpu_topology_dies.py new file +31
-1@@ -0,0 +1,31 @@ +# Check for crash when using invalid dies value for -smp +# +# Copyright (c) 2020 Red Hat, Inc. +# +# Author: +# Cleber Rosa <[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. +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") --
Quoted 2 lines
2.25.4