qemu-e2k/tests
Eric Blake 0426d53c65 qapi: Simplify visiting of alternate types
Previously, working with alternates required two lookup arrays
and some indirection: for type Foo, we created Foo_qtypes[]
which maps each qtype to a value of the generated FooKind enum,
then look up that value in FooKind_lookup[] like we do for other
union types.

This has a couple of subtle bugs.  First, the generator was
creating a call with a parameter '(int *) &(*obj)->type' where
type is an enum type; this is unsafe if the compiler chooses
to store the enum type in a different size than int, where
assigning through the wrong size pointer can corrupt data or
cause a SIGBUS.

Related bug, not not fixed in this patch: qapi-visit.py's
gen_visit_enum() generates a cast of its enum * argument to
int *. Marked FIXME.

Second, since the values of the FooKind enum start at zero, all
entries of the Foo_qtypes[] array that were not explicitly
initialized will map to the same branch of the union as the
first member of the alternate, rather than triggering a desired
failure in visit_get_next_type().  Fortunately, the bug seldom
bites; the very next thing the input visitor does is try to
parse the incoming JSON with the wrong parser, which normally
fails; the output visitor is not used with a C struct in that
state, and the dealloc visitor has nothing to clean up (so
there is no leak).

However, the second bug IS observable in one case: parsing an
integer causes unusual behavior in an alternate that contains
at least a 'number' member but no 'int' member, because the
'number' parser accepts QTYPE_QINT in addition to the expected
QTYPE_QFLOAT (that is, since 'int' is not a member, the type
QTYPE_QINT accidentally maps to FooKind 0; if this enum value
is the 'number' branch the integer parses successfully, but if
the 'number' branch is not first, some other branch tries to
parse the integer and rejects it).  A later patch will worry
about fixing alternates to always parse all inputs that a
non-alternate 'number' would accept, for now this is still
marked FIXME in the updated test-qmp-input-visitor.c, to
merely point out that new undesired behavior of 'ans' matches
the existing undesired behavior of 'asn'.

This patch fixes the default-initialization bug by deleting the
indirection, and modifying get_next_type() to directly assign a
QTypeCode parameter.  This in turn fixes the type-casting bug,
as we are no longer casting a pointer to enum to a questionable
size. There is no longer a need to generate an implicit FooKind
enum associated with the alternate type (since the QMP wire
format never uses the stringized counterparts of the C union
member names).  Since the updated visit_get_next_type() does not
know which qtypes are expected, the generated visitor is
modified to generate an error statement if an unexpected type is
encountered.

Callers now have to know the QTYPE_* mapping when looking at the
discriminator; but so far, only the testsuite was even using the
C struct of an alternate types.  I considered the possibility of
keeping the internal enum FooKind, but initialized differently
than most generated arrays, as in:
  typedef enum FooKind {
      FOO_KIND_A = QTYPE_QDICT,
      FOO_KIND_B = QTYPE_QINT,
  } FooKind;
to create nicer aliases for knowing when to use foo->a or foo->b
when inspecting foo->type; but it turned out to add too much
complexity, especially without a client.

There is a user-visible side effect to this change, but I
consider it to be an improvement. Previously,
the invalid QMP command:
  {"execute":"blockdev-add", "arguments":{"options":
    {"driver":"raw", "id":"a", "file":true}}}
failed with:
  {"error": {"class": "GenericError",
    "desc": "Invalid parameter type for 'file', expected: QDict"}}
(visit_get_next_type() succeeded, and the error comes from the
visit_type_BlockdevOptions() expecting {}; there is no mention of
the fact that a string would also work).  Now it fails with:
  {"error": {"class": "GenericError",
    "desc": "Invalid parameter type for 'file', expected: BlockdevRef"}}
