The MASM Forum

General => The Campus => Topic started by: jj2007 on February 17, 2014, 08:09:52 PM

Title: Weird problem with structure alignment
Post by: jj2007 on February 17, 2014, 08:09:52 PM
The problem is the 4 in bINPUT_RECORD STRUCT 4:

include \masm32\include\masm32rt.inc
aINPUT_RECORD STRUCT
  EventType             WORD ?
  two_byte_alignment    WORD ?
  UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
  ENDS
aINPUT_RECORD ENDS

bINPUT_RECORD STRUCT 4
  EventType             WORD ?
  UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
  ENDS
bINPUT_RECORD ENDS

.data
ira aINPUT_RECORD <>
irb bINPUT_RECORD <>

.code
start:
lea eax, ira.KeyEvent
sub eax, offset ira
print str$(eax), 9, "bytes", 13, 10
lea eax, irb.KeyEvent
sub eax, offset irb
print str$(eax), 9, "bytes", 13, 10
exit
end start


The good news is that JWasm is entirely happy with this, in line with the MASM Programmer's Guide.

The bad news is that Masm 6.14, 6.15, 8, 9 and 10 complain about internal errors.

Windows.inc avoids the problem by inserting a padding word, but that is just a workaround, not a proper solution. It seems it's a known bug, #4 in Japheth's list (http://www.japheth.de/JWasm/Manual.html#CHAPMASMBUGS) - how should it be treated?
Title: Re: Weird problem with structure alignment
Post by: hutch-- on February 17, 2014, 08:22:52 PM
JJ,

I don't see the problem of setting internal alignment with padding, its orthodox technique and works fine.


INPUT_RECORD STRUCT
  EventType             WORD ?
  two_byte_alignment    WORD ?
  UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
  ENDS
INPUT_RECORD ENDS
Title: Re: Weird problem with structure alignment
Post by: sinsi on February 17, 2014, 09:06:36 PM
Workaround?

e_type UNION
    KeyEvent                KEY_EVENT_RECORD            <>
    MouseEvent              MOUSE_EVENT_RECORD          <>
    WindowBufferSizeEvent   WINDOW_BUFFER_SIZE_RECORD   <>
    MenuEvent               MENU_EVENT_RECORD           <>
    FocusEvent              FOCUS_EVENT_RECORD          <>
e_type ENDS

bINPUT_RECORD STRUCT 4
  EventType         WORD ?
  etype             e_type <>
bINPUT_RECORD ENDS


lea eax, irb.etype.FocusEvent

4
4
Title: Re: Weird problem with structure alignment
Post by: jj2007 on February 17, 2014, 10:04:29 PM
Hutch, Sinsi,

Thanks. I had hoped there would be an "official and automatic" solution but it seems a manual intervention is needed, i.e. padding.
Title: Re: Weird problem with structure alignment
Post by: Gunther on February 17, 2014, 10:36:47 PM
Jochen,

Quote from: jj2007 on February 17, 2014, 10:04:29 PM
I had hoped there would be an "official and automatic" solution but it seems a manual intervention is needed, i.e. padding.

that doesn't mean that it would be an inferior solution.

Gunther
Title: Re: Weird problem with structure alignment
Post by: sinsi on February 17, 2014, 11:04:42 PM
Windbg output

(1644.4d4): Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=00000000 ebx=0048f480 ecx=01232054 edx=00000204 esi=00000000 edi=0048f48c
eip=011999e7 esp=0048f458 ebp=0048f470 iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00010246
ml!mergeStructInst+0x3da:
011999e7 8a00            mov     al,byte ptr [eax]          ds:002b:00000000=??


ML output

MASM : fatal error A1016: Internal error

  Version 11.00.50727.1

  ExceptionCode            = C0000005
  ExceptionFlags           = 00000000
  ExceptionAddress         = 011999E7 (01170000) "F:\asm\32\ml.exe"
  NumberParameters         = 00000002
  ExceptionInformation[ 0] = 00000000
  ExceptionInformation[ 1] = 00000000

CONTEXT:
  Eax    = 00000000  Esp    = 0048F458
  Ebx    = 0048F480  Ebp    = 0048F470
  Ecx    = 01232054  Esi    = 00000000
  Edx    = 00000204  Edi    = 0048F48C
  Eip    = 011999E7  EFlags = 00010246
  SegCs  = 00000023  SegDs  = 0000002B
  SegSs  = 0000002B  SegEs  = 0000002B
  SegFs  = 00000053  SegGs  = 0000002B
  Dr0    = 00000000  Dr3    = 00000000
  Dr1    = 00000000  Dr6    = 00000000
  Dr2    = 00000000  Dr7    = 00000000


Using "aINPUT_RECORD STRUCT 4" works, possibly since the union is already aligned.
In the same way, "bINPUT_RECORD STRUCT 2" works (but obviously gives a wrong answer, 2 not 4).