qapi: Don't cast Enum* to int*

C compilers are allowed to represent enums as a smaller type
than int, if all enum values fit in the smaller type. There
are even compiler flags that force the use of this smaller
representation, although using them changes the ABI of a
binary. Therefore, our generated code for visit_type_ENUM()
(for all qapi enums) was wrong for casting Enum* to int* when
calling visit_type_enum().

It appears that no one has been using compiler ABI switches
for qemu, because if they had, we are potentially dereferencing
beyond bounds or even risking a SIGBUS on platforms where
unaligned pointer dereferencing is fatal. But it is still
better to avoid the practice entirely, and just use the correct
types.

This matches the fix for alternate qapi types, done earlier in
commit 0426d53 "qapi: Simplify visiting of alternate types",
with generated code changing as:

| void visit_type_QType(Visitor *v, QType *obj, const char *name, Error **errp)
| {
|- visit_type_enum(v, (int *)obj, QType_lookup, "QType", name, errp);
|+ int value = *obj;
|+ visit_type_enum(v, &value, QType_lookup, "QType", name, errp);
|+ *obj = value;
| }

Backports commit 395a233f7c089f23e3c0d43ce34c709dc5acd7de from qemu
This commit is contained in:
Eric Blake 2018-02-19 22:23:26 -05:00 committed by Lioncash
parent 7e83274012
commit b978871f20
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
2 changed files with 12 additions and 5 deletions

View file

@ -52,12 +52,16 @@ out:
void visit_type_QType(Visitor *v, QType *obj, const char *name, Error **errp)
{
visit_type_enum(v, (int *)obj, QType_lookup, "QType", name, errp);
int value = *obj;
visit_type_enum(v, &value, QType_lookup, "QType", name, errp);
*obj = value;
}
void visit_type_QapiErrorClass(Visitor *v, QapiErrorClass *obj, const char *name, Error **errp)
{
visit_type_enum(v, (int *)obj, QapiErrorClass_lookup, "QapiErrorClass", name, errp);
int value = *obj;
visit_type_enum(v, &value, QapiErrorClass_lookup, "QapiErrorClass", name, errp);
*obj = value;
}
static void visit_type_X86CPUFeatureWordInfo_fields(Visitor *v, X86CPUFeatureWordInfo **obj, Error **errp)
@ -134,7 +138,9 @@ out:
void visit_type_X86CPURegister32(Visitor *v, X86CPURegister32 *obj, const char *name, Error **errp)
{
visit_type_enum(v, (int *)obj, X86CPURegister32_lookup, "X86CPURegister32", name, errp);
int value = *obj;
visit_type_enum(v, &value, X86CPURegister32_lookup, "X86CPURegister32", name, errp);
*obj = value;
}
void visit_type_anyList(Visitor *v, anyList **obj, const char *name, Error **errp)

View file

@ -175,12 +175,13 @@ out:
def gen_visit_enum(name):
# FIXME cast from enum *obj to int * invalidly assumes enum is int
return mcgen('''
void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name, Error **errp)
{
visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp);
int value = *obj;
visit_type_enum(v, &value, %(c_name)s_lookup, "%(name)s", name, errp);
*obj = value;
}
''',
c_name=c_name(name), name=name)