unicorn/msvc
Eric Blake 2ee6c960ee
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).

Backports commit 0426d53c6530606bf7641b83f2b755fe61c280ee from qemu
2018-02-19 21:52:39 -05:00
..
samples msvc: Update projects for VS2017. 2018-01-03 19:39:58 -05:00
unicorn qapi: Simplify visiting of alternate types 2018-02-19 21:52:39 -05:00
.gitignore Finalise MSVC port (#739) 2017-01-24 22:09:33 +08:00
README.TXT Msvc readme.txt updated (#803) 2017-04-21 15:54:53 +08:00
unicorn.sln Added MSVC support for arm64eb. 2017-04-25 14:23:58 +10:00

Unicorn-Engine MSVC Native Port Notes

Zak Escano  -  January 2017

These notes are to help myself and others with the upkeep of the msvc native port
of unicorn-engine.

:: Build settings

Visual Studio Version:   Visual Studio 2012  v11.061219.00 Update 5
Platform Toolset:        Visual Studio 2012 - Windows XP (v110_xp)
Character Set:           Use Multi-Byte Character Set
Runtime Library Debug:   Multi-threaded Debug (/MTd)
Runtime Library Release: Multi-threaded (/MT)
Precompiled Header:      Not Using Precompiled Headers
Additional Options:      /wd4018 /wd4244 /wd4267 


:: Changes porting unicorn from GNU/GCC to MSVC.

There were many many many changes to make this also build in MSVC
while still retaining the ability to build in GNU/GCC.
Most were due to either GCC specific things or MSVC lack of decent
standard C support especially in VS2012. Also some were for
posix/platform specific stuff that is not present in windows.

Some of the more common changes were:

* Compatibility for GCC style __attribute__'s.

* Change GCC switch case ranges to specify every case individually, ie:
  "case 1 ... 3:" changes to "case 1: case 2: case 3:"

* Change GCC struct member initialisation to the more generic
  initialisation of all members in order, ie:
  { .value = 1, .stuff = 2 } to { 1, 2 }

* Remove GCC style macro return values which MSVC does not support, ie:
  #define RETURN_ONE(x)  ({ some stuff; (void)1; })

* Compatibility for posix headers that are missing in windows, ie:
  stdbool.h, stdint.h, sys/time.h, unistd.h


:: CPU specific libraries

The gnu/gcc way of building the qemu portion of unicorn-engine involves makefile magic
that builds the same set of sourcecode files multiple times. They are built once for each
supported CPU type and force "#include" a CPU specific header file to re-"#define"
function and variable names that would otherwise be the same for each build.
These multiple builds of the same files are then all linked together to form
the unicorn library.

As an example when building for "x86_64" cpu type the generated header file "x86_64.h"
is force included and it contains a bunch of defines such as:
	#define phys_mem_clean     phys_mem_clean_x86_64
So you can see that it adds the cpu type on to the end of each name in order
to keep the names unique over the multiple builds.

The way I handle this in MSVC is to build a seperate cpu specific library, containing
this set of repeatedly used sourcecode files, for each supported cpu type.
These cpu specific libraries are then linked together to build the unicorn library.

For each supported CPU type

* Each CPU specific lib has a "forced include" file specified at:
	Configuration Properties -> C/C++ -> Advanced -> Forced Include File
	so for x86-64 this is "the file "x86_64.h" which is a generated file.


:: Other things

* The Qemu code for GNU/GCC seems to rely on __i386__ or __x86_64__ defined if
  the host is 32bit or 64bit respectively.
  So when building 32bit libs in msvc we define __i386__.
  And when building 64bit libs in msvc we define __x86_64__.

* There is a tcg-target.c for each target that is included into tcg.c.
  This is done using "#include tcg-target.c"
  It is NOT built separately as part of the *.c files for the project.


:: Info from makefiles

This info is compiled here together to help with deciding on the build settings to use.
It may or may not be of use to anyone in the future once this all builds ok :)

QEMU_INCLUDES=-I$(SRC_PATH)/tcg -I$(SRC_PATH)/tcg/$(ARCH) -I. -I$(SRC_PATH) -I$(SRC_PATH)/include
QEMU_CFLAGS=-m32 -D__USE_MINGW_ANSI_STDIO=1 -DWIN32_LEAN_AND_MEAN -DWINVER=0x501 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common -DUNICORN_HAS_X86 -DUNICORN_HAS_ARM -DUNICORN_HAS_M68K -DUNICORN_HAS_ARM64 -DUNICORN_HAS_MIPS -DUNICORN_HAS_MIPSEL -DUNICORN_HAS_MIPS64 -DUNICORN_HAS_MIPS64EL -DUNICORN_HAS_SPARC -fPIC 
QEMU_CFLAGS += -I.. -I$(SRC_PATH)/target-$(TARGET_BASE_ARCH) -DNEED_CPU_H
QEMU_CFLAGS+=-I$(SRC_PATH)/include
QEMU_CFLAGS+=-include x86_64.h

	includes
-I$(SRC_PATH)/tcg
-I$(SRC_PATH)/tcg/$(ARCH)
-I.
-I$(SRC_PATH)
-I$(SRC_PATH)/include
-I..
-I$(SRC_PATH)/target-$(TARGET_BASE_ARCH)
-I$(SRC_PATH)/include
-include x86_64.h

	defines
-D__USE_MINGW_ANSI_STDIO=1
-DWIN32_LEAN_AND_MEAN
-DWINVER=0x501
-D_GNU_SOURCE
-D_FILE_OFFSET_BITS=64
-D_LARGEFILE_SOURCE
-DNEED_CPU_H
-DUNICORN_HAS_X86
-DUNICORN_HAS_ARM
-DUNICORN_HAS_M68K
-DUNICORN_HAS_ARM64
-DUNICORN_HAS_MIPS
-DUNICORN_HAS_MIPSEL
-DUNICORN_HAS_MIPS64
-DUNICORN_HAS_MIPS64EL
-DUNICORN_HAS_SPARC


	qemu/config-host.mak
		extra_cflags=-m32 -DUNICORN_HAS_X86 -DUNICORN_HAS_ARM -DUNICORN_HAS_M68K -DUNICORN_HAS_ARM64 -DUNICORN_HAS_MIPS -DUNICORN_HAS_MIPSEL -DUNICORN_HAS_MIPS64 -DUNICORN_HAS_MIPS64EL -DUNICORN_HAS_SPARC -fPIC
		extra_ldflags=
		libs_softmmu=
		ARCH=i386
		CONFIG_WIN32=y
		CONFIG_FILEVERSION=2,2,1,0
		CONFIG_PRODUCTVERSION=2,2,1,0
		VERSION=2.2.1
		PKGVERSION=
		SRC_PATH=/f/GitHub/unicorn/qemu
		TARGET_DIRS=x86_64-softmmu  arm-softmmu  m68k-softmmu  aarch64-softmmu  mips-softmmu  mipsel-softmmu  mips64-softmmu  mips64el-softmmu  sparc-softmmu sparc64-softmmu 
		GLIB_CFLAGS=-pthread -mms-bitfields -IC:/msys64/mingw32/include/glib-2.0 -IC:/msys64/mingw32/lib/glib-2.0/include
		CONFIG_ZERO_MALLOC=y
		CONFIG_CPUID_H=y
		CONFIG_THREAD_SETNAME_BYTHREAD=y
		CONFIG_PTHREAD_SETNAME_NP=y
		CFLAGS=-pthread -mms-bitfields -IC:/msys64/mingw32/include/glib-2.0 -IC:/msys64/mingw32/lib/glib-2.0/include -g 
		
		QEMU_CFLAGS=-m32 -D__USE_MINGW_ANSI_STDIO=1 -DWIN32_LEAN_AND_MEAN -DWINVER=0x501 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common -DUNICORN_HAS_X86 -DUNICORN_HAS_ARM -DUNICORN_HAS_M68K -DUNICORN_HAS_ARM64 -DUNICORN_HAS_MIPS -DUNICORN_HAS_MIPSEL -DUNICORN_HAS_MIPS64 -DUNICORN_HAS_MIPS64EL -DUNICORN_HAS_SPARC -fPIC 
		QEMU_INCLUDES=-I$(SRC_PATH)/tcg -I$(SRC_PATH)/tcg/$(ARCH) -I. -I$(SRC_PATH) -I$(SRC_PATH)/include
		LDFLAGS=-Wl,--nxcompat -Wl,--no-seh -Wl,--dynamicbase -Wl,--warn-common -m32 -g 
		LIBS+=-LC:/msys64/mingw32/lib -lgthread-2.0 -pthread -lglib-2.0 -lintl -lwinmm -lws2_32 -liphlpapi  -lz
	
	
	qemu/x86_64-softmmu/Makefile
		QEMU_CFLAGS += -I.. -I$(SRC_PATH)/target-$(TARGET_BASE_ARCH) -DNEED_CPU_H
		QEMU_CFLAGS+=-I$(SRC_PATH)/include


	qemu/x86_64-softmmu/config-target.mak
		TARGET_X86_64=y
		TARGET_NAME=x86_64
		TARGET_BASE_ARCH=i386
		TARGET_ABI_DIR=x86_64
		CONFIG_SOFTMMU=y
		LDFLAGS+=
		QEMU_CFLAGS+=
		QEMU_CFLAGS+=-include x86_64.h

	
	qemu/x86_64-softmmu/config-devices.mak
		CONFIG_VGA=y
		CONFIG_QXL=$(CONFIG_SPICE)
		CONFIG_VGA_PCI=y
		CONFIG_VGA_ISA=y
		CONFIG_VGA_CIRRUS=y
		CONFIG_VMWARE_VGA=y
		CONFIG_VMMOUSE=y
		CONFIG_SERIAL=y
		CONFIG_PARALLEL=y
		CONFIG_I8254=y
		CONFIG_PCSPK=y
		CONFIG_PCKBD=y
		CONFIG_FDC=y
		CONFIG_ACPI=y
		CONFIG_APM=y
		CONFIG_I8257=y
		CONFIG_IDE_ISA=y
		CONFIG_IDE_PIIX=y
		CONFIG_NE2000_ISA=y
		CONFIG_PIIX_PCI=y
		CONFIG_HPET=y
		CONFIG_APPLESMC=y
		CONFIG_I8259=y
		CONFIG_PFLASH_CFI01=y
		CONFIG_TPM_TIS=$(CONFIG_TPM)
		CONFIG_PCI_HOTPLUG_OLD=y
		CONFIG_MC146818RTC=y
		CONFIG_PAM=y
		CONFIG_PCI_PIIX=y
		CONFIG_WDT_IB700=y
		CONFIG_XEN_I386=$(CONFIG_XEN)
		CONFIG_ISA_DEBUG=y
		CONFIG_ISA_TESTDEV=y
		CONFIG_VMPORT=y
		CONFIG_SGA=y
		CONFIG_LPC_ICH9=y
		CONFIG_PCI_Q35=y
		CONFIG_APIC=y
		CONFIG_IOAPIC=y
		CONFIG_ICC_BUS=y
		CONFIG_PVPANIC=y
		CONFIG_MEM_HOTPLUG=y
		CONFIG_PCI=y
		CONFIG_VIRTIO_PCI=y
		CONFIG_VIRTIO=y
		CONFIG_USB_UHCI=y
		CONFIG_USB_OHCI=y
		CONFIG_USB_EHCI=y
		CONFIG_USB_XHCI=y
		CONFIG_NE2000_PCI=y
		CONFIG_EEPRO100_PCI=y
		CONFIG_PCNET_PCI=y
		CONFIG_PCNET_COMMON=y
		CONFIG_AC97=y
		CONFIG_HDA=y
		CONFIG_ES1370=y
		CONFIG_LSI_SCSI_PCI=y
		CONFIG_VMW_PVSCSI_SCSI_PCI=y
		CONFIG_MEGASAS_SCSI_PCI=y
		CONFIG_RTL8139_PCI=y
		CONFIG_E1000_PCI=y
		CONFIG_VMXNET3_PCI=y
		CONFIG_IDE_CORE=y
		CONFIG_IDE_QDEV=y
		CONFIG_IDE_PCI=y
		CONFIG_AHCI=y
		CONFIG_ESP=y
		CONFIG_ESP_PCI=y
		CONFIG_SERIAL=y
		CONFIG_SERIAL_PCI=y
		CONFIG_IPACK=y
		CONFIG_WDT_IB6300ESB=y
		CONFIG_PCI_TESTDEV=y
		CONFIG_NVME_PCI=y
		CONFIG_SB16=y
		CONFIG_ADLIB=y
		CONFIG_GUS=y
		CONFIG_CS4231A=y
		CONFIG_USB_TABLET_WACOM=y
		CONFIG_USB_STORAGE_BOT=y
		CONFIG_USB_STORAGE_UAS=y
		CONFIG_USB_STORAGE_MTP=y
		CONFIG_USB_SMARTCARD=y
		CONFIG_USB_AUDIO=y
		CONFIG_USB_SERIAL=y
		CONFIG_USB_NETWORK=y
		CONFIG_USB_BLUETOOTH=y