The MASM Forum

Specialised Projects => Compiler Based Assembler => Assembler With Microsoft Visual C => Topic started by: TouEnMasm on September 06, 2012, 11:04:06 PM

Title: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 06, 2012, 11:04:06 PM

I have tested the STORAGE_PROPERTY_QUERY structure here his:
Quote
STORAGE_PROPERTY_QUERY   STRUCT
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE 1 dup (?)
STORAGE_PROPERTY_QUERY      ENDS
For each individual part of the structure the c++ agree with the size
stock = sizeof(STORAGE_PROPERTY_QUERY);      12
  stock = sizeof(mesure.PropertyId);                     4
  stock = sizeof(mesure.QueryType);                     4
  stock = sizeof(mesure.AdditionalParameters);     1

4+4+1=12    ???
I suppose that an alignment or a round is made for the byte.
How to do the same thing with masm ??????









Title: Re: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 07, 2012, 12:50:24 AM

to know where are added the three bytes for alignment ,I try to get the adress of each member of the structure.

Quote
DWORD TestSize(STORAGE_PROPERTY_QUERY * essai)
{
   STORAGE_PROPERTY_QUERY * machin;
    STORAGE_PROPERTY_QUERY * chose;

   chose = essai;                ok here,adress of the structure
   machin = * essai->PropertyId; ????? failed
   return 0;
}
Perhaps somebody here know how to get the adress of essai.PropertyId ?


Title: Re: are you sure that 4+4+1=9 ?
Post by: qWord on September 07, 2012, 12:58:44 AM
Quote from: ToutEnMasm on September 07, 2012, 12:50:24 AMto know where are added the three bytes for alignment ,I try to get the adress of each member of the structure.
The bytes are appended. To get a reference:  &essai->PropertyId.
Title: Re: are you sure that 4+4+1=9 ?
Post by: dedndave on September 07, 2012, 02:17:27 AM
STORAGE_PROPERTY_QUERY   STRUCT DWORD
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE ?
STORAGE_PROPERTY_QUERY      ENDS


doesn't that work ???
otherwise - you might try the OPTION directive to set the structure alignment

if neither of those works....
STORAGE_PROPERTY_QUERY   STRUCT
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE ?
  BYTE 3 dup(?)
STORAGE_PROPERTY_QUERY      ENDS


:P
Title: Re: are you sure that 4+4+1=9 ?
Post by: MichaelW on September 07, 2012, 02:18:27 AM
The attachment contains a C test and an ASM test. I think for structures that end with a 1-element array, the general expectation is that you allocate memory for the structure and use the structure definition as a template.
Title: Re: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 07, 2012, 02:46:19 AM

Ok , the & is an answer.
last question is , how i declare machin to made this work?
i have tried int dword ...
Quote
machin = (&essai - &essai->AdditionalParameters );
Title: Re: are you sure that 4+4+1=9 ?
Post by: MichaelW on September 07, 2012, 03:23:43 AM
This should work:

int machin;
. . .
machin = ((int)&essai->PropertyId - (int)&essai->AdditionalParameters );
printf("%d\n\n", machin);


