Hi Jan,

On 31.08.17 10:34, Jan Beulich wrote:
On 30.08.17 at 18:20, <volodymyr_babc...@epam.com> wrote:
My first intention was to declare union with all possible
representations, so it would be possible to access the same UUID as an
array of bytes or, for example, as Microsoft GUID. Like this:

typedef union {
      /* UUID represented as a 128-bit object */
      uint8_t obj[16];

      /* Representation according to RFC 4122 */
      struct {
          __be32  time_low;
          __be16  time_mid;
          __be16  time_hi_and_version;
          __u8    clock_seq_hi_and_reserved;
          __u8    clock_seq_low;
          __u8    node[6];
      } rfc4122;

      /* Microsoft/Intel style GUID representation */
      struct {
          __le32  Data1;
          __le16  Data2;
          __le16  Data3;
          __u8    Data4[8];
      } guid;

      /* SMCCC compatible format */
      struct {
          __le32 r0;
          __le32 r1;
          __le32 r2;
          __le32 r3;
      } smccc;
} xen_uuid_t;


But looks like we can't use something like __packed or
__attribute__((__packed__)) in the public header. This means that we
can't rely on right overlapping and users of this union should take care
to read and write only to one chosen substructure.

I don't see any use of that attribute in the structure definition
above, nor any need to add one - all fields are suitably aligned
anyway. You can't use __be* and __le* types in the public
headers, though - these will need to be uint*_t.
This is a public header. As I understand it can be used by different compilers (gcc, icc, msvc, llvm, etc...). C89 have no restrictions to padding or alignment of fields in structures. No one can guarantee that

sizeof(xen_uuid_t.rfc422) == sizeof(xen_uuid_t.guid) == sizeof(xen_uuid_t.smccc) == 16

On all platforms. Using any compiler. With any compiler options.

This is implementation defined ([1]). Standard says "This should present no problem unless binary data written by one implementation are read by another.". But in case of public headers, this structures can be written by one implementation and read by another.

BTW, I'm very interested how it can be guaranteed that structures
defined in xen.h will have the same size and alignment on both sides of
communication channel, taking into account, then we rely only on C89
standard.

I don't understand this comment.
See my reply above. There absolutely no guarantees, that MSVC compiler will not add extra padding somewhere. This will impair interoperability. I think, this is why (amongst other reasons) WinAPI structures has dwSize as the first field. And this is why _IO* macros in Linux kernel use sizeof() to create ioctl number.
But, as I can see, XEN code checks only magic or version.

[1] http://port70.net/~nsz/c/c89/c89-draft.html#A.6.3.8

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
https://lists.xen.org/xen-devel

Reply via email to