Skip to contentSkip to conversation

C union on ia-64

Linux-ia64 Gururaj Ananthateerta 7 participants 10 messages Oct 23, 2001 – Oct 31, 2001

lore
Share & tools
lore
#0 lore raw

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.

Quoted 16 lines · Takayoshi

Hi,

Quoted 3 lines

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 <[email protected]/[email protected]>

_______________________________________________
Linux-IA64 mailing list
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64

#1 lore raw

Gururaj Ananthateerta <[email protected]> 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.

Signature · 4 lines
Andreas Schwab                                  "And now for something
[email protected]				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
#2 lore raw
In reply to Gururaj Ananthateerta

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

The above link provides an example.

Quoted 16 lines · Andreas Schwab

Gururaj Ananthateerta <[email protected]> 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
[email protected] 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

#3 lore raw
In reply to Gururaj Ananthateerta

Gururaj Ananthateerta <[email protected]> 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.

Signature · 4 lines
Andreas Schwab                                  "And now for something
[email protected]				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
#4 lore raw
In reply to Gururaj Ananthateerta

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

Quoted 44 lines

-----Original Message-----
From: Gururaj Ananthateerta [mailto:[email protected]]
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.

Quoted 3 lines · Andreas Schwab

Gururaj Ananthateerta <[email protected]> writes:

|> Anyone experienced (memory corruption ) problems with

unions on ia-64.

Quoted 1 line

|> I have found couple of documents which discourages the

use of unions on

Quoted 3 lines

|> ia-64 in particular.

Please expand. Unions are an integral part of C, without

them you could

Quoted 6 lines

not call it C.

Andreas.

--
Andreas Schwab "And now

for something

Quoted 1 line

[email protected] completely

different."

Quoted 2 lines

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
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64

#5 lore raw
In reply to Gururaj Ananthateerta

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.

Quoted 30 lines · Gururaj Ananthateerta

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

The above link provides an example.

Quoted 9 lines · Andreas Schwab

Gururaj Ananthateerta <[email protected]> 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.

Quoted 7 lines

Andreas.

--
Andreas Schwab "And now for something
[email protected] 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
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64

Signature · 4 lines
Don Dugger
"Censeo Toto nos in Kansa esse decisse." - D. Gale
[email protected]
Ph: 303/652-0870x117
#6 lore raw
In reply to Gururaj Ananthateerta
Quoted 5 lines

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,

Signature · 1 line
KOCHI Takayoshi <[email protected]/[email protected]>
#7 lore raw
In reply to Gururaj Ananthateerta

/* 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:[email protected]]
Sent: 2001å¹´10æoe^25æ-¥ 3:50
To: linux-64
Subject: Re: [Linux-ia64] C union on ia-64

Quoted 5 lines

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,

Signature · 7 lines
KOCHI Takayoshi <[email protected]/[email protected]>


_______________________________________________
Linux-IA64 mailing list
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64
#8 lore raw
In reply to Gururaj Ananthateerta

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.

Quoted 48 lines · Yu

/* 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:[email protected]]
Sent: 2001å¹´10æoe^25æ-¥ 3:50
To: linux-64
Subject: Re: [Linux-ia64] C union on ia-64

Quoted 5 lines

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,

Signature · 12 lines
KOCHI Takayoshi <[email protected]/[email protected]>


_______________________________________________
Linux-IA64 mailing list
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64

_______________________________________________
Linux-IA64 mailing list
[email protected]
http://lists.linuxia64.org/lists/listinfo/linux-ia64
Signature · 4 lines
Don Dugger
"Censeo Toto nos in Kansa esse decisse." - D. Gale
[email protected]
Ph: 303/652-0870x117
#9 lore raw
In reply to Gururaj Ananthateerta
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"n0ano" = n0ano <[email protected]> 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