Skip to content
Merged
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
56 changes: 56 additions & 0 deletions Architecture/CustomArchitectureInstructionCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ private delegate bool GetInstructionTextCallback(
[UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate void FreeInstructionTextCallback(IntPtr tokens, ulong count);

[UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I1)]
private delegate bool GetInstructionLowLevelILCallback(
IntPtr context,
IntPtr data,
ulong address,
ref ulong length,
IntPtr il);

private void AddInstructionCallbacks(ref BNCustomArchitecture callbacks)
{
callbacks.getInstructionInfo = UnsafeUtils.PinCallback<GetInstructionInfoCallback>(
Expand All @@ -36,6 +45,9 @@ private void AddInstructionCallbacks(ref BNCustomArchitecture callbacks)
this.GetInstructionTextAdapter);
callbacks.freeInstructionText = UnsafeUtils.PinCallback<FreeInstructionTextCallback>(
this.FreeInstructionTextAdapter);
callbacks.getInstructionLowLevelIL =
UnsafeUtils.PinCallback<GetInstructionLowLevelILCallback>(
this.GetInstructionLowLevelILAdapter);
}

private bool GetInstructionTextAdapter(
Expand Down Expand Up @@ -84,6 +96,50 @@ private bool GetInstructionTextAdapter(
}
}

private bool GetInstructionLowLevelILAdapter(
IntPtr context,
IntPtr data,
ulong address,
ref ulong length,
IntPtr ilHandle)
{
try
{
ulong maximumLength = length;
byte[] bytes = new byte[checked((int)maximumLength)];
if (0 != bytes.Length)
{
Marshal.Copy(data, bytes, 0, bytes.Length);
}

using (LowLevelILFunction il = LowLevelILFunction.MustNewFromHandle(
ilHandle,
false,
this.registeredArchitecture))
{
ulong? decodedLength = this.GetInstructionLowLevelIL(bytes, address, il);
if (null == decodedLength
|| 0 == decodedLength.Value
|| maximumLength < decodedLength.Value)
{
return false;
}

length = decodedLength.Value;

return true;
}
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in CustomArchitecture.GetInstructionLowLevelIL: {0}",
exception);

return false;
}
}

private IntPtr AllocateInstructionText(InstructionTextToken[] tokens)
{
BNInstructionTextToken[] nativeTokens = new BNInstructionTextToken[tokens.Length];
Expand Down
Loading