Skip to content
Merged
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
138 changes: 138 additions & 0 deletions Architecture/CustomArchitectureFlagLoweringCallbacks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Runtime.InteropServices;

namespace BinaryNinja
{
public abstract partial class CustomArchitecture
{
[UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate ulong GetFlagWriteLowLevelILCallback(
IntPtr context,
LowLevelILOperation operation,
ulong size,
uint flagWriteType,
uint flag,
IntPtr operands,
ulong operandCount,
IntPtr il);

[UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate ulong GetFlagConditionLowLevelILCallback(
IntPtr context,
LowLevelILFlagCondition condition,
uint semanticClass,
IntPtr il);

[UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.Cdecl)]
private delegate ulong GetSemanticFlagGroupLowLevelILCallback(
IntPtr context,
uint semanticGroup,
IntPtr il);

private void AddFlagLoweringCallbacks(ref BNCustomArchitecture callbacks)
{
callbacks.getFlagWriteLowLevelIL =
UnsafeUtils.PinCallback<GetFlagWriteLowLevelILCallback>(
this.GetFlagWriteLowLevelILAdapter);
callbacks.getFlagConditionLowLevelIL =
UnsafeUtils.PinCallback<GetFlagConditionLowLevelILCallback>(
this.GetFlagConditionLowLevelILAdapter);
callbacks.getSemanticFlagGroupLowLevelIL =
UnsafeUtils.PinCallback<GetSemanticFlagGroupLowLevelILCallback>(
this.GetSemanticFlagGroupLowLevelILAdapter);
}

private ulong GetFlagWriteLowLevelILAdapter(
IntPtr context,
LowLevelILOperation operation,
ulong size,
uint flagWriteType,
uint flag,
IntPtr operands,
ulong operandCount,
IntPtr ilHandle)
{
try
{
RegisterOrConstant[] managedOperands = UnsafeUtils.ReadStructArray<
BNRegisterOrConstant,
RegisterOrConstant>(
operands,
operandCount,
RegisterOrConstant.FromNative);

using (LowLevelILFunction il = this.CreateCallbackLowLevelIL(ilHandle))
{
return this.GetFlagWriteLowLevelIL(
operation,
size,
flagWriteType,
flag,
managedOperands,
il);
}
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in CustomArchitecture.GetFlagWriteLowLevelIL: {0}",
exception);

return 0;
}
}

private ulong GetFlagConditionLowLevelILAdapter(
IntPtr context,
LowLevelILFlagCondition condition,
uint semanticClass,
IntPtr ilHandle)
{
try
{
using (LowLevelILFunction il = this.CreateCallbackLowLevelIL(ilHandle))
{
return this.GetFlagConditionLowLevelIL(condition, semanticClass, il);
}
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in CustomArchitecture.GetFlagConditionLowLevelIL: {0}",
exception);

return 0;
}
}

private ulong GetSemanticFlagGroupLowLevelILAdapter(
IntPtr context,
uint semanticGroup,
IntPtr ilHandle)
{
try
{
using (LowLevelILFunction il = this.CreateCallbackLowLevelIL(ilHandle))
{
return this.GetSemanticFlagGroupLowLevelIL(semanticGroup, il);
}
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in CustomArchitecture.GetSemanticFlagGroupLowLevelIL: {0}",
exception);

return 0;
}
}

private LowLevelILFunction CreateCallbackLowLevelIL(IntPtr handle)
{
return LowLevelILFunction.MustNewFromHandle(
handle,
false,
this.registeredArchitecture);
}
}
}
5 changes: 1 addition & 4 deletions Architecture/CustomArchitectureInstructionCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ private bool GetInstructionLowLevelILAdapter(
Marshal.Copy(data, bytes, 0, bytes.Length);
}

using (LowLevelILFunction il = LowLevelILFunction.MustNewFromHandle(
ilHandle,
false,
this.registeredArchitecture))
using (LowLevelILFunction il = this.CreateCallbackLowLevelIL(ilHandle))
{
ulong? decodedLength = this.GetInstructionLowLevelIL(bytes, address, il);
if (null == decodedLength
Expand Down
1 change: 1 addition & 0 deletions Architecture/CustomArchitectureRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Architecture Register(string name)
this.AddInstructionCallbacks(ref callbacks);
this.AddAssemblyAndPatchCallbacks(ref callbacks);
this.AddIntrinsicCallbacks(ref callbacks);
this.AddFlagLoweringCallbacks(ref callbacks);

using (ScopedAllocator allocator = new ScopedAllocator())
{
Expand Down
Loading