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

Support ReadOnlySpan<byte> #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
69 changes: 28 additions & 41 deletions Gee.External.Capstone/CapstoneDisassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,13 +767,10 @@ NativeDisassembleMode CreateNativeDisassembleMode(CapstoneDisassembler<TDisassem
/// <returns>
/// An array of disassembled instructions.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if the binary code array is a null reference.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the disassembler is disposed.
/// </exception>
public TInstruction[] Disassemble(byte[] binaryCode) {
public TInstruction[] Disassemble(ReadOnlySpan<byte> binaryCode) {
// ...
//
// Throws an exception if the operation fails.
Expand All @@ -793,13 +790,10 @@ public TInstruction[] Disassemble(byte[] binaryCode) {
/// <returns>
/// An array of disassembled instructions.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if the binary code array is a null reference.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the disassembler is disposed.
/// </exception>
public TInstruction[] Disassemble(byte[] binaryCode, long startingAddress) {
public TInstruction[] Disassemble(ReadOnlySpan<byte> binaryCode, long startingAddress) {
// ...
//
// Throws an exception if the operation fails.
Expand All @@ -822,13 +816,10 @@ public TInstruction[] Disassemble(byte[] binaryCode, long startingAddress) {
/// <returns>
/// An array of disassembled instructions.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if the binary code array is a null reference.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the disassembler is disposed.
/// </exception>
public TInstruction[] Disassemble(byte[] binaryCode, long startingAddress, int count) {
public TInstruction[] Disassemble(ReadOnlySpan<byte> binaryCode, long startingAddress, int count) {
// ...
//
// Throws an exception if the operation fails.
Expand Down Expand Up @@ -954,13 +945,10 @@ public string GetRegisterName(TRegisterId registerId) {
/// <returns>
/// A deferred collection of disassembled instructions.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if the binary code array is a null reference.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the disassembler is disposed.
/// </exception>
public IEnumerable<TInstruction> Iterate(byte[] binaryCode) {
public IEnumerable<TInstruction> Iterate(ReadOnlySpan<byte> binaryCode) {
// ...
//
// Throws an exception if the operation fails.
Expand All @@ -979,15 +967,10 @@ public IEnumerable<TInstruction> Iterate(byte[] binaryCode) {
/// <returns>
/// A deferred collection of disassembled instructions.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// Thrown if the binary code array is a null reference.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the disassembler is disposed.
/// </exception>
public IEnumerable<TInstruction> Iterate(byte[] binaryCode, long startingAddress) {
CapstoneDisassembler.ThrowIfValueIsNullReference(nameof(binaryCode), binaryCode);

public IEnumerable<TInstruction> Iterate(ReadOnlySpan<byte> binaryCode, long startingAddress) {
var binaryCodeOffset = 0;

// ...
Expand Down Expand Up @@ -1021,7 +1004,7 @@ public IEnumerable<TInstruction> Iterate(byte[] binaryCode, long startingAddress
//
// To make this work though, we MUST define a local variable for the delegate!
if (this._skipDataCallback != null) {
callback = OnNativeSkipDataCallback;
callback = CreateCallback(binaryCode.ToArray());
}

// ...
Expand Down Expand Up @@ -1067,24 +1050,28 @@ public IEnumerable<TInstruction> Iterate(byte[] binaryCode, long startingAddress
}
}

// <summary>
// Native Skip Data Mode Callback.
// </summary>
IntPtr OnNativeSkipDataCallback(IntPtr cPBinaryCode, IntPtr cBinaryCodeSize, IntPtr cDataOffset, IntPtr pState) {
// ...
//
// Normally, a closure enclosing over a variable modified from a loop, such as this method, is a
// problem because the value of the variable is resolved at the time the closure is invoked and not
// when the variable is captured. This can lead to unexpected behavior if the closure is invoked
// outside the loop since the value of the captured variable will always be resolved to the last value
// it was set to inside the loop.
//
// However, because this closure will always be invoked from inside a disassemble loop, and never from
// outside of one, the variable value resolution behavior is exactly what we are looking for. We want
// the Capstone API to invoke this callback with an updated value for the captured variable every
// time!
var cBytesToSkip = this.SkipDataCallback(binaryCode, binaryCodeOffset);
return new IntPtr(cBytesToSkip);
NativeCapstone.SkipDataCallback CreateCallback(byte[] binaryCode) {
return OnNativeSkipDataCallback;

// <summary>
// Native Skip Data Mode Callback.
// </summary>
IntPtr OnNativeSkipDataCallback(IntPtr cPBinaryCode, IntPtr cBinaryCodeSize, IntPtr cDataOffset, IntPtr pState) {
// ...
//
// Normally, a closure enclosing over a variable modified from a loop, such as this method, is a
// problem because the value of the variable is resolved at the time the closure is invoked and not
// when the variable is captured. This can lead to unexpected behavior if the closure is invoked
// outside the loop since the value of the captured variable will always be resolved to the last value
// it was set to inside the loop.
//
// However, because this closure will always be invoked from inside a disassemble loop, and never from
// outside of one, the variable value resolution behavior is exactly what we are looking for. We want
// the Capstone API to invoke this callback with an updated value for the captured variable every
// time!
var cBytesToSkip = this.SkipDataCallback(binaryCode, binaryCodeOffset);
return new IntPtr(cBytesToSkip);
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions Gee.External.Capstone/Gee.External.Capstone.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
<ItemGroup>
<Content CopyToOutputDirectory="PreserveNewest" Include="runtimes/*/native/*" Pack="true" PackagePath="runtimes" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Memory" Version="4.5.5" />
</ItemGroup>
</Project>
12 changes: 3 additions & 9 deletions Gee.External.Capstone/NativeCapstone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,14 +472,13 @@ internal static Version GetVersion() {
/// <exception cref="System.ObjectDisposedException">
/// Thrown if the disassembler handle is disposed, or if the instruction handle is disposed.
/// </exception>
internal static bool Iterate(NativeDisassemblerHandle hDisassembler, byte[] binaryCode, ref int binaryCodeOffset, ref long address, NativeInstructionHandle hInstruction) {
var hBinaryCode = GCHandle.Alloc(binaryCode, GCHandleType.Pinned);
try {
internal static unsafe bool Iterate(NativeDisassemblerHandle hDisassembler, ReadOnlySpan<byte> binaryCode, ref int binaryCodeOffset, ref long address, NativeInstructionHandle hInstruction) {
fixed (byte* fixedPointer = binaryCode) {
// ...
//
// First, we increment the pointer to the binary code buffer to the point to the address of the
// instruction we want to disassemble.
var pBinaryCode = hBinaryCode.AddrOfPinnedObject() + binaryCodeOffset;
var pBinaryCode = (IntPtr)fixedPointer + binaryCodeOffset;

// ...
//
Expand Down Expand Up @@ -508,11 +507,6 @@ internal static bool Iterate(NativeDisassemblerHandle hDisassembler, byte[] bina

return isDisassembled;
}
finally {
if (hBinaryCode.IsAllocated) {
hBinaryCode.Free();
}
}
}

/// <summary>
Expand Down
Loading