Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created November 6, 2021 06:10
Show Gist options
  • Save guitarrapc/da9b7f71d831c793f4c8c83cf06fa5d3 to your computer and use it in GitHub Desktop.
Save guitarrapc/da9b7f71d831c793f4c8c83cf06fa5d3 to your computer and use it in GitHub Desktop.
Array Bounds Checks betwen .NET 5 and .NET 6. https://twitter.com/badamczewski01/status/1456711012099428359
// before .NET 5: Array Bound Check emitted. (BAD)
// after .NET 6: Array Bound Check removed. (GOOD)
void A(int[] a)
{
if (a.Length > 3)
{
a[0] = 1;
a[1] = 2;
a[2] = 3;
}
}
L0000 sub rsp, 0x28
L0004 mov eax, [rcx+8]
L0007 cmp eax, 3
L000a jle short L0030
L000c cmp eax, 0
L000f jbe short L0035
L0011 mov dword ptr [rcx+0x10], 1
L0018 cmp eax, 1
L001b jbe short L0035
L001d mov dword ptr [rcx+0x14], 2
L0024 cmp eax, 2
L0027 jbe short L0035
L0029 mov dword ptr [rcx+0x18], 3
L0030 add rsp, 0x28
L0034 ret
L0035 call 0x00007ffa1f1eaac0
L003a int3
L0000 mov eax, [rcx+8]
L0003 cmp eax, 3
L0006 jle short L001d
L0008 mov dword ptr [rcx+0x10], 1
L000f mov dword ptr [rcx+0x14], 2
L0016 mov dword ptr [rcx+0x18], 3
L001d ret
// before .NET 5: Array Bound Check emitted. (BAD)
// after .NET 6: Array Bound Check removed. (GOOD)
void A(int[] a)
{
if (a.Length < 3) // if > 3. Array Bounds check are emitted.
return;
a[0] = 1;
a[1] = 2;
a[2] = 3;
}
L0000 sub rsp, 0x28
L0004 mov eax, [rcx+8]
L0007 cmp eax, 3
L000a jge short L0011
L000c add rsp, 0x28
L0010 ret
L0011 cmp eax, 0
L0014 jbe short L003a
L0016 mov dword ptr [rcx+0x10], 1
L001d cmp eax, 1
L0020 jbe short L003a
L0022 mov dword ptr [rcx+0x14], 2
L0029 cmp eax, 2
L002c jbe short L003a
L002e mov dword ptr [rcx+0x18], 3
L0035 add rsp, 0x28
L0039 ret
L003a call 0x00007ffa1f1eaac0
L003f int3
L0000 mov eax, [rcx+8]
L0003 cmp eax, 3
L0006 jge short L0009
L0008 ret
L0009 mov dword ptr [rcx+0x10], 1
L0010 mov dword ptr [rcx+0x14], 2
L0017 mov dword ptr [rcx+0x18], 3
L001e ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment