qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: marcandre.lureau@redhat.com, DirtY.iCE.hu@gmail.com,
	qemu-devel@nongnu.org, ehabkost@redhat.com,
	Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: Re: [Qemu-devel] [PATCH v5 03/46] qapi: Test for C member name collisions
Date: Tue, 22 Sep 2015 11:52:27 -0600	[thread overview]
Message-ID: <5601955B.4050109@redhat.com> (raw)
In-Reply-To: <87h9mmfpf7.fsf@blackfin.pond.sub.org>

[-- Attachment #1: Type: text/plain, Size: 11575 bytes --]

On 09/22/2015 09:23 AM, Markus Armbruster wrote:
> Eric Blake <eblake@redhat.com> writes:
> 
>> Expose some weaknesses in the generator: we don't always forbid
>> the generation of structs that contain multiple members that map
> 
> Slightly misleading.  args-name-clash is a clash between command
> arguments.  These are a struct internally, but we don't currently
> generate an actual struct for it, only an argument list.

Maybe struct-member-clash?  Renames are easy enough, but only if patch
1/46 is okay to go in first. :)

> 
>> to the same C name.  This has already been marked FIXME in
>> qapi.py, but having more tests will make sure future patches
>> produce desired behavior.
> 
> Point to commit d90675f?

Sure, now that it finally landed.

> 
>> Some of these tests will be deleted later, and a positive test
>> added to qapi-schema-test.json in its place, when the code is
> 
> "in their place"?
> 

Yep. (Perils of editing, I started with one test, then added more later
and merged into one patch)

>> reworked so that the collision no longer occurs.
>>
>> Signed-off-by: Eric Blake <eblake@redhat.com>
>> ---

>> +++ b/tests/qapi-schema/flat-union-branch-clash.json
>> @@ -1,4 +1,4 @@
>> -# we check for no duplicate keys between branches and base
>> +# we check for no duplicate keys between branch members and base
>>  { 'enum': 'TestEnum',
>>    'data': [ 'value1', 'value2' ] }
>>  { 'struct': 'Base',
> 
> This clashing business is awfully confusing as soon as unions come into
> play.  When I'm confused, I need to think in writing.

No kidding.  We already attempted to detect clashes, and caught some,
but not all, types of clashes.  And there are indeed two types of member
name clashes: those where the generated C struct has duplicate members
(either because 2 user names map to the same C name, or because the
generated code injects a C name for a purpose other than a "key":value
name), and those where the qapi type would specify the same "key":value
name more than once in the same {} object on the wire (even if the names
would not collide in C because one is accessed through a box pointer).
By patch 16/46, we should be catching all cases of member name clashes,
but there's still work to do to catch collisions in 'command' and/or
'event' names.

Also, by the time 16/46 is in, there are cases where we reject "clashes"
where two member names with different spellings would map to the same C
name, but where the corresponding C struct does not have a clash because
the members are boxed behind different pointers.  Technically, we would
not have to reject such cases, but the case is still confusing enough
that rejecting it forces the qapi writer to consider a naming convention
that is less confusing in the first place.

> 
> The basic case is clash between local, non-variant members.  Needs test
> coverage.  args-name-clash.json provides it, because internally the
> arguments are just another object type.

Correct.  The test proves we don't yet catch the clash, and is fixed
when later commits add the check.

> 
> With a base, the members inherited from base get added to the mix.  We
> need to test a clash betwen local, non-variant member and a member
> inherited from base.

True for both structs and flat unions (the two places where we use
'base').  More on this below.

> 
> With unions, things get complicated, because we have multiple kinds of
> clashes.  Best explained with an example.  Let's use UserDefFlatUnion
> from qapi-schema-test.json.
> 
>     { 'union': 'UserDefFlatUnion',
>       'base': 'UserDefUnionBase',   # intentional forward reference
>       'discriminator': 'enum1',
>       'data': { 'value1' : 'UserDefA',
>                 'value2' : 'UserDefB',
>                 'value3' : 'UserDefB' } }
> 
>     { 'struct': 'UserDefUnionBase',
>       'base': 'UserDefZero',
>       'data': { 'string': 'str', 'enum1': 'EnumOne' } }
> 
> Generated C looks like this:
> 
>     struct UserDefFlatUnion {
>         /* Members inherited from UserDefUnionBase: */
>         int64_t integer;
>         char *string;
>         EnumOne enum1;
>         /* Own members: */
>         // if the schema language supported adding non-variant local
>         // members, they'd go right here
>         union { /* union tag is @enum1 */
>             void *data;
>             UserDefA *value1;
>             UserDefB *value2;
>             UserDefB *value3;
>         };
>     };
> 
> Thus, what can clash in C is the tag values value1, value2, value3 with
> the non-variant members integer, string, enum1.

That is, the tag values now appear as C member names, even though they
did not correspond to QMP "key":value names.  Likewise, the 'data' C
member name can cause a clash.

Was even worse before commit 0f61af3e, where we were also burning the C
name 'kind'.

Commit 1e6c1616 was where we quit burning the C member name 'base'.
Prior to that time, members of base classes did not clash with variant
names because of the C boxing.

If we run into a situation where the enum values collide with base
member names (both of which are ABI), we could still solve the collision
by renaming the C member names for the enum values to something that
don't collide (such as _tag_value1 rather than value1); this is because
the C member names are not ABI and can be changed.  But we can cross
that bridge later if the situation ever arises; for now, it's just
easier to patch the generator to reject qapi where such a collision
would occur.

> 
> On the wire, the union members are unboxed, i.e. we get just
> 
>     "boolean": false
> 
> instead of
> 
>     "value1": { "boolean": false }
> 
> Thus what can clash on the wire is the variant members with the
> non-variant members: boolean with integer, string, enum1 when enum1 is
> value1, and so forth.
> 
> This is the clash flat-union-branch-clash.json tests.  Its error message
> is "Member name 'name' of branch 'value1' clashes with base 'Base'".
> Suboptimal, it should say "with member 'name' of base 'Base'".

Indeed, this is the other type of clash (QMP wire clashes, whether or
not they cause a C member clash).

>> +++ b/tests/qapi-schema/flat-union-branch-clash2.json
>> @@ -0,0 +1,14 @@
>> +# FIXME: we should check for no duplicate C names between branches and base
>> +{ 'enum': 'TestEnum',
>> +  'data': [ 'base', 'c-d' ] }
>> +{ 'struct': 'Base',
>> +  'data': { 'enum1': 'TestEnum', '*c_d': 'str' } }
>> +{ 'struct': 'Branch1',
>> +  'data': { 'string': 'str' } }
>> +{ 'struct': 'Branch2',
>> +  'data': { 'value': 'int' } }
>> +{ 'union': 'TestUnion',
>> +  'base': 'Base',
>> +  'discriminator': 'enum1',
>> +  'data': { 'base': 'Branch1',
>> +            'c-d': 'Branch2' } }
> 
> This tests the other kind of clash: tag value 'c-d' clashes with
> non-variant member name 'c_d'.
> 
> Please add a comment explaining what clash should be reported here.

Will do; and by the end of the series the error is properly reported.

>> +++ b/tests/qapi-schema/flat-union-cycle.json
>> @@ -0,0 +1,6 @@
>> +# we reject a loop in flat unions, due to member collision
>> +{ 'enum': 'Enum', 'data': [ 'okay', 'loop' ] }
>> +{ 'struct': 'Base', 'data': { 'switch': 'Enum' } }
>> +{ 'struct': 'Okay', 'data': { 'int': 'int' } }
>> +{ 'union': 'Union', 'base': 'Base', 'discriminator': 'switch',
>> +  'data': { 'okay': 'Okay', 'loop': 'Base' } }
> 
> This isn't a loop, it's a fork: we get the members of Base via its use
> as base, and again via its use as type of a variant case.
> 
> What does it add over flat-union-branch-clash.json?

I wrote this test when I discovered the assertion failure in the parser
bug as covered by patch 16/46 (a struct attempting to inherit directly
or indirectly from itself is not nice). When I first wrote the test, I
was trying to make sure that a flat union cannot inherit from itself,
but then ran into the problem that a base class must be a struct and not
a union.  So I changed the test to make sure that QMP cannot reuse the
base class as a variant type, since that would require the members of
the base type to occur in QMP more than once, without seeing if any
other test already did that.

You may have a point that this doesn't cover anything beyond
flat-union-branch-clash, and since my later changes to detect
self-inheritance didn't change the error message flagged for this case,
we can probably safely drop this test as not adding anything.

And I guess I should still test that self-inheritance attempts are
rejected, even if we later relax things to allow a non-struct as a base
class.


>> +++ b/tests/qapi-schema/qapi-schema-test.json
>> @@ -32,11 +32,12 @@
>>              'dict1': 'UserDefTwoDict' } }
>>
>>  # for testing unions
>> +# name collisions between branches should not clash
>>  { 'struct': 'UserDefA',
>> -  'data': { 'boolean': 'bool' } }
>> +  'data': { 'boolean': 'bool', '*a_b': 'int' } }
>>
>>  { 'struct': 'UserDefB',
>> -  'data': { 'intb': 'int' } }
>> +  'data': { 'intb': 'int', '*a-b': 'bool' } }
>>
>>  { 'union': 'UserDefFlatUnion',
>>    'base': 'UserDefUnionBase',   # intentional forward reference
> 
> This tests that different variants may have clashing names.  Okay.

That is, even though the variant is accepted at the same QMP {} level,
only one variant at a time can be active, so clashes in names between
variants is not fatal to either QMP or to the generated C code.

> 
> I'm afraid the comment is a bit too terse.  Not sure I'd make the
> connection from it to member a_b and to UserDefB's a-b a fortnight from
> now.
> 

Then I get to beef it up for the next round :)


