Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/
/_ReSharper.Caches/
/.vs
16 changes: 14 additions & 2 deletions MemNet.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Memlib", "MemNet\MemNet.csproj", "{CA5757C4-1576-4CE1-AD33-18C0A6E3BEEF}"
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MemNet", "MemNet\MemNet.csproj", "{CA5757C4-1576-4CE1-AD33-18C0A6E3BEEF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{CB0F42DE-E293-4018-8BC0-0D82721A3717}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Reader", "Examples\Pattern Scanner\Pattern Scanner.csproj", "{36FA29A7-59CB-40EA-B2D2-066D7B9F08C1}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pattern Scanner", "Examples\Pattern Scanner\Pattern Scanner.csproj", "{36FA29A7-59CB-40EA-B2D2-066D7B9F08C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing", "Testing\Testing.csproj", "{24F0E9FF-1BDE-41E8-BE50-BE33D30AC3C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -20,6 +25,13 @@ Global
{36FA29A7-59CB-40EA-B2D2-066D7B9F08C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36FA29A7-59CB-40EA-B2D2-066D7B9F08C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36FA29A7-59CB-40EA-B2D2-066D7B9F08C1}.Release|Any CPU.Build.0 = Release|Any CPU
{24F0E9FF-1BDE-41E8-BE50-BE33D30AC3C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{24F0E9FF-1BDE-41E8-BE50-BE33D30AC3C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24F0E9FF-1BDE-41E8-BE50-BE33D30AC3C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24F0E9FF-1BDE-41E8-BE50-BE33D30AC3C6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{36FA29A7-59CB-40EA-B2D2-066D7B9F08C1} = {CB0F42DE-E293-4018-8BC0-0D82721A3717}
Expand Down
8 changes: 8 additions & 0 deletions MemNet/Wildcard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public Wildcard(string token)
_lowNibble = token[1] == '?' ? null : Convert.ToInt32(token[1].ToString(), 16);
}

public byte? AsByte()
{
if (!_highNibble.HasValue || !_lowNibble.HasValue)
return null;

return (byte)((_highNibble.Value << 4) | _lowNibble.Value);
}

public bool Matches(byte b)
{
int high = (b >> 4) & 0xF;
Expand Down
112 changes: 112 additions & 0 deletions Testing/AobTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Memlib;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Testing;

[TestFixture]
internal class AobTests
{
[Test]
[Repeat(5_000)]
public unsafe void AobGeneralTest()
{
const int length = 0x2000;
var pArr = Marshal.AllocHGlobal(length);
Span<byte> bytes;
unsafe
{
bytes = new Span<byte>(pArr.ToPointer(), length);
}

var rand = new Random();
rand.NextBytes(bytes);

try
{
var pid = Environment.ProcessId;
using var mem = new Memlib.Memory(pid);
mem.Open();

var start = rand.Next(0, bytes.Length - 1);
var end = rand.Next(start + 1, bytes.Length);
Trace.Assert(end - start >= 0); // Sanity check

var pattern = ScanHelper.GeneratePattern(
bytes[start..end],
out var pb0,
out var pbn,
rand);

var results = mem.Search(pattern, pb0, pbn);
foreach (var address in results)
{
var offset = (int)(address - pArr);

for (var j = 0; j < pattern.Length; j++)
{
var patternByte = pattern[j].AsByte();
var matchedByte = bytes[j + offset];
var matches = pattern[j].Matches(matchedByte);
Assert.That(matches, Is.True);
}
}
}
catch (Exception)
{
throw;
}
finally
{
Marshal.FreeHGlobal(pArr);
}
}

[Test]
public void AobFullLengthTest()
{
byte[] bytes = [0xAA, 0xBB, 0xCC, 0xDD];
const string pattern = "AA BB ?? DD";

var pid = Environment.ProcessId;
using var mem = new Memlib.Memory(pid);
mem.Open();

unsafe
{
fixed (byte* pb0 = bytes)
{
var results = mem.Search(pattern, new nint(pb0), new nint(pb0 + bytes.Length));
Assert.That(results, Is.Not.Null);
Assert.That(results.Count, Is.EqualTo(1));
var pb0nint = new nint(pb0); // Can't capture fixed locals so do a little trickery
Assert.That(results.All(p => p == pb0nint), Is.True);
}
}
}

[Test]
public unsafe void EdgeCases()
{
var rand = new Random();
using var mem = new Memlib.Memory(Environment.ProcessId);
mem.Open();

var bytes = new byte[1];
var pattern = new Wildcard[] { new("??"), new("??") };
fixed (byte* pb0 = bytes)
{
var pb0nint = new nint(pb0);
Assert.Throws<ArgumentOutOfRangeException>(
() => mem.Search(pattern, pb0nint, pb0nint));
Assert.Throws<ArgumentException>(
() => mem.Search(pattern, pb0nint, pb0nint + 1));
}
}
}
96 changes: 96 additions & 0 deletions Testing/ReadWriteTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Testing;

[TestFixture]
internal class ReadWriteTests
{
const int Length = 0x40;

private static unsafe void ReadTest<T>(T src, ref T dst) where T : unmanaged
{
var defensiveSrcCopy = src;
var pid = Environment.ProcessId;
using var mem = new Memlib.Memory(pid);
mem.Open();

var pSrc = new nint(&src);
dst = mem.Read<T>(pSrc);

Assert.That(src, Is.EqualTo(defensiveSrcCopy));
Assert.That(dst, Is.EqualTo(src));
}

private static unsafe void WriteTest<T>(T src, ref T dst) where T : unmanaged
{
var defensiveSrcCopy = src;
var pid = Environment.ProcessId;
using var mem = new Memlib.Memory(pid);
mem.Open();

fixed (T* pDst = &dst)
{
mem.Write(new nint(pDst), src);
}

Assert.That(src, Is.EqualTo(defensiveSrcCopy));
Assert.That(dst, Is.EqualTo(src));
}

private static unsafe void PerformReadWriteTest<T>(int length, Random rand) where T : unmanaged
{
Span<T> src = stackalloc T[length];
Span<T> dst = stackalloc T[length];

rand.NextBytes(MemoryMarshal.AsBytes(src));

var pid = Environment.ProcessId;
using var mem = new Memlib.Memory(pid);
mem.Open();

for (var i = 0; i < length; i++)
{
ReadTest(src[i], ref dst[i]);
}

dst.Clear();
rand.NextBytes(MemoryMarshal.AsBytes(src));

for (var i = 0; i < length; i++)
{
WriteTest(src[i], ref dst[i]);
}
}

[Test]
public unsafe void BasicReadWriteTest()
{
PerformReadWriteTest<byte>(Length, Random.Shared);
PerformReadWriteTest<sbyte>(Length, Random.Shared);
PerformReadWriteTest<short>(Length, Random.Shared);
PerformReadWriteTest<ushort>(Length, Random.Shared);
PerformReadWriteTest<int>(Length, Random.Shared);
PerformReadWriteTest<uint>(Length, Random.Shared);
PerformReadWriteTest<long>(Length, Random.Shared);
PerformReadWriteTest<ulong>(Length, Random.Shared);
PerformReadWriteTest<Int128>(Length, Random.Shared);
PerformReadWriteTest<UInt128>(Length, Random.Shared);
PerformReadWriteTest<Half>(Length, Random.Shared);
PerformReadWriteTest<float>(Length, Random.Shared);
PerformReadWriteTest<double>(Length, Random.Shared);
PerformReadWriteTest<decimal>(Length, Random.Shared);
PerformReadWriteTest<Vector2>(Length, Random.Shared);
PerformReadWriteTest<Vector3>(Length, Random.Shared);
PerformReadWriteTest<Vector4>(Length, Random.Shared);
PerformReadWriteTest<Matrix4x4>(Length, Random.Shared);
PerformReadWriteTest<Quaternion>(Length, Random.Shared);
}
}
54 changes: 54 additions & 0 deletions Testing/ScanHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Memlib;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Testing;
internal static class ScanHelper
{
public static byte[] GenerateBytes(int length, Random rand)
{
var bytes = new byte[length];
rand.NextBytes(bytes);
return bytes;
}

public static unsafe Wildcard[] GeneratePattern(ReadOnlySpan<byte> bytes, out nint start, out nint end, Random? rand = null)
{
rand ??= new Random();

fixed (byte* pb0 = bytes)
{
var pbn = pb0 + bytes.Length;
start = new nint(pb0);
end = new nint(pbn);

// Sanity check
fixed (byte* endInclusive = &bytes[^1])
Trace.Assert(pbn == endInclusive + 1);

var pattern = new Wildcard[bytes.Length];
var unknownChance = rand.NextSingle();
for (var offset = 0; pb0 + offset < pbn; offset++)
{
var isUnknown = rand.NextSingle() < unknownChance;
if (isUnknown)
{
pattern[offset] = new Wildcard();
continue;
}

var b = pb0[offset];
var hex = b.ToString("X2");
pattern[offset] = new Wildcard(hex);
}

return pattern;
}
}
}
21 changes: 21 additions & 0 deletions Testing/Testing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MemNet\MemNet.csproj" />
</ItemGroup>

</Project>