Skip to content

Fix genCodeForIndexAddr#3

Open
villelaitila wants to merge 1 commit into
masterfrom
pull-22528
Open

Fix genCodeForIndexAddr#3
villelaitila wants to merge 1 commit into
masterfrom
pull-22528

Conversation

@villelaitila
Copy link
Copy Markdown

This does some weird things - treats the array length as 64 bit when it's in fact 32 bit, fails to zero extend TYP_INT indices, creates new GT_IND/GT_LEA nodes out of thin air.

dotnet#20126 has a similar fix for ARM.

Sample generated code for a[i + 2]:

; before
       488B4510             mov      rax, gword ptr [rbp+10H]
       8B5518               mov      edx, dword ptr [rbp+18H]
       83C202               add      edx, 2
; 64 bit compare, typically works correctly due to the zero-extend to 64 bit model
; and because the array length field is followed by 4 bytes of padding that are
; normally 0
       483B5008             cmp      rdx, qword ptr [rax+8]
       730D                 jae      SHORT G_M55886_IG04
; use a TYP_INT value as 64 bit
       488D449010           lea      rax, bword ptr [rax+4*rdx+16]

; after
       488B4510             mov      rax, gword ptr [rbp+10H]
       8B5518               mov      edx, dword ptr [rbp+18H]
       83C202               add      edx, 2
       3B5008               cmp      edx, dword ptr [rax+8]
       730F                 jae      SHORT G_M55886_IG04
       8BD2                 mov      edx, edx
       488D449010           lea      rax, bword ptr [rax+4*rdx+16]

It's possible to construct contrived examples where the upper 32 bits are not zero: a[(int)checked(longVar + 2)]:

; before
       488B4510             mov      rax, gword ptr [rbp+10H]
       BA02000000           mov      edx, 2
       4863D2               movsxd   rdx, edx
       48035518             add      rdx, qword ptr [rbp+18H]
       7013                 jo       SHORT G_M55886_IG04
; index used as 64 bit, basically the cast to `int` was dropped
       483B5008             cmp      rdx, qword ptr [rax+8]
       7312                 jae      SHORT G_M55886_IG05
       488D449010           lea      rax, bword ptr [rax+4*rdx+16]

; after
       488B4510             mov      rax, gword ptr [rbp+10H]
       BA02000000           mov      edx, 2
       4863D2               movsxd   rdx, edx
       48035518             add      rdx, qword ptr [rbp+18H]
       7014                 jo       SHORT G_M55886_IG04
; 32 bit compare
       3B5008               cmp      edx, dword ptr [rax+8]
       7314                 jae      SHORT G_M55886_IG05
; zero extend to 64 bit
       8BD2                 mov      edx, edx
       488D449010           lea      rax, bword ptr [rax+4*rdx+16]

This does some weird things - treats the array length as 64 bit when it's in fact 32 bit, fails to zero extend TYP_INT indices, creates new GT_IND/GT_LEA nodes out of thin air.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants