All of lore.kernel.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@eu.citrix.com>
To: xen-devel@lists.xensource.com
Subject: [PATCH] unnamed union structure members are dangerous to expose in public headers
Date: Tue, 25 Mar 2008 17:25:34 +0000	[thread overview]
Message-ID: <20080325172534.GD6845@implementation.uk.xensource.com> (raw)

Hello,

We have been struggling with Jean because of a bug in the headers:

#include <stdio.h>
#include <stdint.h>
#include <xen/foreign/x86_64.h>

int main(int argc, char *argv[]) {
	printf("%#lx\n", sizeof(vcpu_guest_context_x86_64_t));
	return 0;
}

will return 0x13e0 when compiled without gcc -std=c99, while the
expected value is 0x1430, resulting to seg faults and all kinds of
tricks.  Compiling with

#include "/usr/include/xen/foreign/x86_64.h"

shows why:

/usr/include/xen/foreign/x86_64.h:88: warning: declaration does not
declare anything

The special case of an unnamed union field is just a gcc extension, and
thus in C99 mode it considers Xen's __DECL_REG() just as an unnamed
union declaration, and does not reserve data in the structure.  The
patch below fixes this by telling gcc that it should use its gcc
extension.



Prefix unnamed union structure fields with __extension__ to make it work
properly even in e.g. C99 standard mode.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>

diff -r be3025f6af2e xen/include/public/arch-ia64.h
--- a/xen/include/public/arch-ia64.h	Mon Mar 24 18:24:03 2008 +0000
+++ b/xen/include/public/arch-ia64.h	Tue Mar 25 17:05:53 2008 +0000
@@ -165,11 +165,11 @@ struct mapped_regs {
     unsigned long  reserved1[29];
     unsigned long  vhpi;
     unsigned long  reserved2[95];
-    union {
+    __extension__ union {
         unsigned long  vgr[16];
         unsigned long bank1_regs[16]; // bank1 regs (r16-r31) when bank0 active
     };
-    union {
+    __extension__ union {
         unsigned long  vbgr[16];
         unsigned long bank0_regs[16]; // bank0 regs (r16-r31) when bank1 active
     };
@@ -180,7 +180,7 @@ struct mapped_regs {
     unsigned long  vpsr;
     unsigned long  vpr;
     unsigned long  reserved4[76];
-    union {
+    __extension__ union {
         unsigned long  vcr[128];
         struct {
             unsigned long dcr;  // CR0
@@ -214,7 +214,7 @@ struct mapped_regs {
             unsigned long rsv6[46];
         };
     };
-    union {
+    __extension__ union {
         unsigned long  reserved5[128];
         struct {
             unsigned long precover_ifs;
@@ -608,7 +608,7 @@ struct xen_ia64_opt_feature {
 struct xen_ia64_opt_feature {
 	unsigned long cmd;		/* Which feature */
 	unsigned char on;		/* Switch feature on/off */
-	union {
+	__extension__ union {
 		struct {
 				/* The page protection bit mask of the pte.
 			 	 * This will be or'ed with the pte. */
diff -r be3025f6af2e xen/include/public/arch-x86/xen-x86_64.h
--- a/xen/include/public/arch-x86/xen-x86_64.h	Mon Mar 24 18:24:03 2008 +0000
+++ b/xen/include/public/arch-x86/xen-x86_64.h	Tue Mar 25 17:05:53 2008 +0000
@@ -142,7 +142,7 @@ struct iret_context {
 
 #ifdef __GNUC__
 /* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */
-#define __DECL_REG(name) union { \
+#define __DECL_REG(name) __extension__ union { \
     uint64_t r ## name, e ## name; \
     uint32_t _e ## name; \
 }
diff -r be3025f6af2e tools/include/xen-foreign/mkheader.py
--- a/tools/include/xen-foreign/mkheader.py	Mon Mar 24 18:24:03 2008 +0000
+++ b/tools/include/xen-foreign/mkheader.py	Tue Mar 25 17:05:53 2008 +0000
@@ -38,7 +38,7 @@ inttypes["x86_64"] = {
 };
 header["x86_64"] = """
 #ifdef __GNUC__
-# define __DECL_REG(name) union { uint64_t r ## name, e ## name; }
+# define __DECL_REG(name) __extension__ union { uint64_t r ## name, e ## name; }
 # define __align8__ __attribute__((aligned (8)))
 #else
 # define __DECL_REG(name) uint64_t r ## name

             reply	other threads:[~2008-03-25 17:25 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-25 17:25 Samuel Thibault [this message]
2008-03-25 17:33 ` [PATCH] unnamed union structure members are dangerous to expose in public headers Keir Fraser
2008-03-25 17:42   ` Samuel Thibault

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=20080325172534.GD6845@implementation.uk.xensource.com \
    --to=samuel.thibault@eu.citrix.com \
    --cc=xen-devel@lists.xensource.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.