(the error when the next type doesn't match any expected types for
the overall alternate).

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1449033659-25497-5-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2015-12-17 08:21:28 +01:00
..
acpi-test-data acpi: update expected files for memory unplug 2015-05-11 09:21:37 +02:00
image-fuzzer typofixes - v4 2015-09-11 10:45:43 +03:00
libqos libqos: add qemu-img presence check 2015-11-13 14:31:42 -05:00
multiboot tests/multiboot: Add test for modules 2015-01-26 12:22:44 +01:00
qapi-schema qapi: Simplify visiting of alternate types 2015-12-17 08:21:28 +01:00
qemu-iotests blkdebug: Avoid '.' in enum values 2015-12-17 08:21:27 +01:00
rocker rocker: tests: don't need to specify master/self when setting vlans 2015-07-07 13:13:22 +01:00
tcg maint: remove unused include for signal.h 2015-09-11 10:21:38 +03:00
vmstate-static-checker-data tests: vmstate static checker: add size mismatch inside substructure 2014-06-23 19:14:52 +02:00
.gitignore tests: Ignore recent test binaries 2015-11-17 18:35:56 +08:00
Makefile qapi: Remove obsolete tests for MAX collision 2015-12-17 08:21:28 +01:00
ac97-test.c tests: Add ac97 qtest 2014-05-05 20:58:33 +02:00
ahci-test.c qtest/ahci: use raw format when qemu-img is absent 2015-11-13 14:31:43 -05:00
bios-tables-test.c maint: avoid useless "if (foo) free(foo)" pattern 2015-09-11 10:21:38 +03:00
boot-order-test.c qtest: Use -display none by default 2013-11-19 10:28:14 +01:00
check-block.sh Add 'make check-block' 2012-03-12 15:14:07 +01:00
check-qdict.c check-qdict: Test cases for new functions 2015-06-12 16:58:06 +02:00
check-qfloat.c qapi: move include files to include/qobject/ 2012-12-19 08:31:31 +01:00
check-qint.c qapi: move include files to include/qobject/ 2012-12-19 08:31:31 +01:00
check-qjson.c check-qjson: Add test for JSON nesting depth limit 2015-11-26 09:18:38 +01:00
check-qlist.c qapi: move include files to include/qobject/ 2012-12-19 08:31:31 +01:00
check-qom-interface.c tests: Test QOM interface casting 2013-12-24 17:27:17 +01:00
check-qom-proplist.c qom: Add a test case for complex property finalization 2015-11-18 21:13:49 +01:00
check-qstring.c qapi: move include files to include/qobject/ 2012-12-19 08:31:31 +01:00
crypto-tls-x509-helpers.c crypto: fix leaks in TLS x509 helper functions 2015-11-18 14:56:58 +00:00
crypto-tls-x509-helpers.h crypto: add sanity checking of TLS x509 credentials 2015-09-15 15:05:09 +01:00
device-introspect-test.c qdev: Protect device-list-properties against broken devices 2015-10-09 15:25:57 +02:00
display-vga-test.c virtio-gpu: add to display-vga test 2015-07-07 11:23:18 +02:00
drive_del-test.c libqtest: New hmp() & friends 2015-10-09 15:25:57 +02:00
ds1338-test.c arm: imx25-pdk: Fix machine name 2015-10-16 11:14:53 +01:00
e1000-test.c tests: Use qtest_add_data_func() consistently 2015-06-19 10:29:14 +02:00
eepro100-test.c tests: Use qtest_add_data_func() consistently 2015-06-19 10:29:14 +02:00
endianness-test.c tests: Use qtest_add_data_func() consistently 2015-06-19 10:29:14 +02:00
es1370-test.c tests: Add es1370 qtest 2014-05-05 20:58:33 +02:00
fdc-test.c hw/block/fdc: Implement tray status 2015-10-23 18:18:23 +02:00
fw_cfg-test.c Implement fw_cfg DMA interface 2015-10-19 15:26:53 +02:00
hd-geo-test.c tests/hd-geo-test.c: Remove unused test_image variable 2015-01-15 10:44:13 +03:00
i440fx-test.c tests/i44fx-test: No need for zeroing memory before memset 2015-11-06 15:42:38 +03:00
i82801b11-test.c tests: Add i82801b11 qtest 2014-03-31 22:36:17 +02:00
ide-test.c tests: Use proper functions types instead of void (*fn) 2015-12-04 18:25:42 +01:00
intel-hda-test.c tests: Add intel-hda qtests 2014-05-05 20:58:33 +02:00
ioh3420-test.c tests: Add ioh3420 qtest 2014-05-05 20:58:33 +02:00
ipoctal232-test.c tests: Add ipoctal232 qtest 2014-02-14 21:07:13 +01:00
ivshmem-test.c ivshmem: Rename property memdev to x-memdev for 2.5 2015-11-25 10:24:27 +01:00
libqtest.c tests: Use proper functions types instead of void (*fn) 2015-12-04 18:25:42 +01:00
libqtest.h tests: Use proper functions types instead of void (*fn) 2015-12-04 18:25:42 +01:00
m48t59-test.c qtest: Include system headers before user headers 2014-02-17 23:10:02 +01:00
ne2000-test.c tests: Add ne2000 qtest 2014-02-14 20:50:19 +01:00
nvme-test.c qtests: Specify image format explicitly 2014-12-10 10:31:12 +01:00
pc-cpu-test.c tests: Use qtest_add_data_func() consistently 2015-06-19 10:29:14 +02:00
pcnet-test.c tests: Add pcnet qtest 2014-02-14 16:22:33 +01:00
pkix_asn1_tab.c crypto: add sanity checking of TLS x509 credentials 2015-09-15 15:05:09 +01:00
pvpanic-test.c pvpanic-test: Assert pause event 2014-03-31 22:36:17 +02:00
q35-test.c q35: add test for SMRAM.D_LCK 2015-06-05 19:45:09 +02:00
qemu-iotests-quick.sh qemu-iotests: Speed up make check-block 2015-01-13 11:47:55 +00:00
qom-test.c qom-test: Fix qmp() leaks 2015-12-04 18:29:31 +01:00
rcutorture.c rcu tests: fix compilation on 32-bit ppc 2015-03-25 13:37:10 +01:00
rtc-test.c qtest: Include system headers before user headers 2014-02-17 23:10:02 +01:00
rtl8139-test.c rtl8139: remove muldiv64() 2015-09-25 14:53:29 +02:00
spapr-phb-test.c tests: Add spapr-pci-host-bridge qtest 2014-03-13 01:21:57 +01:00
tco-test.c tco-test: fix up config accesses and re-enable 2015-07-08 12:38:30 +03:00
test-aio.c test-aio: Fix event notifier cleanup 2015-11-25 14:27:43 +01:00
test-bitops.c tests: Add missing include to test-bitops.c 2014-11-02 10:04:34 +03:00
test-blockjob-txn.c tests: add BlockJobTxn unit test 2015-11-12 16:22:44 +01:00
test-coroutine.c coroutine: move into libqemuutil.a library 2015-10-20 14:59:04 +01:00
test-crypto-cipher.c crypto: add sanity checking of plaintext/ciphertext length 2015-10-22 19:03:08 +01:00
test-crypto-hash.c crypto: introduce new module for computing hash digests 2015-07-07 12:04:07 +02:00
test-crypto-tlscredsx509.c crypto: add sanity checking of TLS x509 credentials 2015-09-15 15:05:09 +01:00
test-crypto-tlssession.c crypto: introduce new module for handling TLS sessions 2015-09-15 15:07:43 +01:00
test-cutils.c tests: add some qemu_strtosz() tests 2015-09-25 12:04:41 +02:00
test-hbitmap.c maint: avoid useless "if (foo) free(foo)" pattern 2015-09-11 10:21:38 +03:00
test-int128.c tests/test-int128: Don't use __noclone__ attribute on clang 2014-03-02 17:20:37 +04:00
test-iov.c test-iov: add iov_discard_front/back() testcases 2013-01-02 15:58:09 +01:00
test-mul64.c tests: Add unit tests for mulu64 and muls64 2013-02-17 14:28:58 +00:00
test-netfilter.c tests: add test cases for netfilter object 2015-10-12 13:34:32 +08:00
test-opts-visitor.c QemuOpts: Wean off qerror_report_err() 2015-06-22 18:20:39 +02:00
test-qdev-global-props.c qdev: Move global validation to a single function 2014-09-18 21:51:24 +03:00
test-qemu-opts.c QemuOpts: Wean off qerror_report_err() 2015-06-22 18:20:39 +02:00
test-qga.c qga: Better mapping of SEEK_* in guest-file-seek 2015-11-25 17:56:45 -06:00
test-qmp-commands.c qapi: Fix c_name() munging 2015-12-17 08:21:27 +01:00
test-qmp-event.c qapi: Unbox base members 2015-11-02 08:30:26 +01:00
test-qmp-input-strict.c qapi: Simplify error cleanup in test-qmp-* 2015-11-10 08:08:21 +01:00
test-qmp-input-visitor.c qapi: Simplify visiting of alternate types 2015-12-17 08:21:28 +01:00
test-qmp-output-visitor.c qapi: Simplify visiting of alternate types 2015-12-17 08:21:28 +01:00
test-rcu-list.c rcu: actually register threads that have RCU read-side critical sections 2015-07-24 13:57:45 +02:00
test-rfifolock.c rfifolock: add recursive FIFO lock 2014-03-13 14:42:21 +01:00
test-string-input-visitor.c tests: simplify code 2014-06-19 18:44:22 +03:00
test-string-output-visitor.c qapi: Don't let implicit enum MAX member collide 2015-12-17 08:21:28 +01:00
test-thread-pool.c block: Rename BlockDriverAIOCB* to BlockAIOCB* 2014-10-20 13:41:27 +02:00
test-throttle.c throttle: add throttle_max_is_missing_limit() test 2015-08-05 12:53:48 +01:00
test-timed-average.c util: Infrastructure for computing recent averages 2015-11-12 16:22:45 +01:00
test-visitor-serialization.c qapi: Simplify non-error testing in test-qmp-* 2015-11-09 16:45:05 +01:00
test-vmstate.c coroutine: move into libqemuutil.a library 2015-10-20 14:59:04 +01:00
test-write-threshold.c block: add event when disk usage exceeds threshold 2015-02-06 17:24:21 +01:00
test-x86-cpuid.c target-i386: Move topology.h to include/hw/i386 2015-03-09 16:30:02 -03:00
test-xbzrle.c maint: remove unused include for strings.h 2015-09-11 10:21:38 +03:00
tmp105-test.c libqtest: escape strings in QMP commands, fix leak 2014-07-01 10:20:41 +02:00
tpci200-test.c tests: Add tpci200 qtest 2014-02-14 21:02:27 +01:00
usb-hcd-ehci-test.c tests: usb: Generic usb device hotplug 2014-10-15 05:03:13 +02:00
usb-hcd-ohci-test.c misc: fix typos in copyright declaration 2015-03-26 14:21:43 +01:00
usb-hcd-uhci-test.c misc: fix typos in copyright declaration 2015-03-26 14:21:43 +01:00
usb-hcd-xhci-test.c misc: fix typos in copyright declaration 2015-03-26 14:21:43 +01:00
vhost-user-bridge.c tests/vhost-user-bridge.c: fix fd leakage 2015-12-02 19:27:26 +02:00
vhost-user-test.c tests: Use proper functions types instead of void (*fn) 2015-12-04 18:25:42 +01:00
virtio-9p-test.c tests: Add virtio-9p qtest 2014-03-31 22:35:50 +02:00
virtio-balloon-test.c tests: Add virtio-balloon qtest 2014-03-13 01:21:56 +01:00
virtio-blk-test.c tests: Check QVIRTIO_F_ANY_LAYOUT flag in virtio-blk test 2015-03-10 14:02:23 +01:00
virtio-console-test.c virtio-console-test: Test virtserialport as well 2014-03-13 21:12:07 +01:00
virtio-net-test.c tests: test rx recovery from cont 2015-08-04 09:41:28 +01:00
virtio-rng-test.c tests: virtio-rng: Check if hot-plug/unplug works 2014-10-15 05:03:12 +02:00
virtio-scsi-test.c virtio-scsi-test: Add test case for tail unaligned WRITE SAME 2015-07-30 15:44:49 +02:00
virtio-serial-test.c tests: virtio-serial: Check if hot-plug/unplug works 2014-10-15 05:03:12 +02:00
vmxnet3-test.c tests: Add vmxnet3 qtest 2014-02-14 16:22:33 +01:00
wdt_ib700-test.c timer: rename NSEC_PER_SEC due to Mac OS X header clash 2015-07-20 17:01:00 +01:00