Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/BASM    [ Add a report in this area ]  
Report #:  129694   Status: Resolved
[Win64] Invalid assembly code when accessing records or static arrays
Project:  Delphi Build #:  21.0.17707.5020
Version:    21.0 Submitted By:   Marcin Wiazowski
Report Type:  Crash / Data loss / Total failure Date Reported:  1/11/2015 2:54:30 PM
Severity:    Critical / Show Stopper Last Updated: 12/1/2015 1:21:31 PM
Platform:    All versions Internal Tracking #:   57985
Resolution: Fixed (Resolution Comments) Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: None
Description
Accessing record fields (other than the first) or static array items (other than the first) generates assembly code, that accesses invalid memory locations.

Tested with XE7 Update 1.
Steps to Reproduce:
Compile and disassemble the following examples:

----------------
program Test1;

var
  TestRecord : packed record
    SomeAddress1 : Pointer;
    SomeAddress2 : Pointer;
  end;

asm
  {Just for reference}
  MOV RAX,offset TestRecord.SomeAddress2

  {All these instructions will be generated improperly}
  MOV byte ptr TestRecord.SomeAddress2,11h
  MOV word ptr TestRecord.SomeAddress2,11h
  MOV word ptr TestRecord.SomeAddress2,1122h
  MOV dword ptr TestRecord.SomeAddress2,11h
  MOV dword ptr TestRecord.SomeAddress2,1122h
  MOV dword ptr TestRecord.SomeAddress2,11223344h

  {All these instructions will be generated improperly}
  CMP byte ptr TestRecord.SomeAddress2,11h
  CMP word ptr TestRecord.SomeAddress2,11h
  CMP word ptr TestRecord.SomeAddress2,1122h
  CMP dword ptr TestRecord.SomeAddress2,11h
  CMP dword ptr TestRecord.SomeAddress2,1122h
  CMP dword ptr TestRecord.SomeAddress2,11223344h

  {All these instructions will be generated improperly}
  XOR byte ptr TestRecord.SomeAddress2,11h
  XOR word ptr TestRecord.SomeAddress2,11h
  XOR word ptr TestRecord.SomeAddress2,1122h
  XOR dword ptr TestRecord.SomeAddress2,11h
  XOR dword ptr TestRecord.SomeAddress2,1122h
  XOR dword ptr TestRecord.SomeAddress2,11223344h

  {All these instructions will be generated improperly}
  AND byte ptr TestRecord.SomeAddress2,11h
  AND word ptr TestRecord.SomeAddress2,11h
  AND word ptr TestRecord.SomeAddress2,1122h
  AND dword ptr TestRecord.SomeAddress2,11h
  AND dword ptr TestRecord.SomeAddress2,1122h
  AND dword ptr TestRecord.SomeAddress2,11223344h
end.
----------------
Disassembler output (note +1, +2 or +4 offsets):

mov     rax, offset SomeAddress2

mov     byte ptr cs:SomeAddress2+1, 11h
mov     word ptr cs:SomeAddress2+2, 11h
mov     word ptr cs:SomeAddress2+2, 1122h
mov     dword ptr cs:SomeAddress2+4, 11h
mov     dword ptr cs:SomeAddress2+4, 1122h
mov     dword ptr cs:SomeAddress2+4, 11223344h

cmp     byte ptr cs:SomeAddress2+1, 11h
cmp     word ptr cs:SomeAddress2+1, 11h
cmp     word ptr cs:SomeAddress2+2, 1122h
cmp     dword ptr cs:SomeAddress2+1, 11h
cmp     dword ptr cs:SomeAddress2+4, 1122h
cmp     dword ptr cs:SomeAddress2+4, 11223344h

xor     byte ptr cs:SomeAddress2+1, 11h
xor     word ptr cs:SomeAddress2+1, 11h
xor     word ptr cs:SomeAddress2+2, 1122h
xor     dword ptr cs:SomeAddress2+1, 11h
xor     dword ptr cs:SomeAddress2+4, 1122h
xor     dword ptr cs:SomeAddress2+4, 11223344h

and     byte ptr cs:SomeAddress2+1, 11h
and     word ptr cs:SomeAddress2+1, 11h
and     word ptr cs:SomeAddress2+2, 1122h
and     dword ptr cs:SomeAddress2+1, 11h
and     dword ptr cs:SomeAddress2+4, 1122h
and     dword ptr cs:SomeAddress2+4, 11223344h
----------------


----------------
program Test2;

var
  TestArray : packed array[0..1] of Pointer;

asm
  {Just for reference}
  MOV RAX,offset TestArray[1]

  {All these instructions will be generated improperly}
  MOV byte ptr TestArray[1],11h
  MOV word ptr TestArray[1],11h
  MOV word ptr TestArray[1],1122h
  MOV dword ptr TestArray[1],11h
  MOV dword ptr TestArray[1],1122h
  MOV dword ptr TestArray[1],11223344h

  {All these instructions will be generated improperly}
  CMP byte ptr TestArray[1],11h
  CMP word ptr TestArray[1],11h
  CMP word ptr TestArray[1],1122h
  CMP dword ptr TestArray[1],11h
  CMP dword ptr TestArray[1],1122h
  CMP dword ptr TestArray[1],11223344h

  {All these instructions will be generated improperly}
  XOR byte ptr TestArray[1],11h
  XOR word ptr TestArray[1],11h
  XOR word ptr TestArray[1],1122h
  XOR dword ptr TestArray[1],11h
  XOR dword ptr TestArray[1],1122h
  XOR dword ptr TestArray[1],11223344h

  {All these instructions will be generated improperly}
  AND byte ptr TestArray[1],11h
  AND word ptr TestArray[1],11h
  AND word ptr TestArray[1],1122h
  AND dword ptr TestArray[1],11h
  AND dword ptr TestArray[1],1122h
  AND dword ptr TestArray[1],11223344h
end.
----------------
Disassembler output (note +1, +2 or +4 offsets):

mov     rax, offset TestArray1

mov     byte ptr cs:TestArray1+1, 11h
mov     word ptr cs:TestArray1+2, 11h
mov     word ptr cs:TestArray1+2, 1122h
mov     dword ptr cs:TestArray1+4, 11h
mov     dword ptr cs:TestArray1+4, 1122h
mov     dword ptr cs:TestArray1+4, 11223344h

cmp     byte ptr cs:TestArray1+1, 11h
cmp     word ptr cs:TestArray1+1, 11h
cmp     word ptr cs:TestArray1+2, 1122h
cmp     dword ptr cs:TestArray1+1, 11h
cmp     dword ptr cs:TestArray1+4, 1122h
cmp     dword ptr cs:TestArray1+4, 11223344h

xor     byte ptr cs:TestArray1+1, 11h
xor     word ptr cs:TestArray1+1, 11h
xor     word ptr cs:TestArray1+2, 1122h
xor     dword ptr cs:TestArray1+1, 11h
xor     dword ptr cs:TestArray1+4, 1122h
xor     dword ptr cs:TestArray1+4, 11223344h

and     byte ptr cs:TestArray1+1, 11h
and     word ptr cs:TestArray1+1, 11h
and     word ptr cs:TestArray1+2, 1122h
and     dword ptr cs:TestArray1+1, 11h
and     dword ptr cs:TestArray1+4, 1122h
and     dword ptr cs:TestArray1+4, 11223344h
----------------
Workarounds
Instead of using:

   MOV byte ptr TestRecord.SomeAddress2,11h
   MOV word ptr TestRecord.SomeAddress2,11h
   MOV word ptr TestRecord.SomeAddress2,1122h
   ...

Use:
   MOV R11,offset TestRecord.SomeAddress2
   MOV byte ptr [R11],11h
   MOV R11,offset TestRecord.SomeAddress2
   MOV word ptr [R11],11h
   MOV R11,offset TestRecord.SomeAddress2
   MOV word ptr [R11],1122h
   ...
Attachment
129694.zip
Comments

None

Server Response from: ETNACODE01