Skip to content

Commit

Permalink
Fix out of bounds vector access
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jul 3, 2024
1 parent fbd83a6 commit 80e8e4f
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/Nethermind/Nethermind.Evm/BitmapHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,50 +110,41 @@ private static void Set16(this Span<byte> bitvec, int pos)
bitvec[pos / 8 + 2] = (byte)~a;
}

private const uint Vector128ByteCount = 16;
private const uint Vector128IntCount = 4;
private const uint Vector256ByteCount = 32;
private const uint Vector256IntCount = 8;

public static bool CheckCollision(ReadOnlySpan<byte> codeSegments, ReadOnlySpan<byte> jumpmask)
{
int count = Math.Min(codeSegments.Length, jumpmask.Length);

uint i = 0;
int i = 0;

ref byte left = ref MemoryMarshal.GetReference<byte>(codeSegments);
ref byte right = ref MemoryMarshal.GetReference<byte>(jumpmask);

if (Vector256.IsHardwareAccelerated)
if (Vector256.IsHardwareAccelerated && count >= Vector256<byte>.Count)
{
Vector256<byte> zeros = Vector256.Create<byte>(0);
for (; i < (uint)count - (Vector256IntCount - 1u); i += Vector256IntCount)
for (; (uint)(i + Vector256<byte>.Count) <= (uint)count; i += Vector256<byte>.Count)
{
Vector256<byte> result = Vector256.LoadUnsafe(ref left, i) & Vector256.LoadUnsafe(ref right, i);
result = Vector256.Min(result, zeros);
if (Vector256.Sum(result) != 0)
Vector256<byte> result = Vector256.LoadUnsafe(ref left, (uint)i) & Vector256.LoadUnsafe(ref right, (uint)i);
if (result != default)
{
return true;
}
}
}
else if (Vector128.IsHardwareAccelerated)
else if (Vector128.IsHardwareAccelerated && count >= Vector128<byte>.Count)
{
Vector128<byte> zeros = Vector128.Create<byte>(0);
for (; i < (uint)count - (Vector128IntCount - 1u); i += Vector128IntCount)
for (; (i + Vector128<byte>.Count) <= (uint)count; i += Vector128<byte>.Count)
{
Vector128<byte> result = Vector128.LoadUnsafe(ref left, i) & Vector128.LoadUnsafe(ref right, i);
result = Vector128.Min(result, zeros);
if (Vector128.Sum(result) != 0)
Vector128<byte> result = Vector128.LoadUnsafe(ref left, (uint)i) & Vector128.LoadUnsafe(ref right, (uint)i);
if (result != default)
{
return true;
}
}
}

for (int j = (int)i; j < (uint)count; j++)
for (; i < count; i++)
{
if ((codeSegments[j] & jumpmask[j]) != 0)
if ((codeSegments[i] & jumpmask[i]) != 0)
{
return true;
}
Expand Down

0 comments on commit 80e8e4f

Please sign in to comment.