But if you want the offset of the AdditionalParameters member from the start of the structure you will need to reverse the subtraction, so the result will be 8 instead of -8.
Title: Re: are you sure that 4+4+1=9 ?
Post by: qWord on September 07, 2012, 03:29:37 AM
The rules for aligment in structures can read up here: Structure Alignment (http://msdn.microsoft.com/en-us/library/aa296569(v=vs.60).aspx)
Title: Re: are you sure that 4+4+1=9 ?
Post by: japheth on September 07, 2012, 03:36:49 AM

to get the offset of a member within a struct, the C offsetof() macro is probably the best choice:

mbrofs = offsetof( struct xxx, member );
Title: Re: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 07, 2012, 04:40:32 AM
Thanks at all for answers.
Here a resume of my test:
Quote
DWORD TestSize(STORAGE_PROPERTY_QUERY * essai)
{
    int machin;
    STORAGE_PROPERTY_QUERY * chose;
   chose = essai;
   machin = ((int)&essai->PropertyId - (int)&essai->AdditionalParameters );
   //machin = 8 or -8
   machin = offsetof( STORAGE_PROPERTY_QUERY,AdditionalParameters );
   // machin = 8 need  #include <stddef.h>
      return 0;
}

And for masm the good method to declare an alignment for a structure
Quote
STORAGE_PROPERTY_QUERY   STRUCT DWORD
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE 1 dup(?) ; dup (?)
STORAGE_PROPERTY_QUERY      ENDS   
This give the good size and the good alignment
I have learn much today.
Title: Re: are you sure that 4+4+1=9 ?
Post by: jj2007 on September 07, 2012, 08:38:01 AM
AdditionalParameters BYTE 1 dup(?) ; dup (?)
and
AdditionalParameters BYTE ?

are identical.

AdditionalParameters BYTE 4 dup(?)

achieves align DWORD, too.
Title: Re: are you sure that 4+4+1=9 ?
Post by: dedndave on September 07, 2012, 11:45:14 AM
hiya Jochen
this one yields the correct result for SIZEOF AdditionalParameters
STORAGE_PROPERTY_QUERY   STRUCT
   PropertyId DWORD ?
   QueryType DWORD ?
   AdditionalParameters BYTE ?
  BYTE 3 dup(?)
STORAGE_PROPERTY_QUERY      ENDS
Title: Re: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 07, 2012, 03:22:46 PM

The interest of all this is to give at masm the correct alignment for each structure as do the c++.
here is a sample of translate,winhttp sample:
Quote
// defaut alignment for c++ is 4 ,DWORD for masm
#if defined(_WIN64)
#include <pshpack8.h>
#else
#include <pshpack4.h> //return to the defaut alignment
#endif
................ here structures ----------------------
#include <poppack.h>

can be done as this with masm
Quote
IF DEFINED(_WIN64)
;Include pshpack8.SDK
DEFALIGNMASM TEXTEQU <QWORD>
ELSE
;Include pshpack4.SDK
DEFALIGNMASM TEXTEQU <DWORD>
ENDIF
................ here structures ----------------------
     ANYSTRUCTURE STRUCT DEFALIGNMASM
        ..........
     ANYSTRUCTURE ENDS
;include <poppack.h>
DEFALIGNMASM TEXTEQU <DWORD>   ;return to defaut alignment
I have verify that this soluce work.
adding bytes to the structure isn't a good way
Title: Re: are you sure that 4+4+1=9 ?
Post by: japheth on September 07, 2012, 05:07:34 PM
> I have verify that this soluce work.

It works most of the time, but AFAIR the Win32 alignment is also 8 (=QWORD), not DWORD.

It just doesn't matter usually because in Win32 there are very few QWORD members found in the structures.

But for an example where alignment DWORD doesn't work in Win32, see structure PARTITION_INFORMATION in winioctl.h.

So the simplest solution may just be to add -Zp8 as cmdline parameter when you want all Windows structures to be correct.
Title: Re: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 07, 2012, 06:57:24 PM

The Zp4 or Zp8 option are only a partial solution.
This don't avoid the "Invalid parameter" send by the function receiving a bad size of the structure.
With such an error,there is only to search , can be a long time.
Title: Re: are you sure that 4+4+1=9 ?
Post by: jj2007 on September 07, 2012, 08:08:29 PM
There are a number of options, see snippet below for testing. The question is which size the API expects if it throws an error.

include \masm32\include\masm32rt.inc

STORAGE_PROPERTY_QUERY STRUCT ;DWORD
  PropertyId DWORD ?
  QueryType DWORD ?
  AdditionalParameters BYTE ?
;  manual_padding db (DWORD-1) dup(?)
STORAGE_PROPERTY_QUERY ENDS

.code
spq STORAGE_PROPERTY_QUERY <>
dummy dd ?

start: mov eax, offset dummy
sub eax, offset spq
print str$(eax), " bytes in memory", 13, 10
print str$(sizeof spq), " size of spq", 13, 10
print str$(sizeof STORAGE_PROPERTY_QUERY), " size of STORAGE_PROPERTY_QUERY", 13, 10
print str$(STORAGE_PROPERTY_QUERY), " STORAGE_PROPERTY_QUERY", 13, 10
inkey " "
exit

end start
Title: Re: are you sure that 4+4+1=9 ?
Post by: MichaelW on September 07, 2012, 08:23:36 PM
There won't always be an error reported. Sometimes the misalignment will just cause a hard to diagnose problem. I think the only reasonable and reliable way around this problem is to check each structure definition against a Microsoft compiler and header file.

Title: Re: are you sure that 4+4+1=9 ?
Post by: TouEnMasm on September 08, 2012, 10:18:04 PM
I have modified the "ready to use sdk" (windows.inc subforum).
Now he give the same alignment to the structures than the c++.
If it is not enough,PARTITION_INFORMATION  seem to be a special case,the defaut alignment of DWORD can be change to QWORD in the translate.inc file.

Quote
   ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   DefaultAlignement equ <DWORD>
   DEFALIGNMASM TEXTEQU  DefaultAlignement
   ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
change to
Quote
   ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   DefaultAlignement equ <QWORD>
   DEFALIGNMASM TEXTEQU  DefaultAlignement
   ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
and all the structures with no alignment specify are qword align
Specified alignment can be BYTE,WORD,DWORD or QWORD.