All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: Fam Zheng <famz@redhat.com>,
	qemu-devel@nongnu.org, wenchaoqemu@gmail.com,
	Luiz Capitulino <lcapitulino@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v3 09/14] qapi: add check_type helper function
Date: Thu, 14 Aug 2014 18:10:11 +0200	[thread overview]
Message-ID: <87d2c36oik.fsf@blackfin.pond.sub.org> (raw)
In-Reply-To: <53ECAEB6.5070602@redhat.com> (Eric Blake's message of "Thu, 14 Aug 2014 06:42:30 -0600")

Eric Blake <eblake@redhat.com> writes:

> On 08/14/2014 04:10 AM, Markus Armbruster wrote:
>> Subject suggests you're merely adding a helper function.  You're
>> actually fixing bugs.  Something like
>> 
>>     qapi: More rigorous checking of type expressions
>> 
>> would be clearer.
>
> Along with squashing in 8/14, you are correct about this fixing bugs
> (serves me right for refactoring, then realizing that we needed more
> tests, then fixing the tests without checking that the subject line
> stayed appropriate).
>
>>> +++ b/scripts/qapi.py
>>> @@ -329,6 +329,32 @@ def check_union(expr, expr_info):
>>>          # Todo: add checking for values. Key is checked as above,
>>> value can be
>>>          # also checked here, but we need more functions to handle
>>> array case.
>>>
>>> +def check_type(expr_info, name, data, allow_native = False):
>>> +    if not data:
>>> +        return
>>> +    if isinstance(data, list):
>>> +        if len(data) != 1 or not isinstance(data[0], basestring):
>>> +            raise QAPIExprError(expr_info,
>>> +                                "Use of array type in '%s' must contain "
>>> +                                "single element type name" % name)
>>> +        data = data[0]
>> 
>> Peeling off the array here means error messages below won't mention it.
>> Visible in data-array-unknown.err below.  But good enough is good
>> enough.
>> 
>>> +
>>> +    if isinstance(data, basestring):
>>> +        if find_struct(data) or find_enum(data) or find_union(data):
>>> +            return
>>> +        if data == 'str' or data == 'int' or data == 'visitor':
>> 
>> Is this complete?  Should we be checking against builtin_types[]?
>
> It was complete insofar that it passes with the current
> qapi-schema.json.  Checking builtin_types[] would indeed be nicer (I
> didn't know that existed).
>
>> 
>> Pardon my ignorance: where does 'visitor' come from?
>
> qom-set, qom-get.  Nasty commands that aren't typesafe, and which
> explicitly use 'gen':'no' to abuse the qapi system by bypassing the code
> generator while still exposing a backdoor command that can break the rules.
>
> netdev_add ('*props':'**') and object-add ('*props':'dict') are the only
> other 'gen':'no' clients; I'm not sure why they didn't cause the parser
> to barf the way 'visitor' did.  Maybe my approach should instead be to
> unify all four 'gen':'no' expressions to use the same syntax of "**" to
> represent the fact that the QMP code is not type-safe, as well as
> actually documenting this (ab)use of ** (the fact that none of the
> documentation mentions 'gen' is telling).  Looks like I've just added a
> pre-req patch into my v4 :)

Documentation for 'gen': 'no', '**', and 'visitor' is most welcome.

I wonder why 'no', and not simply false.  Hmm, looks like the code
generator ignores the value.  'gen': 'sure, why not, if you feel like
it'.

Should 'visitor' be in builtin_types[]?

>>> +            if allow_native:
>>> +                return
>>> +            raise QAPIExprError(expr_info,
>>> +                                "Primitive type '%s' not allowed as data "
>>> +                                "of '%s'" % (data, name))
>>> +        raise QAPIExprError(expr_info,
>>> +                            "Unknown type '%s' as data of '%s'"
>>> +                            % (data, name))
>> 
>> "as data of" suggests the problem is in member 'data', even when it's
>> actually in member 'returns'.  Visible in returns-unknown.err below.
>
> The function is declared as:
>
>>> +def check_type(expr_info, name, data, allow_native = False):
>
> but allow_native is True only for the 'returns' case.  Maybe I should
> just repurpose that parameter for what it really is - 'data' vs.
> 'returns', and use it for improving the error message in addition to
> deciding whether native types are allowed.

Yeah.

>>> +    elif not isinstance(data, OrderedDict):
>>> +        raise QAPIExprError(expr_info,
>>> +                            "Expecting dictionary for data of '%s'" % name)
>>> +
>>>  def check_exprs(schema):
>>>      for expr_elem in schema.exprs:
>>>          expr = expr_elem['expr']
>>> @@ -356,6 +382,10 @@ def check_exprs(schema):
>>>                  raise QAPIExprError(info,
>>>                                      "enum '%s' requires an array for 'data'"
>>>                                      % expr.get('enum'))
>>> +        else:
>> 
>> This is for 'type' and 'command', right?
>
> 'type', 'union', 'event', and 'command' ('data' is a dict for those
> four, and an array for the fifth meta-type of 'enum').  I'll add a comment.

You're right.  I misread the sequence of if statements as one big
if... elif... elif... else statement.

A blank line before the if... else would probably do.

>> 
>>> +            check_type(info, expr_name(expr), members)
>>> +            if expr.has_key('command') and expr.has_key('returns'):
>>> +                check_type(info, expr['command'], expr['returns'], True)
>>>
>>>  def parse_schema(input_file):
>>>      try:
>> 
>> Looks pretty good, but I didn't check systematically against
>> qapi-code-gen.txt for correctness and completeness.  We can always
>> improve on top.
>
> I'll double-check things as well, when posting v4.

Thanks!

  reply	other threads:[~2014-08-14 16:10 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-06  1:14 [Qemu-devel] [PATCH v3 00/14] drop qapi nested structs Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 01/14] qapi: consistent whitespace in tests/Makefile Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 02/14] qapi: ignore files created during make check Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 03/14] qapi: add some enum tests Eric Blake
2014-08-14  9:23   ` Markus Armbruster
2014-08-14 12:21     ` Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 04/14] qapi: better error message for bad enum Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 05/14] qapi: add some expr tests Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 06/14] qapi: require valid expressions Eric Blake
2014-08-14  9:38   ` Markus Armbruster
2014-08-14 12:26     ` Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 07/14] qapi: add some type check tests Eric Blake
2014-08-14  9:47   ` Markus Armbruster
2014-08-14 12:26     ` Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 08/14] qapi: add expr_name() helper Eric Blake
2014-08-14  9:49   ` Markus Armbruster
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 09/14] qapi: add check_type helper function Eric Blake
2014-08-14 10:10   ` Markus Armbruster
2014-08-14 12:42     ` Eric Blake
2014-08-14 16:10       ` Markus Armbruster [this message]
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 10/14] qapi: merge UserDefTwo and UserDefNested in tests Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 11/14] qapi: drop tests for inline subtypes Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 12/14] qapi: drop inline subtype in query-version Eric Blake
2014-08-14 11:45   ` Markus Armbruster
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 13/14] qapi: drop inline subtype in query-pci Eric Blake
2014-08-06  1:14 ` [Qemu-devel] [PATCH v3 14/14] qapi: drop support for inline subtypes Eric Blake
2014-08-12 12:47 ` [Qemu-devel] [PATCH v3 00/14] drop qapi nested structs Eric Blake
2014-08-14 12:00 ` Markus Armbruster
2014-08-15 18:12 ` Luiz Capitulino

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=87d2c36oik.fsf@blackfin.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=lcapitulino@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=wenchaoqemu@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.