Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) and remove the use of unsafe #3492

Merged
merged 16 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ paket-files/
PublishProfiles
/.vscode
launchSettings.json
/coverages
**/.DS_Store

# Benchmarks
**/BenchmarkDotNet.Artifacts/
**/BenchmarkDotNet.Artifacts/
2 changes: 1 addition & 1 deletion src/Neo.VM/Types/ByteString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal bool Equals(StackItem? other, ref uint limits)
public override bool GetBoolean()
{
if (Size > Integer.MaxSize) throw new InvalidCastException();
return Unsafe.NotZero(GetSpan());
return GetSpan().NotZero();
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

public override BigInteger GetInteger()
Expand Down
11 changes: 10 additions & 1 deletion src/Neo.VM/Unsafe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,17 @@ unsafe internal static class Unsafe
{
private const long DefaultXxHash3Seed = 40343;

/// <summary>
/// All bytes are zero or not in a byte array
/// </summary>
/// <param name="x">The byte array</param>
/// <returns>false if all bytes are zero, true otherwise</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool NotZero(ReadOnlySpan<byte> x)
public static bool NotZero(this ReadOnlySpan<byte> x)
{
#if NET7_0_OR_GREATER
return x.IndexOfAnyExcept((byte)0) >= 0;
#else
int len = x.Length;
if (len == 0) return false;
fixed (byte* xp = x)
shargon marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -40,6 +48,7 @@ public static bool NotZero(ReadOnlySpan<byte> x)
}
}
return false;
#endif
}

/// <summary>
Expand Down
33 changes: 0 additions & 33 deletions tests/Neo.VM.Tests/UT_Unsafe.cs

This file was deleted.

31 changes: 31 additions & 0 deletions tests/Neo.VM.Tests/UT_Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,36 @@ public void TestModInverseTest()

Assert.AreEqual(new BigInteger(52), new BigInteger(19).ModInverse(141));
}

[TestMethod]
public void TestNotZero()
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.IsFalse(new ReadOnlySpan<byte>(System.Array.Empty<byte>()).NotZero());
Assert.IsFalse(new ReadOnlySpan<byte>(new byte[4]).NotZero());
Assert.IsFalse(new ReadOnlySpan<byte>(new byte[7]).NotZero());
Assert.IsFalse(new ReadOnlySpan<byte>(new byte[8]).NotZero());
Assert.IsFalse(new ReadOnlySpan<byte>(new byte[9]).NotZero());
Assert.IsFalse(new ReadOnlySpan<byte>(new byte[11]).NotZero());

Assert.IsTrue(new ReadOnlySpan<byte>(new byte[4] { 0x00, 0x00, 0x00, 0x01 }).NotZero());
Assert.IsTrue(new ReadOnlySpan<byte>(new byte[7] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }).NotZero());
Assert.IsTrue(new ReadOnlySpan<byte>(new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }).NotZero());
Assert.IsTrue(new ReadOnlySpan<byte>(new byte[9] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 }).NotZero());
Assert.IsTrue(new ReadOnlySpan<byte>(new byte[11] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }).NotZero());

var bytes = new byte[64];
for (int i = 0; i < bytes.Length; i++)
{
ReadOnlySpan<byte> span = bytes.AsSpan();
Assert.IsFalse(span[i..].NotZero());

for (int j = i; j < bytes.Length; j++)
{
bytes[j] = 0x01;
Assert.IsTrue(span[i..].NotZero());
bytes[j] = 0x00;
}
}
}
}
}
Loading