Skip to content

Commit

Permalink
Refactor Disassembler class and add new Disassemble overloads (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
js6pak authored Jul 6, 2023
1 parent cb8877f commit a37eb58
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 116 deletions.
39 changes: 37 additions & 2 deletions Disarm.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public BasicTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) {
[Fact]
public void TestDisassembleEntireBody()
{
var result = Disassembler.DisassembleOnDemand(TestBodies.IncludesPcRelAddressing, 0);
var result = Disassembler.Disassemble(TestBodies.IncludesPcRelAddressing, 0);

foreach (var instruction in result)
{
Expand All @@ -20,11 +20,46 @@ public void TestDisassembleEntireBody()
[Fact]
public void TestLongerBody()
{
var result = Disassembler.DisassembleOnDemand(TestBodies.HasABadBitMask, 0);
var result = Disassembler.Disassemble(TestBodies.HasABadBitMask, 0);

foreach (var instruction in result)
{
OutputHelper.WriteLine(instruction.ToString());
}
}

[Fact]
public unsafe void TestOverloads()
{
byte[] byteArray = TestBodies.HasABadBitMask;
ReadOnlySpan<byte> span = byteArray;
ReadOnlyMemory<byte> memory = byteArray;
fixed (byte* bytePointer = byteArray)
{
using var byteArrayEnumerator = Disassembler.Disassemble(byteArray, 0).GetEnumerator();
using var spanEnumerator = Disassembler.Disassemble(span, 0).GetEnumerator();
using var spanListEnumerator = Disassembler.Disassemble(span, 0, out _).GetEnumerator();
using var memoryEnumerator = Disassembler.Disassemble(memory, 0).GetEnumerator();
using var bytePointerEnumerator = Disassembler.Disassemble(bytePointer, byteArray.Length, 0).GetEnumerator();

while (byteArrayEnumerator.MoveNext())
{
Assert.True(spanEnumerator.MoveNext());
Assert.True(spanListEnumerator.MoveNext());
Assert.True(memoryEnumerator.MoveNext());
Assert.True(bytePointerEnumerator.MoveNext());

var expected = byteArrayEnumerator.Current;
Assert.Equal(expected, spanEnumerator.Current);
Assert.Equal(expected, spanListEnumerator.Current);
Assert.Equal(expected, memoryEnumerator.Current);
Assert.Equal(expected, bytePointerEnumerator.Current);
}

Assert.False(spanEnumerator.MoveNext());
Assert.False(spanListEnumerator.MoveNext());
Assert.False(memoryEnumerator.MoveNext());
Assert.False(bytePointerEnumerator.MoveNext());
}
}
}
2 changes: 1 addition & 1 deletion Disarm.Tests/BranchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void BranchAddressesAreCorrect()
{
ulong address = 0x023b6a90;
var bytes = new byte[] { 0x3f, 0x69, 0xa2, 0x17 };
var insn = Disassembler.Disassemble(bytes, address).Instructions[0];
var insn = Disassembler.Disassemble(bytes, address).Single();

Assert.Equal(Arm64Mnemonic.B, insn.Mnemonic);
Assert.Equal(0xc50f8cU, insn.BranchTarget);
Expand Down
1 change: 1 addition & 0 deletions Disarm.Tests/Disarm.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
21 changes: 0 additions & 21 deletions Disarm/Arm64DisassemblyResult.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Disarm/Disarm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>Disarm</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/SamboyCoding/Disarm</PackageProjectUrl>
Expand All @@ -31,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="IndexRange" Version="1.0.2" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="PolySharp" Version="1.13.1" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="System.Memory" Version="4.5.5" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
Expand Down
Loading

0 comments on commit a37eb58

Please sign in to comment.