pb4.30[b4] RtlInitUnicodeString error in x64 , ok in x32

Just starting out? Need help? Post your questions and find answers here.
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

pb4.30[b4] RtlInitUnicodeString error in x64 , ok in x32

Post by bingo »

Code: Select all

Structure UNICODE_STRING 
usLength.w 
usMaximumLength.w 
usBuffer.i
EndStructure

pu.UNICODE_STRING

Procedure Ansi2Uni(ansi.s) 
SHStrDup_(@ansi,@myuni.i)
ProcedureReturn myuni 
EndProcedure

testunistring.i = Ansi2Uni("test")

Debug testunistring ;ok

RtlInitUnicodeString_(@pu,testunistring)

Debug pu\usLength ; = 8
Debug pu\usMaximumLength ; = 10

Debug testunistring ; but 0 in x64 !?

Debug pu\usBuffer

Debug PeekS(pu\usBuffer) ; = t , but error in x64
pointer of 'testunistring' is NULL after RtlInitUnicodeString (x64) :roll:
it works in x32 ...
["1:0>1"]
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Just a a structure alignment problem. Use this one and it works:

Code: Select all

Structure UNICODE_STRING
usLength.w
usMaximumLength.w
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  _alignment.l
CompilerEndIf
usBuffer.i
EndStructure
quidquid Latine dictum sit altum videtur
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post by bingo »

thanks ! but where is this documented ??? :shock:
["1:0>1"]
Fred
Administrator
Administrator
Posts: 18253
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

On x64, every C structures fields of 8 bytes (pointers, quad) are padded on a 8 bytes boundary.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Maybe I'm not understanding something. Wouldn't the .i in the structure compile to 8 bytes instead of 4 automatically on an x64 operating system?
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Mistrel wrote:Maybe I'm not understanding something. Wouldn't the .i in the structure compile to 8 bytes instead of 4 automatically on an x64 operating system?
@Mistrel:

on x64 (i),(q),(*) need to align on 8 byte boundary

2(w) + 2(w) + 8(i) = (i) is not aligned on 8 byte boundary
2(w) + 2(w) +4(l) + 8(i) = (i) is aligned on 8 byte boundary
Last edited by Demivec on Mon Dec 01, 2008 9:27 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18253
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You're right. And as the 2 fields before it are 2 words (2x2 = 4), the .i will be at the offset '4', which is not valid on x64. It needs to be on a 8 bytes boundary offset (so you add a 4 bytes padding).

If you have a structure like that:

Code: Select all

Structure x
  a.b
  b.i ; At offset 1, not good on x64
EndStructure
you will need to pad it like that:

Code: Select all

Structure x
  a.b
  pad.b[7]
  b.i ; At offset 8, ok on x64
EndStructure
Note, that it is needed only for 8 bytes field size (for 4 bytes fields size, you need to be on 4 byte boundary tough).
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

After reading about data structure alignment I understand now how it works. On x86 processors:
Wikipedia wrote:.. the x86 architecture originally did not require aligned memory access and still works without it
I also read that it's not uncommon for the compiler to add padding automatically. Will PureBasic support something like this?

http://en.wikipedia.org/wiki/Data_structure_alignment
Fred
Administrator
Administrator
Posts: 18253
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

I don't think we will introduce this, as automatic structure padding is somewhat tricky. You will have to know what to do anyway, so just explictely pad it and it you're done. Note: C compiler has the #pragma pack() directive which tells how to pad a structure, and when misused it leds to very hard to spot bugs.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

I've always tried to pair up words and bytes so that there are 4byte alignments out of habit really. I guess I'll have to consider pairing things up on 8bytes where possible then. (usually no harder than moving vars around in a structure and add a reserved here and there :) )
Post Reply