Skip to content

Commit

Permalink
Refactor to target .NET Standard 1.1 (#344)
Browse files Browse the repository at this point in the history
* Refactor to target .NET Standard 1.1

* Added Nuget details to .csproj

* Reviewed Engine methods and docs; added own Exception type.

* Added migration guide for .NET bindings.
  • Loading branch information
Grégoire Geis authored and aquynh committed Aug 2, 2018
1 parent 3ea06c9 commit 0ab2b81
Show file tree
Hide file tree
Showing 34 changed files with 831 additions and 962 deletions.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions bindings/csharp/Keystone.Net.Tests/Keystone.Net.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RootNamespace>Keystone.Tests</RootNamespace>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../Keystone.Net/Keystone.Net.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Shouldly" Version="3.0.0" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions bindings/csharp/Keystone.Net.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using NUnit.Framework;
using Shouldly;

namespace Keystone.Tests
{
[TestFixture]
public class ExecutionTests
{
[OneTimeSetUp]
public static void InitializeKeystone()
{
// Ensures the lib could be loaded
Engine.IsArchitectureSupported(Architecture.X86).ShouldBeTrue();
}

[Test]
public void ShouldEmitValidX86Data()
{
using (Engine engine = new Engine(Architecture.X86, Mode.X32) { ThrowOnError = true })
{
engine.Assemble("nop", 0).Buffer.ShouldBe(new byte[] { 0x90 });
engine.Assemble("add eax, eax", 0).Buffer.ShouldBe(new byte[] { 0x01, 0xC0 });
}
}

[Test]
public void ShouldEmitValidARMData()
{
using (Engine engine = new Engine(Architecture.ARM, Mode.ARM) { ThrowOnError = true })
{
engine.Assemble("mul r1, r0, r0", 0).Buffer.ShouldBe(new byte[] { 0x90, 0x00, 0x01, 0xE0 });
}
}

[Test]
public void ShouldThrowOnError()
{
using (Engine engine = new Engine(Architecture.ARM, Mode.ARM) { ThrowOnError = false })
{
engine.Assemble("push eax, 0x42", 0).ShouldBeNull();
engine.Assemble("doesntexist", 0).ShouldBeNull();
}

using (Engine engine = new Engine(Architecture.ARM, Mode.ARM) { ThrowOnError = true })
{
Should.Throw<KeystoneException>(() => engine.Assemble("push eax, 0x42", 0));
Should.Throw<KeystoneException>(() => engine.Assemble("doestexist", 0));
}
}

[Test, Ignore("Feature requires Keystone built after October 7th 2016.")]
public void ShouldHaveValidExample()
{
using (Engine keystone = new Engine(Architecture.X86, Mode.X32) { ThrowOnError = true })
{
ulong address = 0;

keystone.ResolveSymbol += (string s, ref ulong w) =>
{
if (s == "_j1")
{
w = 0x1234abcd;
return true;
}

return false;
};

EncodedData enc = keystone.Assemble("xor eax, eax; jmp _j1", address);

enc.Buffer.ShouldBe(new byte[] { 0x00 });
enc.Address.ShouldBe(address);
enc.StatementCount.ShouldBe(3);
}
}
}
}
50 changes: 50 additions & 0 deletions bindings/csharp/Keystone.Net.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2036
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Keystone.Net", "Keystone.Net\Keystone.Net.csproj", "{E3579F77-600A-4146-9296-3834B81F16E2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Keystone.Net.Tests", "Keystone.Net.Tests\Keystone.Net.Tests.csproj", "{377B6FDC-156F-4B8B-B693-2A5C7B604A97}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E3579F77-600A-4146-9296-3834B81F16E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Debug|x64.ActiveCfg = Debug|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Debug|x64.Build.0 = Debug|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Debug|x86.ActiveCfg = Debug|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Debug|x86.Build.0 = Debug|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Release|Any CPU.Build.0 = Release|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Release|x64.ActiveCfg = Release|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Release|x64.Build.0 = Release|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Release|x86.ActiveCfg = Release|Any CPU
{E3579F77-600A-4146-9296-3834B81F16E2}.Release|x86.Build.0 = Release|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Debug|x64.ActiveCfg = Debug|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Debug|x64.Build.0 = Debug|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Debug|x86.ActiveCfg = Debug|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Debug|x86.Build.0 = Debug|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Release|Any CPU.Build.0 = Release|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Release|x64.ActiveCfg = Release|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Release|x64.Build.0 = Release|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Release|x86.ActiveCfg = Release|Any CPU
{377B6FDC-156F-4B8B-B693-2A5C7B604A97}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0DA5689F-C570-41D7-93A8-F33F20F4B618}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [arm64Constants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum Arm64Error : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [armConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum ArmError : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [hexagonConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum HexagonError : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [keystoneConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum KeystoneArchitecture : int
public enum Architecture : int
{
KS_ARCH_ARM = 1,
KS_ARCH_ARM64 = 2,
KS_ARCH_MIPS = 3,
KS_ARCH_X86 = 4,
KS_ARCH_PPC = 5,
KS_ARCH_SPARC = 6,
KS_ARCH_SYSTEMZ = 7,
KS_ARCH_HEXAGON = 8,
KS_ARCH_MAX = 9,
ARM = 1,
ARM64 = 2,
MIPS = 3,
X86 = 4,
PPC = 5,
SPARC = 6,
SYSTEMZ = 7,
HEXAGON = 8,
MAX = 9,
}

public enum KeystoneMode : uint
public enum Mode : uint
{
KS_MODE_LITTLE_ENDIAN = 0,
KS_MODE_BIG_ENDIAN = 1073741824,
KS_MODE_ARM = 1,
KS_MODE_THUMB = 16,
KS_MODE_V8 = 64,
KS_MODE_MICRO = 16,
KS_MODE_MIPS3 = 32,
KS_MODE_MIPS32R6 = 64,
KS_MODE_MIPS32 = 4,
KS_MODE_MIPS64 = 8,
KS_MODE_16 = 2,
KS_MODE_32 = 4,
KS_MODE_64 = 8,
KS_MODE_PPC32 = 4,
KS_MODE_PPC64 = 8,
KS_MODE_QPX = 16,
KS_MODE_SPARC32 = 4,
KS_MODE_SPARC64 = 8,
KS_MODE_V9 = 16,
LITTLE_ENDIAN = 0,
BIG_ENDIAN = 1073741824,
ARM = 1,
THUMB = 16,
V8 = 64,
MICRO = 16,
MIPS3 = 32,
MIPS32R6 = 64,
MIPS32 = 4,
MIPS64 = 8,
X16 = 2,
X32 = 4,
X64 = 8,
PPC32 = 4,
PPC64 = 8,
QPX = 16,
SPARC32 = 4,
SPARC64 = 8,
V9 = 16,
}

public enum KeystoneError : short
Expand Down Expand Up @@ -86,22 +86,22 @@ public enum KeystoneError : short
KS_ERR_ASM_FRAGMENT_INVALID = 163,
KS_ERR_ASM_INVALIDOPERAND = 512,
KS_ERR_ASM_MISSINGFEATURE = 513,
KS_ERR_ASM_MNEMONICFAIL = 514,
KS_ERR_ASM_MNEMONICFAIL = 514
}

public enum KeystoneOptionType : short
public enum OptionType : int
{
KS_OPT_SYNTAX = 1,
KS_OPT_SYM_RESOLVER = 2,
SYNTAX = 1,
SYM_RESOLVER = 2,
}

public enum KeystoneOptionValue : short
public enum OptionValue : short
{
KS_OPT_SYNTAX_INTEL = 1,
KS_OPT_SYNTAX_ATT = 2,
KS_OPT_SYNTAX_NASM = 4,
KS_OPT_SYNTAX_MASM = 8,
KS_OPT_SYNTAX_GAS = 16,
KS_OPT_SYNTAX_RADIX16 = 32,
SYNTAX_INTEL = 1,
SYNTAX_ATT = 2,
SYNTAX_NASM = 4,
SYNTAX_MASM = 8,
SYNTAX_GAS = 16,
SYNTAX_RADIX16 = 32
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [mipsConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum MipsError : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [ppcConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum PpcError : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [sparcConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum SparcError : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [systemzConstants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum SystemzError : short
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// For Keystone Engine. AUTO-GENERATED FILE, DO NOT EDIT [x86Constants.cs]
namespace KeystoneNET
namespace Keystone
{
public enum X86Error : short
{
Expand Down
33 changes: 33 additions & 0 deletions bindings/csharp/Keystone.Net/EncodedData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Keystone
{
/// <summary>
/// Defines an encoded instruction or group of instructions.
/// </summary>
public sealed class EncodedData
{
/// <summary>
/// Constructs the encoded data.
/// </summary>
internal EncodedData(byte[] buffer, int statementCount, ulong address)
{
Buffer = buffer;
Address = address;
StatementCount = statementCount;
}

/// <summary>
/// Gets the address of the first instruction for this operation.
/// </summary>
public ulong Address { get; }

/// <summary>
/// Gets the result of an assembly operation.
/// </summary>
public byte[] Buffer { get; }

/// <summary>
/// Gets the number of statements found.
/// </summary>
public int StatementCount { get; }
}
}
Loading

0 comments on commit 0ab2b81

Please sign in to comment.