Skip to content

Conversation

Akeit0
Copy link

@Akeit0 Akeit0 commented Aug 3, 2025

Redundant checks were not removed by the JIT because bufferLength was in the field and also because of the possibility of overflow.

I pasted JIT asm for reference.
It is recommended to separate the window to see main and pr at the same time.

Envitonment

.net9.0
Windows11
13th Gen Intel(R) Core(TM) i7-13700F   2.10 GHz
main
; Assembly listing for method MemoryPack.MemoryPackWriter`1[System.__Canon]:WriteNullObjectHeader():this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 0 single block inlinees; 2 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rbx
       sub      rsp, 32
       mov      rbx, rcx

G_M000_IG02:                ;; offset=0x0008
       cmp      dword ptr [rbx+0x18], 0
       jg       SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x000E
       mov      rcx, rbx
       mov      r8d, 1
       call     [MemoryPack.MemoryPackWriter`1[System.__Canon]:RequestNewBuffer(int):this]

G_M000_IG04:                ;; offset=0x001D
       mov      rax, bword ptr [rbx+0x10]
       mov      byte  ptr [rax], 255
       mov      eax, dword ptr [rbx+0x18]
       dec      eax
       js       SHORT G_M000_IG06
       mov      dword ptr [rbx+0x18], eax
       inc      bword ptr [rbx+0x10]
       inc      dword ptr [rbx+0x1C]
       inc      dword ptr [rbx+0x24]

G_M000_IG05:                ;; offset=0x0038
       add      rsp, 32
       pop      rbx
       ret

G_M000_IG06:                ;; offset=0x003E
       call     [MemoryPack.MemoryPackSerializationException:ThrowInvalidAdvance()]
       int3

; Total bytes of code 69

; Assembly listing for method MemoryPack.MemoryPackWriter`1[System.__Canon]:WriteUnmanaged[int](byref):this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 0 single block inlinees; 2 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rsi
       push     rbx
       sub      rsp, 40
       mov      qword ptr [rsp+0x20], rdx
       mov      rbx, rcx
       mov      rsi, r8

G_M000_IG02:                ;; offset=0x0011
       mov      rcx, qword ptr [rdx+0x18]
       mov      rcx, qword ptr [rcx+0x10]
       test     rcx, rcx
       je       SHORT G_M000_IG07

G_M000_IG03:                ;; offset=0x001E
       mov      rdx, rcx

G_M000_IG04:                ;; offset=0x0021
       cmp      dword ptr [rbx+0x18], 4
       jge      SHORT G_M000_IG06

G_M000_IG05:                ;; offset=0x0027
       mov      rcx, rbx
       mov      r8d, 4
       call     [MemoryPack.MemoryPackWriter`1[System.__Canon]:RequestNewBuffer(int):this]

G_M000_IG06:                ;; offset=0x0036
       mov      rax, bword ptr [rbx+0x10]
       mov      ecx, dword ptr [rsi]
       mov      dword ptr [rax], ecx
       mov      eax, dword ptr [rbx+0x18]
       add      eax, -4
       jns      SHORT G_M000_IG08
       jmp      SHORT G_M000_IG10

G_M000_IG07:                ;; offset=0x0048
       mov      rcx, rdx
       mov      rdx, 0x7FF8A68682F8
       call     CORINFO_HELP_RUNTIMEHANDLE_METHOD
       mov      rdx, rax
       jmp      SHORT G_M000_IG04

G_M000_IG08:                ;; offset=0x005F
       mov      dword ptr [rbx+0x18], eax
       add      bword ptr [rbx+0x10], 4
       add      dword ptr [rbx+0x1C], 4
       add      dword ptr [rbx+0x20], 4

G_M000_IG09:                ;; offset=0x006F
       add      rsp, 40
       pop      rbx
       pop      rsi
       ret

G_M000_IG10:                ;; offset=0x0076
       call     [MemoryPack.MemoryPackSerializationException:ThrowInvalidAdvance()]
       int3

; Total bytes of code 125

; Assembly listing for method MemoryPack.MemoryPackReader:TryReadObjectHeader(byref):ubyte:this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 0 single block inlinees; 2 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rdi
       push     rsi
       push     rbx
       sub      rsp, 32
       mov      rbx, rcx
       mov      rsi, rdx

G_M000_IG02:                ;; offset=0x000D
       cmp      dword ptr [rbx+0x20], 0
       jg       SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x0013
       mov      rcx, rbx
       mov      edx, 1
       call     [MemoryPack.MemoryPackReader:GetNextSpan(int):byref:this]
       jmp      SHORT G_M000_IG05

G_M000_IG04:                ;; offset=0x0023
       mov      rax, bword ptr [rbx+0x18]

G_M000_IG05:                ;; offset=0x0027
       movzx    rcx, byte  ptr [rax]
       mov      byte  ptr [rsi], cl
       mov      edi, dword ptr [rbx+0x20]
       dec      edi
       jns      SHORT G_M000_IG07

G_M000_IG06:                ;; offset=0x0033
       mov      rcx, rbx
       mov      edx, 1
       call     [MemoryPack.MemoryPackReader:TryAdvanceSequence(int):ubyte:this]
       test     eax, eax
       jne      SHORT G_M000_IG08

G_M000_IG07:                ;; offset=0x0045
       mov      dword ptr [rbx+0x20], edi
       inc      bword ptr [rbx+0x18]
       inc      dword ptr [rbx+0x24]
       inc      dword ptr [rbx+0x28]

G_M000_IG08:                ;; offset=0x0052
       cmp      byte  ptr [rsi], 255
       setne    al
       movzx    rax, al

G_M000_IG09:                ;; offset=0x005B
       add      rsp, 32
       pop      rbx
       pop      rsi
       pop      rdi
       ret

; Total bytes of code 99

; Assembly listing for method MemoryPack.MemoryPackReader:ReadUnmanaged[int]():int:this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 0 single block inlinees; 2 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rdi
       push     rsi
       push     rbx
       sub      rsp, 32
       mov      rbx, rcx

G_M000_IG02:                ;; offset=0x000A
       cmp      dword ptr [rbx+0x20], 4
       jge      SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x0010
       mov      rcx, rbx
       mov      edx, 4
       call     [MemoryPack.MemoryPackReader:GetNextSpan(int):byref:this]
       jmp      SHORT G_M000_IG05

G_M000_IG04:                ;; offset=0x0020
       mov      rax, bword ptr [rbx+0x18]

G_M000_IG05:                ;; offset=0x0024
       mov      esi, dword ptr [rax]
       mov      edi, dword ptr [rbx+0x20]
       add      edi, -4
       jns      SHORT G_M000_IG07

G_M000_IG06:                ;; offset=0x002E
       mov      rcx, rbx
       mov      edx, 4
       call     [MemoryPack.MemoryPackReader:TryAdvanceSequence(int):ubyte:this]
       test     eax, eax
       jne      SHORT G_M000_IG08

G_M000_IG07:                ;; offset=0x0040
       mov      dword ptr [rbx+0x20], edi
       add      bword ptr [rbx+0x18], 4
       add      dword ptr [rbx+0x24], 4
       add      dword ptr [rbx+0x28], 4

G_M000_IG08:                ;; offset=0x0050
       mov      eax, esi

G_M000_IG09:                ;; offset=0x0052
       add      rsp, 32
       pop      rbx
       pop      rsi
       pop      rdi
       ret

; Total bytes of code 90

; Assembly listing for method MemoryPack.MemoryPackReader:ReadUnmanaged[int](byref):this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 0 single block inlinees; 2 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rsi
       push     rbx
       sub      rsp, 40
       mov      rbx, rcx
       mov      rsi, rdx

G_M000_IG02:                ;; offset=0x000C
       cmp      dword ptr [rbx+0x20], 4
       jge      SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x0012
       mov      rcx, rbx
       mov      edx, 4
       call     [MemoryPack.MemoryPackReader:GetNextSpan(int):byref:this]
       jmp      SHORT G_M000_IG05

G_M000_IG04:                ;; offset=0x0022
       mov      rax, bword ptr [rbx+0x18]

G_M000_IG05:                ;; offset=0x0026
       mov      ecx, dword ptr [rax]
       mov      dword ptr [rsi], ecx
       mov      esi, dword ptr [rbx+0x20]
       add      esi, -4
       jns      SHORT G_M000_IG07

G_M000_IG06:                ;; offset=0x0032
       mov      rcx, rbx
       mov      edx, 4
       call     [MemoryPack.MemoryPackReader:TryAdvanceSequence(int):ubyte:this]
       test     eax, eax
       jne      SHORT G_M000_IG08

G_M000_IG07:                ;; offset=0x0044
       mov      dword ptr [rbx+0x20], esi
       add      bword ptr [rbx+0x18], 4
       add      dword ptr [rbx+0x24], 4
       add      dword ptr [rbx+0x28], 4

G_M000_IG08:                ;; offset=0x0054
       add      rsp, 40
       pop      rbx
       pop      rsi
       ret

; Total bytes of code 91
pr
; Assembly listing for method MemoryPack.MemoryPackWriter`1[System.__Canon]:WriteNullObjectHeader():this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 1 single block inlinees; 1 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rbx
       sub      rsp, 32
       mov      rbx, rcx

G_M000_IG02:                ;; offset=0x0008
       cmp      dword ptr [rbx+0x18], 0
       jg       SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x000E
       mov      rcx, rbx
       mov      r8d, 1
       call     [MemoryPack.MemoryPackWriter`1[System.__Canon]:RequestNewBuffer(int):this]

G_M000_IG04:                ;; offset=0x001D
       mov      rax, bword ptr [rbx+0x10]
       mov      byte  ptr [rax], 255
       dec      dword ptr [rbx+0x18]
       inc      bword ptr [rbx+0x10]
       inc      dword ptr [rbx+0x1C]
       inc      dword ptr [rbx+0x24]

G_M000_IG05:                ;; offset=0x0031
       add      rsp, 32
       pop      rbx
       ret

; Total bytes of code 55

; Assembly listing for method MemoryPack.MemoryPackWriter`1[System.__Canon]:WriteUnmanaged[int](byref):this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 1 single block inlinees; 1 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rsi
       push     rbx
       sub      rsp, 40
       mov      qword ptr [rsp+0x20], rdx
       mov      rbx, rcx
       mov      rsi, r8

G_M000_IG02:                ;; offset=0x0011
       mov      rcx, qword ptr [rdx+0x18]
       mov      rcx, qword ptr [rcx+0x10]
       test     rcx, rcx
       je       SHORT G_M000_IG08

G_M000_IG03:                ;; offset=0x001E
       mov      rdx, rcx

G_M000_IG04:                ;; offset=0x0021
       cmp      dword ptr [rbx+0x18], 4
       jge      SHORT G_M000_IG06

G_M000_IG05:                ;; offset=0x0027
       mov      rcx, rbx
       mov      r8d, 4
       call     [MemoryPack.MemoryPackWriter`1[System.__Canon]:RequestNewBuffer(int):this]

G_M000_IG06:                ;; offset=0x0036
       mov      rax, bword ptr [rbx+0x10]
       mov      ecx, dword ptr [rsi]
       mov      dword ptr [rax], ecx
       add      bword ptr [rbx+0x10], 4
       add      dword ptr [rbx+0x18], -4
       add      dword ptr [rbx+0x1C], 4
       add      dword ptr [rbx+0x20], 4

G_M000_IG07:                ;; offset=0x004F
       add      rsp, 40
       pop      rbx
       pop      rsi
       ret

G_M000_IG08:                ;; offset=0x0056
       mov      rcx, rdx
       mov      rdx, 0x7FF8A68684A8
       call     CORINFO_HELP_RUNTIMEHANDLE_METHOD
       mov      rdx, rax
       jmp      SHORT G_M000_IG04

; Total bytes of code 109

; Assembly listing for method MemoryPack.MemoryPackReader:TryReadObjectHeader(byref):ubyte:this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 1 single block inlinees; 1 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rsi
       push     rbx
       sub      rsp, 40
       mov      rbx, rcx
       mov      rsi, rdx

G_M000_IG02:                ;; offset=0x000C
       cmp      dword ptr [rbx+0x20], 0
       jg       SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x0012
       mov      rcx, rbx
       mov      edx, 1
       call     [MemoryPack.MemoryPackReader:GetNextSpan(int):byref:this]
       jmp      SHORT G_M000_IG05

G_M000_IG04:                ;; offset=0x0022
       mov      rax, bword ptr [rbx+0x18]

G_M000_IG05:                ;; offset=0x0026
       movzx    rax, byte  ptr [rax]
       mov      byte  ptr [rsi], al
       dec      dword ptr [rbx+0x20]
       inc      bword ptr [rbx+0x18]
       inc      dword ptr [rbx+0x24]
       inc      dword ptr [rbx+0x28]
       cmp      byte  ptr [rsi], 255
       setne    al
       movzx    rax, al

G_M000_IG06:                ;; offset=0x0041
       add      rsp, 40
       pop      rbx
       pop      rsi
       ret

; Total bytes of code 72

; Assembly listing for method MemoryPack.MemoryPackReader:ReadUnmanaged[int]():int:this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 1 single block inlinees; 1 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rbx
       sub      rsp, 32
       mov      rbx, rcx

G_M000_IG02:                ;; offset=0x0008
       cmp      dword ptr [rbx+0x20], 4
       jge      SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x000E
       mov      rcx, rbx
       mov      edx, 4
       call     [MemoryPack.MemoryPackReader:GetNextSpan(int):byref:this]
       jmp      SHORT G_M000_IG05

G_M000_IG04:                ;; offset=0x001E
       mov      rax, bword ptr [rbx+0x18]

G_M000_IG05:                ;; offset=0x0022
       mov      eax, dword ptr [rax]
       add      dword ptr [rbx+0x20], -4
       add      bword ptr [rbx+0x18], 4
       add      dword ptr [rbx+0x24], 4
       add      dword ptr [rbx+0x28], 4

G_M000_IG06:                ;; offset=0x0035
       add      rsp, 32
       pop      rbx
       ret

; Total bytes of code 59

; Assembly listing for method MemoryPack.MemoryPackReader:ReadUnmanaged[int](byref):this (FullOpts)
; Emitting BLENDED_CODE for X64 with AVX - Windows
; FullOpts code
; optimized code
; rsp based frame
; partially interruptible
; No PGO data
; 0 inlinees with PGO data; 1 single block inlinees; 1 inlinees without PGO data

G_M000_IG01:                ;; offset=0x0000
       push     rsi
       push     rbx
       sub      rsp, 40
       mov      rbx, rcx
       mov      rsi, rdx

G_M000_IG02:                ;; offset=0x000C
       cmp      dword ptr [rbx+0x20], 4
       jge      SHORT G_M000_IG04

G_M000_IG03:                ;; offset=0x0012
       mov      rcx, rbx
       mov      edx, 4
       call     [MemoryPack.MemoryPackReader:GetNextSpan(int):byref:this]
       jmp      SHORT G_M000_IG05

G_M000_IG04:                ;; offset=0x0022
       mov      rax, bword ptr [rbx+0x18]

G_M000_IG05:                ;; offset=0x0026
       mov      eax, dword ptr [rax]
       mov      dword ptr [rsi], eax
       add      dword ptr [rbx+0x20], -4
       add      bword ptr [rbx+0x18], 4
       add      dword ptr [rbx+0x24], 4
       add      dword ptr [rbx+0x28], 4

G_M000_IG06:                ;; offset=0x003B
       add      rsp, 40
       pop      rbx
       pop      rsi
       ret

; Total bytes of code 66

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.

1 participant