3 # Copyright (c) 2017 Red Hat Inc
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 Run QEMU with all combinations of -machine and -device types,
24 check for crashes and unexpected errors.
35 from itertools import chain
37 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'python'))
38 from qemu.machine import QEMUMachine
39 from qemu.aqmp import ConnectError
41 logger = logging.getLogger('device-crash-test')
45 # Purposes of the following rule list:
46 # * Avoiding verbose log messages when we find known non-fatal
48 # * Avoiding fatal errors when we find known crashes
49 # * Skipping machines/devices that are known not to work out of
50 # the box, when running in --quick mode
52 # Keeping the rule list updated is desirable, but not required,
53 # because unexpected cases where QEMU exits with exitcode=1 will
54 # just trigger a INFO message.
56 # Valid error rule keys:
57 # * accel: regexp, full match only
58 # * machine: regexp, full match only
59 # * device: regexp, full match only
60 # * log: regexp, partial match allowed
61 # * exitcode: if not present, defaults to 1. If None, matches any exitcode
62 # * warn: if True, matching failures will be logged as warnings
63 # * expected: if True, QEMU is expected to always fail every time
64 # when testing the corresponding test case
65 # * loglevel: log level of log output when there's a match.
67 # Machines that won't work out of the box:
68 # MACHINE | ERROR MESSAGE
69 {'machine':'niagara', 'expected':True}, # Unable to load a firmware for -M niagara
70 {'machine':'boston', 'expected':True}, # Please provide either a -kernel or -bios argument
71 {'machine':'leon3_generic', 'expected':True}, # Can't read bios image (null)
73 # devices that don't work out of the box because they require extra options to "-device DEV":
74 # DEVICE | ERROR MESSAGE
75 {'device':'.*-(i386|x86_64)-cpu', 'expected':True}, # CPU socket-id is not set
76 {'device':'icp', 'expected':True}, # icp_realize: required link 'xics' not found: Property '.xics' not found
77 {'device':'ics', 'expected':True}, # ics_base_realize: required link 'xics' not found: Property '.xics' not found
78 # "-device ide-cd" does work on more recent QEMU versions, so it doesn't have expected=True
79 {'device':'ide-cd'}, # No drive specified
80 {'device':'ide-hd', 'expected':True}, # No drive specified
81 {'device':'ipmi-bmc-extern', 'expected':True}, # IPMI external bmc requires chardev attribute
82 {'device':'isa-debugcon', 'expected':True}, # Can't create serial device, empty char device
83 {'device':'isa-ipmi-bt', 'expected':True}, # IPMI device requires a bmc attribute to be set
84 {'device':'isa-ipmi-kcs', 'expected':True}, # IPMI device requires a bmc attribute to be set
85 {'device':'isa-parallel', 'expected':True}, # Can't create serial device, empty char device
86 {'device':'ivshmem-doorbell', 'expected':True}, # You must specify a 'chardev'
87 {'device':'ivshmem-plain', 'expected':True}, # You must specify a 'memdev'
88 {'device':'loader', 'expected':True}, # please include valid arguments
89 {'device':'nand', 'expected':True}, # Unsupported NAND block size 0x1
90 {'device':'nvdimm', 'expected':True}, # 'memdev' property is not set
91 {'device':'nvme', 'expected':True}, # Device initialization failed
92 {'device':'pc-dimm', 'expected':True}, # 'memdev' property is not set
93 {'device':'pci-bridge', 'expected':True}, # Bridge chassis not specified. Each bridge is required to be assigned a unique chassis id > 0.
94 {'device':'pci-bridge-seat', 'expected':True}, # Bridge chassis not specified. Each bridge is required to be assigned a unique chassis id > 0.
95 {'device':'pxb', 'expected':True}, # Bridge chassis not specified. Each bridge is required to be assigned a unique chassis id > 0.
96 {'device':'scsi-block', 'expected':True}, # drive property not set
97 {'device':'scsi-generic', 'expected':True}, # drive property not set
98 {'device':'scsi-hd', 'expected':True}, # drive property not set
99 {'device':'spapr-pci-host-bridge', 'expected':True}, # BUID not specified for PHB
100 {'device':'spapr-rng', 'expected':True}, # spapr-rng needs an RNG backend!
101 {'device':'spapr-vty', 'expected':True}, # chardev property not set
102 {'device':'tpm-tis', 'expected':True}, # tpm_tis: backend driver with id (null) could not be found
103 {'device':'unimplemented-device', 'expected':True}, # property 'size' not specified or zero
104 {'device':'usb-braille', 'expected':True}, # Property chardev is required
105 {'device':'usb-mtp', 'expected':True}, # rootdir property must be configured
106 {'device':'usb-redir', 'expected':True}, # Parameter 'chardev' is missing
107 {'device':'usb-serial', 'expected':True}, # Property chardev is required
108 {'device':'usb-storage', 'expected':True}, # drive property not set
109 {'device':'vfio-amd-xgbe', 'expected':True}, # -device vfio-amd-xgbe: vfio error: wrong host device name
110 {'device':'vfio-calxeda-xgmac', 'expected':True}, # -device vfio-calxeda-xgmac: vfio error: wrong host device name
111 {'device':'vfio-pci', 'expected':True}, # No provided host device
112 {'device':'vfio-pci-igd-lpc-bridge', 'expected':True}, # VFIO dummy ISA/LPC bridge must have address 1f.0
113 {'device':'vhost-scsi.*', 'expected':True}, # vhost-scsi: missing wwpn
114 {'device':'vhost-vsock-device', 'expected':True}, # guest-cid property must be greater than 2
115 {'device':'vhost-vsock-pci', 'expected':True}, # guest-cid property must be greater than 2
116 {'device':'virtio-9p-ccw', 'expected':True}, # 9pfs device couldn't find fsdev with the id = NULL
117 {'device':'virtio-9p-device', 'expected':True}, # 9pfs device couldn't find fsdev with the id = NULL
118 {'device':'virtio-9p-pci', 'expected':True}, # 9pfs device couldn't find fsdev with the id = NULL
119 {'device':'virtio-blk-ccw', 'expected':True}, # drive property not set
120 {'device':'virtio-blk-device', 'expected':True}, # drive property not set
121 {'device':'virtio-blk-device', 'expected':True}, # drive property not set
122 {'device':'virtio-blk-pci', 'expected':True}, # drive property not set
123 {'device':'virtio-crypto-ccw', 'expected':True}, # 'cryptodev' parameter expects a valid object
124 {'device':'virtio-crypto-device', 'expected':True}, # 'cryptodev' parameter expects a valid object
125 {'device':'virtio-crypto-pci', 'expected':True}, # 'cryptodev' parameter expects a valid object
126 {'device':'virtio-input-host-device', 'expected':True}, # evdev property is required
127 {'device':'virtio-input-host-pci', 'expected':True}, # evdev property is required
128 {'device':'xen-pvdevice', 'expected':True}, # Device ID invalid, it must always be supplied
129 {'device':'vhost-vsock-ccw', 'expected':True}, # guest-cid property must be greater than 2
130 {'device':'zpci', 'expected':True}, # target must be defined
131 {'device':'pnv-(occ|icp|lpc)', 'expected':True}, # required link 'xics' not found: Property '.xics' not found
132 {'device':'powernv-cpu-.*', 'expected':True}, # pnv_core_realize: required link 'xics' not found: Property '.xics' not found
134 # ioapic devices are already created by pc and will fail:
135 {'machine':'q35|pc.*', 'device':'kvm-ioapic', 'expected':True}, # Only 1 ioapics allowed
136 {'machine':'q35|pc.*', 'device':'ioapic', 'expected':True}, # Only 1 ioapics allowed
138 # "spapr-cpu-core needs a pseries machine"
139 {'machine':'(?!pseries).*', 'device':'.*-spapr-cpu-core', 'expected':True},
141 # KVM-specific devices shouldn't be tried without accel=kvm:
142 {'accel':'(?!kvm).*', 'device':'kvmclock', 'expected':True},
144 # xen-specific machines and devices:
145 {'accel':'(?!xen).*', 'machine':'xen.*', 'expected':True},
146 {'accel':'(?!xen).*', 'device':'xen-.*', 'expected':True},
148 # this fails on some machine-types, but not all, so they don't have expected=True:
149 {'device':'vmgenid'}, # vmgenid requires DMA write support in fw_cfg, which this machine type does not provide
151 # Silence INFO messages for errors that are common on multiple
153 {'log':r"No '[\w-]+' bus found for device '[\w-]+'"},
154 {'log':r"images* must be given with the 'pflash' parameter"},
155 {'log':r"(Guest|ROM|Flash|Kernel) image must be specified"},
156 {'log':r"[cC]ould not load [\w ]+ (BIOS|bios) '[\w-]+\.bin'"},
157 {'log':r"Couldn't find rom image '[\w-]+\.bin'"},
158 {'log':r"speed mismatch trying to attach usb device"},
159 {'log':r"Can't create a second ISA bus"},
160 {'log':r"duplicate fw_cfg file name"},
161 # sysbus-related error messages: most machines reject most dynamic sysbus devices:
162 {'log':r"Option '-device [\w.,-]+' cannot be handled by this machine"},
163 {'log':r"Device [\w.,-]+ is not supported by this machine yet"},
164 {'log':r"Device [\w.,-]+ can not be dynamically instantiated"},
165 {'log':r"Platform Bus: Can not fit MMIO region of size "},
166 # other more specific errors we will ignore:
167 {'device':'.*-spapr-cpu-core', 'log':r"CPU core type should be"},
168 {'log':r"MSI(-X)? is not supported by interrupt controller"},
169 {'log':r"pxb-pcie? devices cannot reside on a PCIe? bus"},
170 {'log':r"Ignoring smp_cpus value"},
171 {'log':r"sd_init failed: Drive 'sd0' is already in use because it has been automatically connected to another device"},
172 {'log':r"This CPU requires a smaller page size than the system is using"},
173 {'log':r"MSI-X support is mandatory in the S390 architecture"},
174 {'log':r"rom check and register reset failed"},
175 {'log':r"Unable to initialize GIC, CPUState for CPU#0 not valid"},
176 {'log':r"Multiple VT220 operator consoles are not supported"},
177 {'log':r"core 0 already populated"},
178 {'log':r"could not find stage1 bootloader"},
179 {'log':r"No '.*' bus found for device"},
181 # other exitcode=1 failures not listed above will just generate INFO messages:
182 {'exitcode':1, 'loglevel':logging.INFO},
184 # everything else (including SIGABRT and SIGSEGV) will be a fatal error:
185 {'exitcode':None, 'fatal':True, 'loglevel':logging.FATAL},
189 def errorRuleTestCaseMatch(rule, t):
190 """Check if a test case specification can match a error rule
192 This only checks if a error rule is a candidate match
193 for a given test case, it won't check if the test case
194 results/output match the rule. See ruleListResultMatch().
196 return (('machine' not in rule or
197 'machine' not in t or
198 re.match(rule['machine'] + '$', t['machine'])) and
199 ('accel' not in rule or
201 re.match(rule['accel'] + '$', t['accel'])) and
202 ('device' not in rule or
204 re.match(rule['device'] + '$', t['device'])))
207 def ruleListCandidates(t):
208 """Generate the list of candidates that can match a test case"""
209 for i, rule in enumerate(ERROR_RULE_LIST):
210 if errorRuleTestCaseMatch(rule, t):
214 def findExpectedResult(t):
215 """Check if there's an expected=True error rule for a test case
217 Returns (i, rule) tuple, where i is the index in
218 ERROR_RULE_LIST and rule is the error rule itself.
220 for i, rule in ruleListCandidates(t):
221 if rule.get('expected'):
225 def ruleListResultMatch(rule, r):
226 """Check if test case results/output match a error rule
228 It is valid to call this function only if
229 errorRuleTestCaseMatch() is True for the rule (e.g. on
230 rules returned by ruleListCandidates())
232 assert errorRuleTestCaseMatch(rule, r['testcase'])
233 return ((rule.get('exitcode', 1) is None or
234 r['exitcode'] == rule.get('exitcode', 1)) and
235 ('log' not in rule or
236 re.search(rule['log'], r['log'], re.MULTILINE)))
239 def checkResultRuleList(r):
240 """Look up error rule for a given test case result
242 Returns (i, rule) tuple, where i is the index in
243 ERROR_RULE_LIST and rule is the error rule itself.
245 for i, rule in ruleListCandidates(r['testcase']):
246 if ruleListResultMatch(rule, r):
249 raise Exception("this should never happen")
252 def qemuOptsEscape(s):
253 """Escape option value QemuOpts"""
254 return s.replace(",", ",,")
257 def formatTestCase(t):
258 """Format test case info as "key=value key=value" for prettier logging output"""
259 return ' '.join('%s=%s' % (k, v) for k, v in t.items())
262 def qomListTypeNames(vm, **kwargs):
263 """Run qom-list-types QMP command, return type names"""
264 types = vm.command('qom-list-types', **kwargs)
265 return [t['name'] for t in types]
269 """Parse 'info qdm' output"""
270 args = {'command-line': 'info qdm'}
271 devhelp = vm.command('human-monitor-command', **args)
272 for l in devhelp.split('\n'):
274 if l == '' or l.endswith(':'):
276 d = {'name': re.search(r'name "([^"]+)"', l).group(1),
277 'no-user': (re.search(', no-user', l) is not None)}
281 class QemuBinaryInfo(object):
282 def __init__(self, binary, devtype):
287 self._machine_info = {}
289 dbg("devtype: %r", devtype)
290 args = ['-S', '-machine', 'none,accel=kvm:tcg']
291 dbg("querying info for QEMU binary: %s", binary)
292 vm = QEMUMachine(binary=binary, args=args)
295 self.alldevs = set(qomListTypeNames(vm, implements=devtype, abstract=False))
296 # there's no way to query DeviceClass::user_creatable using QMP,
298 self.no_user_devs = set([d['name'] for d in infoQDM(vm, ) if d['no-user']])
299 self.machines = list(m['name'] for m in vm.command('query-machines'))
300 self.user_devs = self.alldevs.difference(self.no_user_devs)
301 self.kvm_available = vm.command('query-kvm')['enabled']
305 def machineInfo(self, machine):
306 """Query for information on a specific machine-type
308 Results are cached internally, in case the same machine-
309 type is queried multiple times.
311 if machine in self._machine_info:
312 return self._machine_info[machine]
315 args = ['-S', '-machine', '%s' % (machine)]
316 dbg("querying machine info for binary=%s machine=%s", self.binary, machine)
317 vm = QEMUMachine(binary=self.binary, args=args)
320 mi['runnable'] = True
322 dbg("exception trying to run binary=%s machine=%s", self.binary, machine, exc_info=sys.exc_info())
323 dbg("log: %r", vm.get_log())
324 mi['runnable'] = False
327 self._machine_info[machine] = mi
334 def getBinaryInfo(args, binary):
335 if binary not in BINARY_INFO:
336 BINARY_INFO[binary] = QemuBinaryInfo(binary, args.devtype)
337 return BINARY_INFO[binary]
340 def checkOneCase(args, testcase):
341 """Check one specific case
343 Returns a dictionary containing failure information on error,
346 binary = testcase['binary']
347 accel = testcase['accel']
348 machine = testcase['machine']
349 device = testcase['device']
351 dbg("will test: %r", testcase)
353 args = ['-S', '-machine', '%s,accel=%s' % (machine, accel),
354 '-device', qemuOptsEscape(device)]
355 cmdline = ' '.join([binary] + args)
356 dbg("will launch QEMU: %s", cmdline)
357 vm = QEMUMachine(binary=binary, args=args)
363 except Exception as this_exc:
365 exc_traceback = traceback.format_exc()
366 dbg("Exception while running test case")
372 if exc is not None or ec != 0:
374 'exc_traceback':exc_traceback,
381 def binariesToTest(args, testcase):
385 r = [f.path for f in os.scandir('.')
386 if f.name.startswith('qemu-system-') and
387 f.is_file() and os.access(f, os.X_OK)]
391 def accelsToTest(args, testcase):
392 if getBinaryInfo(args, testcase['binary']).kvm_available:
397 def machinesToTest(args, testcase):
398 return getBinaryInfo(args, testcase['binary']).machines
401 def devicesToTest(args, testcase):
402 return getBinaryInfo(args, testcase['binary']).user_devs
405 TESTCASE_VARIABLES = [
406 ('binary', binariesToTest),
407 ('accel', accelsToTest),
408 ('machine', machinesToTest),
409 ('device', devicesToTest),
413 def genCases1(args, testcases, var, fn):
414 """Generate new testcases for one variable
416 If an existing item already has a variable set, don't
417 generate new items and just return it directly. This
418 allows the "-t" command-line option to be used to choose
419 a specific test case.
421 for testcase in testcases:
423 yield testcase.copy()
425 for i in fn(args, testcase):
431 def genCases(args, testcase):
432 """Generate test cases for all variables
434 cases = [testcase.copy()]
435 for var, fn in TESTCASE_VARIABLES:
436 dbg("var: %r, fn: %r", var, fn)
437 cases = genCases1(args, cases, var, fn)
441 def casesToTest(args, testcase):
442 cases = genCases(args, testcase)
445 cases = random.sample(cases, min(args.random, len(cases)))
448 dbg("%d test cases to test", len(cases))
451 random.shuffle(cases)
455 def logFailure(f, level):
457 logger.log(level, "failed: %s", formatTestCase(t))
458 logger.log(level, "cmdline: %s", f['cmdline'])
459 for l in f['log'].strip().split('\n'):
460 logger.log(level, "log: %s", l)
461 logger.log(level, "exit code: %r", f['exitcode'])
463 # If the Exception is merely a QMP connect error,
464 # reduce the logging level for its traceback to
465 # improve visual clarity.
466 if isinstance(f.get('exc'), ConnectError):
467 logger.log(level, "%s.%s: %s",
468 type(f['exc']).__module__,
469 type(f['exc']).__qualname__,
471 level = logging.DEBUG
473 if f['exc_traceback']:
474 logger.log(level, "exception:")
475 for l in f['exc_traceback'].split('\n'):
476 logger.log(level, " %s", l.rstrip('\n'))
480 parser = argparse.ArgumentParser(description="QEMU -device crash test")
481 parser.add_argument('-t', metavar='KEY=VALUE', nargs='*',
482 help="Limit test cases to KEY=VALUE",
483 action='append', dest='testcases', default=[])
484 parser.add_argument('-d', '--debug', action='store_true',
486 parser.add_argument('-v', '--verbose', action='store_true', default=True,
487 help='verbose output')
488 parser.add_argument('-q', '--quiet', dest='verbose', action='store_false',
489 help='non-verbose output')
490 parser.add_argument('-r', '--random', type=int, metavar='COUNT',
491 help='run a random sample of COUNT test cases',
493 parser.add_argument('--shuffle', action='store_true',
494 help='Run test cases in random order')
495 parser.add_argument('--dry-run', action='store_true',
496 help="Don't run any tests, just generate list")
497 parser.add_argument('-D', '--devtype', metavar='TYPE',
498 help="Test only device types that implement TYPE")
499 parser.add_argument('-Q', '--quick', action='store_true', default=True,
500 help="Quick mode: skip test cases that are expected to fail")
501 parser.add_argument('-F', '--full', action='store_false', dest='quick',
502 help="Full mode: test cases that are expected to fail")
503 parser.add_argument('--strict', action='store_true', dest='strict',
504 help="Treat all warnings as fatal")
505 parser.add_argument('qemu', nargs='*', metavar='QEMU',
506 help='QEMU binary to run')
507 args = parser.parse_args()
515 logging.basicConfig(stream=sys.stdout, level=lvl, format='%(levelname)s: %(message)s')
518 # Async QMP, when in use, is chatty about connection failures.
519 # This script knowingly generates a ton of connection errors.
520 # Silence this logger.
521 logging.getLogger('qemu.aqmp.qmp_client').setLevel(logging.CRITICAL)
529 dbg("testcases: %r", args.testcases)
531 for t in chain(*args.testcases):
533 k, v = kv.split('=', 1)
536 if len(binariesToTest(args, tc)) == 0:
537 print("No QEMU binary found", file=sys.stderr)
538 parser.print_usage(sys.stderr)
541 for t in casesToTest(args, tc):
542 logger.info("running test case: %s", formatTestCase(t))
545 expected_match = findExpectedResult(t)
548 not getBinaryInfo(args, t['binary']).machineInfo(t['machine'])['runnable'])):
549 dbg("skipped: %s", formatTestCase(t))
557 f = checkOneCase(args, t)
558 except KeyboardInterrupt:
562 i, rule = checkResultRuleList(f)
563 dbg("testcase: %r, rule list match: %r", t, rule)
564 wl_stats.setdefault(i, []).append(f)
565 level = rule.get('loglevel', logging.DEBUG)
567 if rule.get('fatal') or (args.strict and level >= logging.WARN):
568 fatal_failures.append(f)
570 dbg("success: %s", formatTestCase(t))
572 logger.warn("Didn't fail as expected: %s", formatTestCase(t))
574 logger.info("Total: %d test cases", total)
576 logger.info("Skipped %d test cases", skipped)
579 stats = sorted([(len(wl_stats.get(i, [])), rule) for i, rule in
580 enumerate(ERROR_RULE_LIST)], key=lambda x: x[0])
581 for count, rule in stats:
582 dbg("error rule stats: %d: %r", count, rule)
585 for f in fatal_failures:
587 logger.error("Fatal failure: %s", formatTestCase(t))
588 logger.error("Fatal failures on some machine/device combinations")
591 if __name__ == '__main__':