public inbox for linux-ia64@vger.kernel.org
 help / color / mirror / Atom feed
* [Linux-ia64] C union on ia-64
@ 2001-10-23 19:03 Gururaj Ananthateerta
  2001-10-24 11:35 ` Andreas Schwab
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Gururaj Ananthateerta @ 2001-10-23 19:03 UTC (permalink / raw)
  To: linux-ia64

Anyone experienced (memory corruption ) problems with unions on ia-64.
I have found couple of documents which discourages the use of unions on
ia-64 in particular.


"KOCHI, Takayoshi" wrote:

> Hi,
>
> > I would recommend starting with the book `Linux Device Drivers'
> > by Rubini.  It's kind of out of date (written for the 2.0 kernel)
> > but it gives you a good idea about the basics.
>
> There is a refreshed version (for 2.4 kernel) published this year.
>
> Regards,
> --
> KOCHI Takayoshi <t-kouchi@cq.jp.nec.com/t-kouchi@mvf.biglobe.ne.jp>
>
> _______________________________________________
> Linux-IA64 mailing list
> Linux-IA64@linuxia64.org
> http://lists.linuxia64.org/lists/listinfo/linux-ia64



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
@ 2001-10-24 11:35 ` Andreas Schwab
  2001-10-24 18:52 ` Gururaj Ananthateerta
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Andreas Schwab @ 2001-10-24 11:35 UTC (permalink / raw)
  To: linux-ia64

Gururaj Ananthateerta <gururaj@cup.hp.com> writes:

|> Anyone experienced (memory corruption ) problems with unions on ia-64.
|> I have found couple of documents which discourages the use of unions on
|> ia-64 in particular.

Please expand.  Unions are an integral part of C, without them you could
not call it C.

Andreas.

-- 
Andreas Schwab                                  "And now for something
Andreas.Schwab@suse.de				completely different."
SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
  2001-10-24 11:35 ` Andreas Schwab
@ 2001-10-24 18:52 ` Gururaj Ananthateerta
  2001-10-24 19:46 ` Andreas Schwab
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Gururaj Ananthateerta @ 2001-10-24 18:52 UTC (permalink / raw)
  To: linux-ia64

http://developer.intel.com/design/itanium/linuxDDPorting/sld025.htm

The above link provides an example.



Andreas Schwab wrote:

> Gururaj Ananthateerta <gururaj@cup.hp.com> writes:
>
> |> Anyone experienced (memory corruption ) problems with unions on ia-64.
> |> I have found couple of documents which discourages the use of unions on
> |> ia-64 in particular.
>
> Please expand.  Unions are an integral part of C, without them you could
> not call it C.
>
> Andreas.
>
> --
> Andreas Schwab                                  "And now for something
> Andreas.Schwab@suse.de                          completely different."
> SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
> Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
  2001-10-24 11:35 ` Andreas Schwab
  2001-10-24 18:52 ` Gururaj Ananthateerta
@ 2001-10-24 19:46 ` Andreas Schwab
  2001-10-24 19:46 ` Boehm, Hans
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Andreas Schwab @ 2001-10-24 19:46 UTC (permalink / raw)
  To: linux-ia64

Gururaj Ananthateerta <gururaj@cup.hp.com> writes:

|> http://developer.intel.com/design/itanium/linuxDDPorting/sld025.htm
|> 
|> The above link provides an example.

How are the types defined?  Without that information nothing can be said
about it.  The C standard explictly says that it is undefined behaviour if
you access a member of a union that was stored under a different member.

Andreas.

-- 
Andreas Schwab                                  "And now for something
Andreas.Schwab@suse.de				completely different."
SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5


^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
                   ` (2 preceding siblings ...)
  2001-10-24 19:46 ` Andreas Schwab
@ 2001-10-24 19:46 ` Boehm, Hans
  2001-10-24 19:50 ` n0ano
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Boehm, Hans @ 2001-10-24 19:46 UTC (permalink / raw)
  To: linux-ia64

That slide looks to me like it escaped from a presentation on 64-bit Windows
programming, where "unsigned long" is 4 bytes long, unlike "void *", and
where it's customary to call both types by different names.  

It is never legal C to union the two structures store into one structure
field, and retrieve the value through a field in the other structure.  In
many cases it may work, but it is officially undefined, and hence allowed to
confuse the compiler's alias analysis.  Thus the compiler is entitled to
generate incorrect code.  This applies to all platforms.

In the 64-bit Windows case, this sort of thing is guaranteed to fail, since
the fields also don't line up correctly.  On the Linux side, there is no
difference between the 64-bit and 32-bit cases.  

Unless there's a better explanation, could someone at Intel correct this?

Hans

> -----Original Message-----
> From: Gururaj Ananthateerta [mailto:gururaj@cup.hp.com]
> Sent: Wednesday, October 24, 2001 11:53 AM
> To: Andreas Schwab
> Cc: KOCHI, Takayoshi; linux-64
> Subject: Re: [Linux-ia64] C union on ia-64
> 
> 
> http://developer.intel.com/design/itanium/linuxDDPorting/sld025.htm
> 
> The above link provides an example.
> 
> 
> 
> Andreas Schwab wrote:
> 
> > Gururaj Ananthateerta <gururaj@cup.hp.com> writes:
> >
> > |> Anyone experienced (memory corruption ) problems with 
> unions on ia-64.
> > |> I have found couple of documents which discourages the 
> use of unions on
> > |> ia-64 in particular.
> >
> > Please expand.  Unions are an integral part of C, without 
> them you could
> > not call it C.
> >
> > Andreas.
> >
> > --
> > Andreas Schwab                                  "And now 
> for something
> > Andreas.Schwab@suse.de                          completely 
> different."
> > SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
> > Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
> 
> 
> _______________________________________________
> Linux-IA64 mailing list
> Linux-IA64@linuxia64.org
> http://lists.linuxia64.org/lists/listinfo/linux-ia64
> 


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
                   ` (3 preceding siblings ...)
  2001-10-24 19:46 ` Boehm, Hans
@ 2001-10-24 19:50 ` n0ano
  2001-10-24 19:50 ` KOCHI, Takayoshi
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: n0ano @ 2001-10-24 19:50 UTC (permalink / raw)
  To: linux-ia64

Gururaj-

Sigh.  Intel's example has nothing to do with unions not working,
unions work fine.  All the example was pointing out is that any
code that assumes sizes of data items is risky.  This slide is
a little unclear - using a union will not corrupt data, it might
cause your code to use unexpected data.

A better example would be:

	struct s1 {
		int i1;
		int i2;
		int i3;
	};

	struct s2 {
		long l1;
		long l2;
		long l3;
	};

	union un {
		struct s1;
		struct s2;
	} u;

	u.s1.i2 = 1;
	u.s2.l1 = 0;
	printf("i2 = %d\n", u.s1.i2);

will print out `i2 = 1' on an IA32 machine and `i2 = 0' on an
IA64 machine.  Both answers are correct, it's just that on an IA32
machine int's and long's are the same size and on an IA64 machine
they are different.

Anytime you use a union to look at the same memory contents with
different layouts you are doing something that is risky and should
be avoided if at all possible.  This is true for any architecture.

