From: Markus Armbruster <armbru@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Maydell <peter.maydell@linaro.org>,
QEMU Developers <qemu-devel@nongnu.org>
Subject: Re: use of uninitialized variable involving visit_type_uint32() and friends
Date: Mon, 04 Apr 2022 08:24:02 +0200 [thread overview]
Message-ID: <87ee2d1i4d.fsf@pond.sub.org> (raw)
In-Reply-To: <33548764-9f91-b4df-c2b6-b897713d56fd@redhat.com> (Paolo Bonzini's message of "Fri, 1 Apr 2022 17:46:39 +0200")
Paolo Bonzini <pbonzini@redhat.com> writes:
> On 4/1/22 15:11, Markus Armbruster wrote:
>>> If it can do really serious interprocedural analysis, it _might_ be able
>>> to see through the visitor constructor and know that the "value = *obj"
>>> is not initialized (e.g. "all callers of object_property_set use an
>>> input visitor"). I doubt that honestly, but a man can dream.
>>
>> I'm wary of arguments based on "a sufficiently smart compiler can"...
>
> Absolutely.
>
>>> Because it communicates what the caller expects: "I have left this
>>> uninitialized because I expect my "v" argument to be the kind of visitor
>>> that fills it in". It's this argument that gives me the confidence
>>> needed to shut up Coverity's false positives.
>>>
>>> Embedding the visitor type in the signature makes it impossible not to
>>> pass it, unlike e.g. an assertion in every getter or setter.
>>
>> I think we got two kinds of code calling visitor methods:
>>
>> 1. Code for use with one kind of visitor only
>>
>> We get to pass a literal argument to the additional parameter you
>> propose.
>>
>> 2. Code for use with arbitrary visitors (such as qapi-visit*.c)
>>
>> We need to pass v->type, where @v is the existing visitor argument.
>> Except we can't: struct Visitor and VisitorType are private, defined
>> in <visitor-impl.h>. Easy enough to work around, but has a distinct
>> "this design is falling apart" smell, at least to me.
>
> Hmm, maybe that's a feature though. If we only need v->type in .c files
> for the generated visit_type_* functions, then it's not a huge deal that
> they will have to include <visitor-impl.h>. All callers outside
> generated type visitors (which includes for example QMP command
> marshaling), instead, would _have_ to pass visitor type constants and
> make it clear what direction the visit is going.
I quoted the generated qapi-visit*.c as an example. There may
handwritten instances, too.
>> Note that "intent explicit in every method call" is sufficient, but not
>> necessary for "intent is locally explicit, which lets us dismiss false
>> positives with confidence". We could do "every function that calls
>> methods". Like checking a precondition. We already have
>> visit_is_input(). We could have visit_is_output().
>>
>> The sane way to make output intent explicit is of course passing the
>> thing by value rather than by reference. To get that, we could generate
>> even more code. So, if the amount of code we currently generate isn't
>> disgusting enough, ...
>
> Yeah, that would be ugly. Or, we could generate the same code plus some
> static inline wrappers that take a
>
> struct InputVisitor {
> Visitor dont_use_me_it_hurts;
> }
> struct OutputVisitor {
> Visitor dont_use_me_it_hurts;
> }
>
> That would be zero-cost abstraction at runtime.
Looks worth exploring!
next prev parent reply other threads:[~2022-04-04 6:28 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-31 17:35 use of uninitialized variable involving visit_type_uint32() and friends Peter Maydell
2022-03-31 22:27 ` Daniel Henrique Barboza
2022-04-01 8:07 ` Paolo Bonzini
2022-04-01 9:15 ` Markus Armbruster
2022-04-01 11:16 ` Paolo Bonzini
2022-04-01 13:11 ` Markus Armbruster
2022-04-01 15:46 ` Paolo Bonzini
2022-04-04 6:24 ` Markus Armbruster [this message]
2022-06-27 13:33 ` Peter Maydell
2022-06-27 15:33 ` Markus Armbruster
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=87ee2d1i4d.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--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).