>> +++ b/tests/qapi-schema/struct-base-clash2.json
>> @@ -0,0 +1,5 @@
>> +# FIXME - a base class collides with a member named base
>> +{ 'struct': 'Base', 'data': {} }
>> +{ 'struct': 'Sub',
>> +  'base': 'Base',
>> +  'data': { 'base': 'str' } }
> 
> What's this about?  Hmm, I think it's about the way we do a struct
> type's base.  For a union type, we add the base's members, as shown
> above.  For a struct type, we add the base *boxed*, like this:
> 
>     struct Sub {
>         // The base type
>         Base *base;
>         // Own members
>         char *base;
>     };
> 
> Therefore, a struct type with a base can't have a member named base.
> But that's simply daft.  As soon as we change it to match union types,
> this test case goes away.  If we change it soon, do we still need this
> test?  Will it be done later in this series?

Yes, we fix it up later in the series, at which point this test
disappears. But having the test now makes it easier to see what the
later patch is changing.

>> +++ b/tests/qapi-schema/union-clash2.json
>> @@ -0,0 +1,3 @@
>> +# FIXME - a union branch named 'data' collides with generated C code
>> +{ 'union': 'TestUnion',
>> +  'data': { 'data': 'int' } }
> 
> This tests another stupid clash: we put a member named data in our
> generated unions.  As soon as we stop doing that, this test will go
> away.  If we stop soon, do we still need this test?  Will we stop later
> in this series?

Yes, we fix it up later in the series, at which point this test
disappears. But having the test now makes it easier to see what the
later patch is changing.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

  reply	other threads:[~2015-09-22 17:52 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-21 21:57 [Qemu-devel] [PATCH v5 00/46] post-introspection cleanups, and qapi-ify netdev_add Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 01/46] qapi: Sort qapi-schema tests Eric Blake
2015-09-23 14:26   ` Eric Blake
2015-09-23 15:09     ` Markus Armbruster
2015-09-23 15:19       ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 02/46] qapi: Clean up qapi.py per pep8 Eric Blake
2015-09-22 14:00   ` Markus Armbruster
2015-09-22 14:58     ` Eric Blake
2015-09-23  9:20       ` Markus Armbruster
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 03/46] qapi: Test for C member name collisions Eric Blake
2015-09-22 15:23   ` Markus Armbruster
2015-09-22 17:52     ` Eric Blake [this message]
2015-09-23  9:43       ` Markus Armbruster
2015-09-23 12:45         ` Eric Blake
2015-09-23 14:02           ` Markus Armbruster
2015-09-23 14:19             ` Eric Blake
2015-09-23 15:12               ` Markus Armbruster
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 04/46] qapi: Add tests for empty unions Eric Blake
2015-09-24 14:16   ` Markus Armbruster
2015-09-24 15:52     ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 05/46] qapi: Test use of 'number' within alternates Eric Blake
2015-09-24 14:36   ` Markus Armbruster
2015-09-24 16:00     ` Eric Blake
2015-09-24 16:29       ` Markus Armbruster
2015-09-25 22:32         ` Eric Blake
2015-09-28  9:26           ` Markus Armbruster
2015-09-25 22:50         ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 06/46] qapi: Improve 'include' error message Eric Blake
2015-09-24 14:39   ` Markus Armbruster
2015-09-24 16:04     ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 07/46] qapi: Don't pass pre-existing error to later call Eric Blake
2015-09-24 14:58   ` Markus Armbruster
2015-09-24 16:14     ` Eric Blake
2015-09-26 21:05       ` Eric Blake
2015-09-28  9:14         ` Markus Armbruster
2015-10-06 21:10           ` [Qemu-devel] [RFC PATCH] qapi: split visit_end_struct() into pieces Eric Blake
2015-10-07 12:00             ` Markus Armbruster
2015-10-07 13:08               ` Markus Armbruster
2015-10-07 14:57               ` Eric Blake
2015-10-07 15:23                 ` Markus Armbruster
2015-09-26 21:41     ` [Qemu-devel] [PATCH v5 07/46] qapi: Don't pass pre-existing error to later call Eric Blake
2015-09-27  2:26       ` Eric Blake
2015-09-28  9:24       ` Markus Armbruster
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 08/46] qapi: Reuse code for flat union base validation Eric Blake
2015-09-25 16:30   ` Markus Armbruster
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 09/46] qapi: Use consistent generated code patterns Eric Blake
2015-09-25 16:54   ` Markus Armbruster
2015-09-25 19:06     ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 10/46] qapi: Merge generation of per-member visits Eric Blake
2015-09-28  6:17   ` Markus Armbruster
2015-09-28 15:40     ` Eric Blake
2015-09-29  7:37       ` Markus Armbruster
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 11/46] qapi: Don't use info as witness of implicit object type Eric Blake
2015-09-28 12:43   ` Markus Armbruster
2015-09-29  3:58     ` Eric Blake
2015-09-29  7:51       ` Markus Armbruster
2015-09-30  4:13         ` [Qemu-devel] [RFC PATCH] qapi: Use callback to determine visit filtering Eric Blake
2015-10-01  6:12           ` Markus Armbruster
2015-10-01 14:09             ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 12/46] qapi: Track location that created an implicit type Eric Blake
2015-09-28 12:56   ` Markus Armbruster
2015-09-29  4:03     ` Eric Blake
2015-09-29  8:02       ` Markus Armbruster
2015-09-30 16:02         ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 13/46] qapi: Track owner of each object member Eric Blake
2015-09-30 16:06   ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 14/46] qapi: Detect collisions in C member names Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 15/46] qapi: Defer duplicate member checks to schema check() Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 16/46] qapi: Detect base class loops Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 17/46] qapi: Provide nicer array names in introspection Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 18/46] qapi-introspect: Guarantee particular sorting Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 19/46] qapi: Simplify visiting of alternate types Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 20/46] qapi: Fix alternates that accept 'number' but not 'int' Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 21/46] qmp: Fix reference-counting of qnull on empty output visit Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 22/46] qapi: Don't abuse stack to track qmp-output root Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 23/46] qapi: Remove dead visitor code Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 24/46] qapi: Document visitor interfaces Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 25/46] qapi: Plug leaks in test-qmp-input-visitor Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 26/46] qapi: Test failure in middle of array parse Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 27/46] qapi: Simplify visits of optional fields Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 28/46] qapi: Rework deallocation of partial struct Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 29/46] qapi: Change visit_type_FOO() to no longer return partial objects Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 30/46] net: use Netdev instead of NetClientOptions in client init Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 31/46] qapi: use 'type' in generated C code to match QMP union wire form Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 32/46] qapi: Hide tag_name data member of variants Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 33/46] vnc: hoist allocation of VncBasicInfo to callers Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 34/46] qapi: Unbox base members Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 35/46] qapi-visit: Remove redundant functions for flat union base Eric Blake
2015-09-23 20:55   ` Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 36/46] qapi: Avoid use of 'data' member of qapi unions Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 37/46] qapi: Forbid empty unions and useless alternates Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 38/46] qapi: Drop useless 'data' member of unions Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 39/46] qapi: Plumb in 'box' to qapi generator lower levels Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 40/46] qapi: Implement boxed structs for commands/events Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 41/46] qapi: Support boxed unions Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 42/46] qapi: support implicit structs in OptsVisitor Eric Blake
2015-09-21 21:57 ` [Qemu-devel] [PATCH v5 43/46] qapi: Change Netdev into a flat union Eric Blake
2015-09-21 21:58 ` [Qemu-devel] [PATCH v5 44/46] net: Use correct type for bool flag Eric Blake
2015-09-21 21:58 ` [Qemu-devel] [PATCH v5 45/46] net: Complete qapi-fication of netdev_add Eric Blake
2015-09-23 15:40   ` Paolo Bonzini
2015-09-23 16:37     ` Eric Blake
2015-09-25 16:48       ` Paolo Bonzini
2015-09-28  9:31         ` Markus Armbruster
2015-09-28 11:29           ` Paolo Bonzini
2015-09-21 21:58 ` [Qemu-devel] [PATCH v5 46/46] qapi: Allow anonymous base for flat union Eric Blake
2015-09-23 20:59   ` Eric Blake
2015-09-28 13:07 ` [Qemu-devel] [PATCH v5 00/46] post-introspection cleanups, and qapi-ify netdev_add Markus Armbruster
2015-09-29  3:43   ` Eric Blake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5601955B.4050109@redhat.com \
    --to=eblake@redhat.com \
    --cc=DirtY.iCE.hu@gmail.com \
    --cc=armbru@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).