PS: Interestingly enought, I believe Intel's slide is actually
wrong.  If `ULONG' is an `unsigned long' and `PVOID' is a `void *'
then all of the data items have the same size and the two structures
can be used interchangably on either IA32 or IA64.  It's a
dangerous technique but it would work in this case.

On Wed, Oct 24, 2001 at 11:52:40AM -0700, Gururaj Ananthateerta wrote:
> http://developer.intel.com/design/itanium/linuxDDPorting/sld025.htm
> 
> The above link provides an example.
> 
> 
> 
> Andreas Schwab wrote:
> 
> > Gururaj Ananthateerta <gururaj@cup.hp.com> writes:
> >
> > |> Anyone experienced (memory corruption ) problems with unions on ia-64.
> > |> I have found couple of documents which discourages the use of unions on
> > |> ia-64 in particular.
> >
> > Please expand.  Unions are an integral part of C, without them you could
> > not call it C.
> >
> > Andreas.
> >
> > --
> > Andreas Schwab                                  "And now for something
> > Andreas.Schwab@suse.de                          completely different."
> > SuSE Labs, SuSE GmbH, Schanzäckerstr. 10, D-90443 Nürnberg
> > Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
> 
> 
> _______________________________________________
> Linux-IA64 mailing list
> Linux-IA64@linuxia64.org
> http://lists.linuxia64.org/lists/listinfo/linux-ia64

-- 
Don Dugger
"Censeo Toto nos in Kansa esse decisse." - D. Gale
n0ano@indstorage.com
Ph: 303/652-0870x117


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
                   ` (4 preceding siblings ...)
  2001-10-24 19:50 ` n0ano
@ 2001-10-24 19:50 ` KOCHI, Takayoshi
  2001-10-25  1:12 ` Tang, Yu
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: KOCHI, Takayoshi @ 2001-10-24 19:50 UTC (permalink / raw)
  To: linux-ia64

> PS: Interestingly enought, I believe Intel's slide is actually
> wrong.  If `ULONG' is an `unsigned long' and `PVOID' is a `void *'
> then all of the data items have the same size and the two structures
> can be used interchangably on either IA32 or IA64.  It's a
> dangerous technique but it would work in this case.

I believe win64 uses P64 for its programming model.
(while we use LP64 instead)
So their ULONG is still 32bit in IA64.

Regards,
-- 
KOCHI Takayoshi <t-kouchi@cq.jp.nec.com/t-kouchi@mvf.biglobe.ne.jp>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* RE: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
                   ` (5 preceding siblings ...)
  2001-10-24 19:50 ` KOCHI, Takayoshi
@ 2001-10-25  1:12 ` Tang, Yu
  2001-10-25 14:36 ` n0ano
  2001-10-31 23:16 ` Jes Sorensen
  8 siblings, 0 replies; 10+ messages in thread
From: Tang, Yu @ 2001-10-25  1:12 UTC (permalink / raw)
  To: linux-ia64

/* size.c */
#include "stido.h"
int main(int argc, char *argv[])
{
	printf("%d,%d\n",sizeof(unsigned long),sizeof(void*));
	return 0;
}

under linux:
$gcc size.c -o size
$./size
8,8

under 64-bit WinXP:
c:\temp\size
4,8

-----Original Message-----
From: KOCHI, Takayoshi [mailto:t-kouchi@mvf.biglobe.ne.jp]
Sent: 2001å¹´10æoe^25æ-¥ 3:50
To: linux-64
Subject: Re: [Linux-ia64] C union on ia-64


> PS: Interestingly enought, I believe Intel's slide is actually
> wrong.  If `ULONG' is an `unsigned long' and `PVOID' is a `void *'
> then all of the data items have the same size and the two structures
> can be used interchangably on either IA32 or IA64.  It's a
> dangerous technique but it would work in this case.

I believe win64 uses P64 for its programming model.
(while we use LP64 instead)
So their ULONG is still 32bit in IA64.

Regards,
-- 
KOCHI Takayoshi <t-kouchi@cq.jp.nec.com/t-kouchi@mvf.biglobe.ne.jp>


_______________________________________________
Linux-IA64 mailing list
Linux-IA64@linuxia64.org
http://lists.linuxia64.org/lists/listinfo/linux-ia64


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
                   ` (6 preceding siblings ...)
  2001-10-25  1:12 ` Tang, Yu
@ 2001-10-25 14:36 ` n0ano
  2001-10-31 23:16 ` Jes Sorensen
  8 siblings, 0 replies; 10+ messages in thread
From: n0ano @ 2001-10-25 14:36 UTC (permalink / raw)
  To: linux-ia64

Yeah, I know that NT uses a P64 model but if you'll look at
the caption of the slide in question it is for `Linux Device
Drivers'.  Anyone who typedef's `ULONG' to anything other than 
a 64-bit long on IA64 Linux deserves the confusion that that
will create.

Actually, I believe I was at John's presentation of this slide
set and I'm pretty sure he clarified all of this during his
talk.  This just points out the danger of looking at just a
set of slides.  Without the author to explain things it can
be easy to misinterpret things.

On Thu, Oct 25, 2001 at 09:12:51AM +0800, Tang, Yu wrote:
> /* size.c */
> #include "stido.h"
> int main(int argc, char *argv[])
> {
> 	printf("%d,%d\n",sizeof(unsigned long),sizeof(void*));
> 	return 0;
> }
> 
> under linux:
> $gcc size.c -o size
> $./size
> 8,8
> 
> under 64-bit WinXP:
> c:\temp\size
> 4,8
> 
> -----Original Message-----
> From: KOCHI, Takayoshi [mailto:t-kouchi@mvf.biglobe.ne.jp]
> Sent: 2001å¹´10æoe^25æ-¥ 3:50
> To: linux-64
> Subject: Re: [Linux-ia64] C union on ia-64
> 
> 
> > PS: Interestingly enought, I believe Intel's slide is actually
> > wrong.  If `ULONG' is an `unsigned long' and `PVOID' is a `void *'
> > then all of the data items have the same size and the two structures
> > can be used interchangably on either IA32 or IA64.  It's a
> > dangerous technique but it would work in this case.
> 
> I believe win64 uses P64 for its programming model.
> (while we use LP64 instead)
> So their ULONG is still 32bit in IA64.
> 
> Regards,
> -- 
> KOCHI Takayoshi <t-kouchi@cq.jp.nec.com/t-kouchi@mvf.biglobe.ne.jp>
> 
> 
> _______________________________________________
> Linux-IA64 mailing list
> Linux-IA64@linuxia64.org
> http://lists.linuxia64.org/lists/listinfo/linux-ia64
> 
> _______________________________________________
> Linux-IA64 mailing list
> Linux-IA64@linuxia64.org
> http://lists.linuxia64.org/lists/listinfo/linux-ia64

-- 
Don Dugger
"Censeo Toto nos in Kansa esse decisse." - D. Gale
n0ano@indstorage.com
Ph: 303/652-0870x117


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [Linux-ia64] C union on ia-64
  2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
                   ` (7 preceding siblings ...)
  2001-10-25 14:36 ` n0ano
@ 2001-10-31 23:16 ` Jes Sorensen
  8 siblings, 0 replies; 10+ messages in thread
From: Jes Sorensen @ 2001-10-31 23:16 UTC (permalink / raw)
  To: linux-ia64

>>>>> "n0ano" = n0ano  <n0ano@indstorage.com> writes:

n0ano> Yeah, I know that NT uses a P64 model but if you'll look at the
n0ano> caption of the slide in question it is for `Linux Device
n0ano> Drivers'.  Anyone who typedef's `ULONG' to anything other than
n0ano> a 64-bit long on IA64 Linux deserves the confusion that that
n0ano> will create.

Actually, for Linux *nobody* should typedef ULONG or PVOID or any of
thos broken windows types to anything. Use the types as they are
named: `unsigned long', `void *' and use fixed sized types when fixed
sizes are needed: `u32', `u64', `s32' etc etc.

Promoting bogus typedefs is only going to make the code ugly,
unmaintainable and hard to debug.

Jes


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2001-10-31 23:16 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-10-23 19:03 [Linux-ia64] C union on ia-64 Gururaj Ananthateerta
2001-10-24 11:35 ` Andreas Schwab
2001-10-24 18:52 ` Gururaj Ananthateerta
2001-10-24 19:46 ` Andreas Schwab
2001-10-24 19:46 ` Boehm, Hans
2001-10-24 19:50 ` n0ano
2001-10-24 19:50 ` KOCHI, Takayoshi
2001-10-25  1:12 ` Tang, Yu
2001-10-25 14:36 ` n0ano
2001-10-31 23:16 ` Jes Sorensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox