All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Jackson <Ian.Jackson@eu.citrix.com>
To: Julien Grall <julien.grall@citrix.com>
Cc: Keir Fraser <keir@xen.org>,
	ian.campbell@citrix.com,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>,
	Tim Deegan <tim@xen.org>, Jan Beulich <jbeulich@suse.com>,
	xen-devel@lists.xenproject.org
Subject: Re: [PATCH 4/4] xen/public: arm: rework the macro set_xen_guest_handle_raw
Date: Wed, 4 Nov 2015 16:22:13 +0000	[thread overview]
Message-ID: <22074.12469.923941.837807@mariner.uk.xensource.com> (raw)
In-Reply-To: <5638D1E4.4010208@citrix.com>

Julien Grall writes ("Re: [PATCH 4/4] xen/public: arm: rework the macro set_xen_guest_handle_raw"):
> On 03/11/15 14:18, Stefano Stabellini wrote:
> >> +#define set_xen_guest_handle_raw(hnd, val)                              \
> >> +    do {                                                                \
> >> +        /* Check if the handle is 64-bit (i.e 8-byte) */                \
> >> +        (void) sizeof(struct { int : -!!(sizeof (hnd) != 8); });        \
> >> +        /* Check if the type of val is compatible with the handle */    \
> >> +        (void) sizeof((val) != (hnd).p);                                \
> >> +        (hnd).q = (uint64_t)(uintptr_t)(val);                           \
> >>      } while ( 0 )
> >>  #define set_xen_guest_handle(hnd, val) set_xen_guest_handle_raw(hnd, val)

I hate to throw yet more variables into this, but I had a discussion
with some C experts in the pub about this problem.  Further thought
(including a red herring where someone suggested that memcpy would
"launder" the types) results in this proposal:


union xen_guest_handle_TYPE {
  TYPE *p;
  uint64_t align;
};

struct xen_guest_handle__pointer {
  uint8_t p0,p1,p2,p3;
};

/*
 * void set_xen_guest_handle_raw(xen_guest_handle_TYPE *hnd, TYPE *val);
 */
#define set_xen_guest_handle_raw(hnd, val)
  do {
     void *copy = (val);
     struct xen_guest_handle__pointer *src = &(hnd);
     struct xen_guest_handle__pointer *dst = &(xen_xgh_copy);
     dst->p0 = src->p0;
     dst->p1 = src->p1;
     dst->p2 = src->p2;
     dst->p3 = src->p3;
     dst[1]->p0 = 0;
     dst[1]->p1 = 0;
     dst[1]->p2 = 0;
     dst[1]->p3 = 0;
     sizeof((hnd).p == (val)); /* typecheck */
  }

#define get_xen_guest_handle(hnd) ((hnd).p)


The above is legal for the following reasons (C99 6.5):

Considering the accesses to src and dst.  These are legal according to
(7) point 5.  (Accesses via a character type are always legal.)

After the above macro has been used to set, what is the effective type
of hnd for a subsequent get_xen_guest_handle ?

If hnd is an object with a declared type (ie, an actual variable,
function parameter, or whatever, rather than from malloc), then the
effective type is that of hnd, so the effective type is the union, and
the effective type of hnd.p is TYPE*.

If hnd is not a declared object, then the effective type might be set
by one of the stores in (6): store through an non-character lvalue;
copy via memcpy or memmove; copy as `an array of character type'.

But we store only through character lvalues.  We do not store with
memcpy or memmove.  We do not copy as an array, but as a struct.

So none of the rules defining the effective type apply.  We are left
with the fallback: the effective type of the subsequent read is the
type used for access.

So a subsequent get's read of hnd.p is legal.


With luck the compiler's optimiser will eliminate the temporaries and
aggregate the byte-wide stores.


In the actual macro all of the struct and union fields and all of the
temporary variables should be prefixed with `xen_xgh_' or some such,
in case the calling code has (arguably foolishly) #defined src or dst
or p or something.

Ian.

  reply	other threads:[~2015-11-04 16:22 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-30 18:13 [PATCH 0/4] xen/public: arm: rework set_xen_guest_handle_raw Julien Grall
2015-10-30 18:13 ` [PATCH 1/4] xen/public: arm: Clarify the name of guest handle structures Julien Grall
2015-11-02 15:14   ` Stefano Stabellini
2015-10-30 18:13 ` [PATCH 2/4] xen/public: arm: Rework __guest_handle_param* Julien Grall
2015-11-02 15:19   ` Stefano Stabellini
2015-11-02 15:24     ` Julien Grall
2015-11-02 15:35       ` Ian Campbell
2015-11-02 15:39         ` Julien Grall
2015-11-02 15:49           ` Ian Campbell
2015-10-30 18:13 ` [PATCH 3/4] xen/public: Don't expose XEN_GUEST_HANDLE_PARAM outside of the hypervisor Julien Grall
2015-11-02 13:45   ` Jan Beulich
2015-11-02 13:52     ` Stefano Stabellini
2015-10-30 18:13 ` [PATCH 4/4] xen/public: arm: rework the macro set_xen_guest_handle_raw Julien Grall
2015-11-02 15:55   ` Stefano Stabellini
2015-11-02 16:15     ` Jan Beulich
2015-11-03 12:35     ` Ian Campbell
2015-11-03 14:01       ` Julien Grall
2015-11-03 14:34         ` Ian Campbell
2015-11-04 11:17           ` Julien Grall
2015-11-04 11:27             ` Ian Campbell
2015-11-04 11:40               ` Julien Grall
2015-11-04 14:29                 ` Ian Campbell
2015-11-04 14:42                   ` Julien Grall
2015-11-04 14:54                     ` Ian Campbell
2015-11-04 15:19                 ` Stefano Stabellini
2015-11-04 15:20                   ` Ian Jackson
2015-11-04 15:45                     ` Ian Jackson
2015-11-04 15:26                   ` Ian Campbell
2015-11-04 15:46                     ` Ian Jackson
2015-11-04 16:04                       ` Ian Campbell
2015-11-03 14:18   ` Stefano Stabellini
2015-11-03 15:25     ` Julien Grall
2015-11-04 16:22       ` Ian Jackson [this message]
2015-11-04 16:24         ` Ian Jackson
2015-11-04 16:39         ` Jan Beulich
2015-11-04 16:50           ` Ian Jackson
2015-11-04 16:59             ` Julien Grall
2015-11-04 17:05             ` Jan Beulich
2015-11-04 17:06               ` Ian Jackson
2015-11-05  8:37                 ` Jan Beulich
2015-11-06 12:10             ` Ian Campbell
2015-11-02 13:42 ` [PATCH 0/4] xen/public: arm: rework set_xen_guest_handle_raw Jan Beulich
2015-11-02 13:45   ` Julien Grall

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=22074.12469.923941.837807@mariner.uk.xensource.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=jbeulich@suse.com \
    --cc=julien.grall@citrix.com \
    --cc=keir@xen.org \
    --cc=stefano.stabellini@eu.citrix.com \
    --cc=tim@xen.org \
    --cc=xen-devel@lists.xenproject.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 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.