diff --git a/Architecture/CustomArchitectureInstructionCallbacks.cs b/Architecture/CustomArchitectureInstructionCallbacks.cs index b3e2fb8..5608a34 100644 --- a/Architecture/CustomArchitectureInstructionCallbacks.cs +++ b/Architecture/CustomArchitectureInstructionCallbacks.cs @@ -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( @@ -36,6 +45,9 @@ private void AddInstructionCallbacks(ref BNCustomArchitecture callbacks) this.GetInstructionTextAdapter); callbacks.freeInstructionText = UnsafeUtils.PinCallback( this.FreeInstructionTextAdapter); + callbacks.getInstructionLowLevelIL = + UnsafeUtils.PinCallback( + this.GetInstructionLowLevelILAdapter); } private bool GetInstructionTextAdapter( @@ -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];