From: Greg Kurz <groug@kaod.org>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: "Daniel P . Berrange" <berrange@redhat.com>,
"QEMU Developers" <qemu-devel@nongnu.org>,
"Markus Armbruster" <armbru@redhat.com>,
qemu-ppc <qemu-ppc@nongnu.org>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"David Gibson" <david@gibson.dropbear.id.au>
Subject: Re: [for-5.0 PATCH] ppc: Make PPCVirtualHypervisor an incomplete type
Date: Mon, 9 Dec 2019 22:04:04 +0100 [thread overview]
Message-ID: <20191209220404.4b1539c8@bahia.w3ibm.bluemix.net> (raw)
In-Reply-To: <CAFEAcA-WFd9XVXS5bt4sWOw9BzuS=ODTDneTUJzQ4z8Mks4X3A@mail.gmail.com>
On Mon, 9 Dec 2019 16:42:39 +0000
Peter Maydell <peter.maydell@linaro.org> wrote:
> On Mon, 9 Dec 2019 at 16:28, Greg Kurz <groug@kaod.org> wrote:
> >
> > On Mon, 9 Dec 2019 15:02:38 +0100
> > Philippe Mathieu-Daudé <philmd@redhat.com> wrote:
> >
> > > On 12/9/19 2:28 PM, Greg Kurz wrote:
> > > > PPCVirtualHypervisor is an interface instance. It should never be
> > > > dereferenced. Drop the dummy type definition for extra safety, which
> > > > is the common practice with QOM interfaces.
> > >
> > > This "common practice" is also referenced in commit 00ed3da9b5:
> > >
> > > xics: Minor fixes for XICSFabric interface
> > >
> > > Interface instances should never be directly dereferenced. So, the
> > > common
> > > practice is to make them incomplete types to make sure no-one does
> > > that.
> > > XICSFrabric, however, had a dummy type which is less safe.
> > >
> > > We were also using OBJECT_CHECK() where we should have been using
> > > INTERFACE_CHECK().
> > >
> > > This indeed follow the changes from commit aa1b35b975d8:
> > >
> > > qom: make interface types abstract
> > >
> > > Interfaces don't have instance, let's make the interface type really
> > > abstract to avoid confusion.
> > >
> > > Now I can't find guidelines for this. If you don't know about it and use
> > > 'git-grep', it is very confusing to see we use structures we never define.
> > >
> >
> > I agree that this deliberate usage of incomplete types isn't common.
> >
> > > Can we document this use please?
> > >
> >
> > Probably we could amend the related section in the object.h header file.
> > Something like:
> >
> > --- a/include/qom/object.h
> > +++ b/include/qom/object.h
> > @@ -200,8 +200,11 @@ typedef struct InterfaceInfo InterfaceInfo;
> > *
> > * Interfaces allow a limited form of multiple inheritance. Instances are
> > * similar to normal types except for the fact that are only defined by
> > - * their classes and never carry any state. You can dynamically cast an object
> > - * to one of its #Interface types and vice versa.
> > + * their classes and never carry any state. As a consequence, a pointer to
> > + * an interface instance should always be of incomplete type in order to be
> > + * sure it cannot be dereferenced.
>
> It might be helpful to add here the concrete details of how to do that,
> so people don't have to look up what an incomplete type is:
>
> "That is, you should define the 'typedef struct SomethingIf SomethingIf'
> so that you can pass around 'SomethingIf *si' arguments, but not define
> a 'struct SomethingIf { ... }'. The only things you can validly do with
> a 'SomethingIf *' are to pass it as an argument to a method on its corresponding
> SomethingIfClass, or to dynamically cast the interface pointer to a pointer
> to the concrete object which is implementing the interface."
>
> ?
>
> > + * You can dynamically cast an object to one of its #Interface types and vice
> > + * versa.
>
> ...though that last part is then kind of awkwardly similar to this sentence.
> There's probably better wording possible than what I suggest above.
>
What about ?
* Interfaces allow a limited form of multiple inheritance. Instances are
* similar to normal types except for the fact that are only defined by
- * their classes and never carry any state. You can dynamically cast an object
- * to one of its #Interface types and vice versa.
+ * their classes and never carry any state. As a consequence, a pointer to
+ * an interface instance should always be of incomplete type in order to be
+ * sure it cannot be dereferenced. That is, you should define the
+ * 'typedef struct SomethingIf SomethingIf' so that you can pass around
+ * 'SomethingIf *si' arguments, but not define a 'struct SomethingIf { ... }'.
+ * The only things you can validly do with a 'SomethingIf *' are to pass it as
+ * an argument to a method on its corresponding SomethingIfClass, or to
+ * dynamically cast it to an object that implements the interface.
> thanks
> -- PMM
next prev parent reply other threads:[~2019-12-09 21:05 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-12-09 13:28 [for-5.0 PATCH] ppc: Make PPCVirtualHypervisor an incomplete type Greg Kurz
2019-12-09 14:02 ` Philippe Mathieu-Daudé
2019-12-09 16:27 ` Greg Kurz
2019-12-09 16:42 ` Peter Maydell
2019-12-09 21:04 ` Greg Kurz [this message]
2019-12-10 10:52 ` Markus Armbruster
2019-12-09 17:57 ` no-reply
2019-12-09 18:08 ` no-reply
2019-12-10 0:36 ` David Gibson
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=20191209220404.4b1539c8@bahia.w3ibm.bluemix.net \
--to=groug@kaod.org \
--cc=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=marcandre.lureau@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@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).