unicorn/bindings/dotnet/UnicornManaged/Unicorn.fs

343 lines
16 KiB
Forth
Raw Normal View History

2016-01-04 10:30:11 +00:00
namespace UnicornManaged
2015-10-14 08:55:50 +00:00
open System
open System.Threading
open System.Collections.Generic
open System.Runtime.InteropServices
2016-01-04 15:59:05 +00:00
open System.Linq
2016-01-04 10:30:11 +00:00
open UnicornManaged.Const
open UnicornManaged.Binding
2015-10-14 08:55:50 +00:00
// exported hooks
2016-01-04 10:30:11 +00:00
type CodeHook = delegate of Unicorn * Int64 * Int32 * Object -> unit
and BlockHook = delegate of Unicorn * Int64 * Int32 * Object -> unit
2015-10-14 08:55:50 +00:00
and InterruptHook = delegate of Unicorn * Int32 * Object -> unit
2016-01-04 10:30:11 +00:00
and MemReadHook = delegate of Unicorn * Int64 * Int32 * Object -> unit
and MemWriteHook = delegate of Unicorn * Int64 * Int32 * Int64 * Object -> unit
2016-01-04 15:59:05 +00:00
and EventMemHook = delegate of Unicorn * Int32 * Int64 * Int32 * Int64 * Object -> Boolean
and InHook = delegate of Unicorn * Int32 * Int32 * Object -> Int32
2015-10-14 08:55:50 +00:00
and OutHook = delegate of Unicorn * Int32 * Int32 * Int32 * Object -> unit
and SyscallHook = delegate of Unicorn * Object -> unit
// the managed unicorn engine
2016-01-04 10:30:11 +00:00
and Unicorn(arch: Int32, mode: Int32, binding: IBinding) =
2015-10-14 08:55:50 +00:00
// hook callback list
2016-01-04 15:59:05 +00:00
let _codeHooks = new List<(CodeHook * Object)>()
let _blockHooks = new List<(BlockHook * Object)>()
let _interruptHooks = new List<(InterruptHook * Object)>()
let _memReadHooks = new List<(MemReadHook * Object)>()
let _memWriteHooks = new List<(MemWriteHook * Object)>()
let _memEventHooks = new Dictionary<Int32, List<(EventMemHook * Object)>>()
let _inHooks = new List<(InHook * Object)>()
let _outHooks = new List<(OutHook * Object)>()
let _syscallHooks = new List<(SyscallHook * Object)>()
let _disposablePointers = new List<nativeint>()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
let _eventMemMap =
[
(UC_HOOK_MEM_READ_UNMAPPED, UC_MEM_READ_UNMAPPED)
(UC_HOOK_MEM_WRITE_UNMAPPED, UC_MEM_WRITE_UNMAPPED)
(UC_HOOK_MEM_FETCH_UNMAPPED, UC_MEM_FETCH_UNMAPPED)
(UC_HOOK_MEM_READ_PROT, UC_MEM_READ_PROT)
(UC_HOOK_MEM_WRITE_PROT, UC_MEM_WRITE_PROT)
(UC_HOOK_MEM_FETCH_PROT, UC_MEM_FETCH_PROT)
] |> dict
2015-10-14 08:55:50 +00:00
let mutable _eng = [|UIntPtr.Zero|]
2016-01-04 10:30:11 +00:00
2015-10-14 08:55:50 +00:00
let checkResult(errCode: Int32, errMsg: String) =
if errCode <> Common.UC_ERR_OK then raise(ApplicationException(String.Format("{0}. Error: {1}", errMsg, errCode)))
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
let hookDel(callbacks: List<'a * Object>) (callback: 'a)=
2015-10-14 08:55:50 +00:00
// TODO: invoke the native function in order to not call the trampoline anymore
2016-01-04 15:59:05 +00:00
callbacks
|> Seq.tryFind(fun item -> match item with | (c, _) -> c = callback)
2015-10-14 08:55:50 +00:00
|> (fun k -> if k.IsSome then callbacks.Remove(k.Value) |> ignore)
2016-01-04 10:30:11 +00:00
let allocate(size: Int32) =
let mem = Marshal.AllocHGlobal(size)
_disposablePointers.Add(mem)
mem.ToPointer()
2016-01-04 10:30:11 +00:00
do
2016-01-04 15:59:05 +00:00
// initialize event list
_eventMemMap
|> Seq.map(fun kv -> kv.Key)
|> Seq.iter (fun eventType -> _memEventHooks.Add(eventType, new List<EventMemHook * Object>()))
// init engine
_eng <- [|new UIntPtr(allocate(IntPtr.Size))|]
2016-01-04 10:30:11 +00:00
let err = binding.UcOpen(uint32 arch, uint32 mode, _eng)
2015-10-14 08:55:50 +00:00
checkResult(err, "Unable to open the Unicorn Engine")
2016-01-04 10:30:11 +00:00
new(arch, mode) = new Unicorn(arch, mode, BindingFactory.getDefault())
member private this.CheckResult(errorCode: Int32) =
// return the exception instead of raising it in order to have a more meaningful stack trace
if errorCode <> Common.UC_ERR_OK then
let errorMessage = this.StrError(errorCode)
Some <| UnicornEngineException(errorCode, errorMessage)
else None
2016-01-04 10:30:11 +00:00
member this.MemMap(address: Int64, size: Int64, perm: Int32) =
let size = new UIntPtr(uint64 size)
match binding.MemMap(_eng.[0], uint64 address, size, uint32 perm) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 10:30:11 +00:00
member this.MemMapPtr(address: Int64, size: Int64, perm: Int32, ptr: IntPtr) =
let size = new UIntPtr(uint64 size)
let ptr = new UIntPtr(ptr.ToPointer())
match binding.MemMapPtr(_eng.[0], uint64 address, size, uint32 perm, ptr) |> this.CheckResult with
| Some e -> raise e | None -> ()
member this.MemUnmap(address: Int64, size: Int64) =
let size = new UIntPtr(uint64 size)
match binding.MemUnmap(_eng.[0], uint64 address, size) |> this.CheckResult with
| Some e -> raise e | None -> ()
member this.MemProtect(address: Int64, size: Int64, ?perm: Int32) =
let size = new UIntPtr(uint64 size)
let perm = defaultArg perm Common.UC_PROT_ALL
match binding.MemProtect(_eng.[0], uint64 address, size, uint32 perm) |> this.CheckResult with
| Some e -> raise e | None -> ()
member this.MemWrite(address: Int64, value: Byte array) =
match binding.MemWrite(_eng.[0], uint64 address, value, new UIntPtr(uint32 value.Length)) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 10:30:11 +00:00
member this.MemRead(address: Int64, memValue: Byte array) =
match binding.MemRead(_eng.[0], uint64 address, memValue, new UIntPtr(uint32 memValue.Length)) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
member this.RegWrite(regId: Int32, value: Byte array) =
2016-01-04 10:30:11 +00:00
match binding.RegWrite(_eng.[0], regId, value) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
member this.RegWrite(regId: Int32, value: Int64) =
this.RegWrite(regId, int64ToBytes value)
2015-10-14 08:55:50 +00:00
member this.RegRead(regId: Int32, regValue: Byte array) =
2016-01-04 10:30:11 +00:00
match binding.RegRead(_eng.[0], regId, regValue) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
member this.RegRead(regId: Int32) =
let buffer = Array.zeroCreate<Byte> 8
this.RegRead(regId, buffer)
bytesToInt64 buffer
2016-01-04 10:30:11 +00:00
member this.EmuStart(beginAddr: Int64, untilAddr: Int64, timeout: Int64, count: Int64) =
match binding.EmuStart(_eng.[0], uint64 beginAddr, uint64 untilAddr, uint64 timeout, uint64 count) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
member this.EmuStop() =
2016-01-04 10:30:11 +00:00
match binding.EmuStop(_eng.[0]) |> this.CheckResult with
| Some e -> raise e | None -> ()
2016-01-04 10:30:11 +00:00
2015-10-14 08:55:50 +00:00
member this.Close() =
2016-01-04 10:30:11 +00:00
match binding.Close(_eng.[0]) |> this.CheckResult with
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
member this.ArchSupported(arch: Int32) =
2016-01-04 10:30:11 +00:00
binding.ArchSupported(arch)
2015-10-14 08:55:50 +00:00
member this.ErrNo() =
2016-01-04 10:30:11 +00:00
binding.Errono(_eng.[0])
2015-10-14 08:55:50 +00:00
member this.StrError(errorNo: Int32) =
2016-01-04 10:30:11 +00:00
let errorStringPointer = binding.Strerror(errorNo)
Marshal.PtrToStringAnsi(errorStringPointer)
2015-10-14 08:55:50 +00:00
2016-01-04 10:30:11 +00:00
member this.AddCodeHook(callback: CodeHook, userData: Object, beginAddr: Int64, endAddr: Int64) =
let trampoline(u: IntPtr) (addr: Int64) (size: Int32) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_codeHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, addr, size, userData))
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
if _codeHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new CodeHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddNoarg(_eng.[0], hh, Common.UC_HOOK_CODE, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 beginAddr, uint64 endAddr) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_codeHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
member this.AddCodeHook(callback: CodeHook, beginAddr: Int64, endAddr: Int64) =
2016-01-04 10:30:11 +00:00
this.AddCodeHook(callback, null, beginAddr, endAddr)
2015-10-14 08:55:50 +00:00
member this.HookDel(callback: CodeHook) =
hookDel _codeHooks callback
2016-01-04 10:30:11 +00:00
member this.AddBlockHook(callback: BlockHook, userData: Object, beginAddr: Int64, endAddr: Int64) =
let trampoline(u: IntPtr) (addr: Int64) (size: Int32) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_blockHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, addr, size, userData))
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
if _blockHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new BlockHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddNoarg(_eng.[0], hh, Common.UC_HOOK_BLOCK, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 beginAddr, uint64 endAddr) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_blockHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
member this.HookDel(callback: BlockHook) =
hookDel _blockHooks callback
member this.AddInterruptHook(callback: InterruptHook, userData: Object, hookBegin: UInt64, hookEnd : UInt64) =
2015-10-14 08:55:50 +00:00
let trampoline(u: IntPtr) (intNumber: Int32) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_interruptHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, intNumber, userData))
if _interruptHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new InterruptHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddNoarg(_eng.[0], hh, Common.UC_HOOK_INTR, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, hookBegin, hookEnd) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_interruptHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
2016-01-04 10:30:11 +00:00
member this.AddInterruptHook(callback: InterruptHook) =
this.AddInterruptHook(callback, null, uint64 1, uint64 0)
2016-01-04 10:30:11 +00:00
2015-10-14 08:55:50 +00:00
member this.HookDel(callback: InterruptHook) =
hookDel _interruptHooks callback
2016-01-04 10:30:11 +00:00
member this.AddMemReadHook(callback: MemReadHook, userData: Object, beginAddr: Int64, endAddr: Int64) =
let trampoline(u: IntPtr) (addr: Int64) (size: Int32) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_memReadHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, addr, size, userData))
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
if _memReadHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new MemReadHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddNoarg(_eng.[0], hh, Common.UC_HOOK_MEM_READ, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 beginAddr, uint64 endAddr) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_memReadHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
member this.HookDel(callback: MemReadHook) =
hookDel _memReadHooks callback
2016-01-04 10:30:11 +00:00
member this.AddMemWriteHook(callback: MemWriteHook, userData: Object, beginAddr: Int64, endAddr: Int64) =
let trampoline(u: IntPtr) (addr: Int64) (size: Int32) (value: Int64) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_memWriteHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, addr, size, value, userData))
if _memWriteHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new MemWriteHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddNoarg(_eng.[0], hh, Common.UC_HOOK_MEM_WRITE, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 beginAddr, uint64 endAddr) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_memWriteHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
member this.HookDel(callback: MemWriteHook) =
hookDel _memWriteHooks callback
member this.AddEventMemHook(callback: EventMemHook, eventType: Int32, userData: Object) =
2016-01-04 15:59:05 +00:00
let trampoline(u: IntPtr) (eventType: Int32) (addr: Int64) (size: Int32) (value: Int64) (user: IntPtr) =
_memEventHooks.Keys
|> Seq.filter(fun eventFlag -> (eventType &&& eventFlag) <> 0)
|> Seq.map(fun eventflag -> _memEventHooks.[eventflag])
|> Seq.concat
|> Seq.map(fun (callback, userData) -> callback.Invoke(this, eventType, addr, size, value, userData))
|> Seq.forall id
// register the event if not already done
_memEventHooks.Keys
|> Seq.filter(fun eventFlag -> (eventType &&& eventFlag) <> 0)
|> Seq.filter(fun eventFlag -> _memEventHooks.[eventFlag] |> Seq.isEmpty)
|> Seq.iter(fun eventFlag ->
2015-10-14 08:55:50 +00:00
let funcPointer = Marshal.GetFunctionPointerForDelegate(new EventMemHookInternal(trampoline))
2016-01-04 15:59:05 +00:00
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddNoarg(_eng.[0], hh, eventFlag, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 0, uint64 0) |> this.CheckResult with
| Some e -> raise e | None -> ()
2016-01-04 15:59:05 +00:00
)
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
// register the callbacks
_memEventHooks.Keys
|> Seq.filter(fun eventFlag -> (eventType &&& eventFlag) <> 0)
|> Seq.iter(fun eventFlag -> _memEventHooks.[eventFlag].Add((callback, userData)))
member this.AddEventMemHook(callback: EventMemHook, eventType: Int32) =
this.AddEventMemHook(callback, eventType, null)
2015-10-14 08:55:50 +00:00
member this.HookDel(callback: EventMemHook) =
2016-01-04 15:59:05 +00:00
let callbacks = (_memEventHooks.Values |> Seq.concat).ToList()
hookDel callbacks callback
2015-10-14 08:55:50 +00:00
member this.AddInHook(callback: InHook, userData: Object) =
let trampoline(u: IntPtr) (port: Int32) (size: Int32) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_inHooks
|> Seq.map(fun (callback, userData) -> callback.Invoke(this, port, size, userData))
|> Seq.last
if _inHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new InHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddArg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 0, uint64 0, X86.UC_X86_INS_IN) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_inHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
member this.AddInHook(callback: InHook) =
this.AddInHook(callback, null)
2015-10-14 08:55:50 +00:00
member this.AddOutHook(callback: OutHook, userData: Object) =
let trampoline(u: IntPtr) (port: Int32) (size: Int32) (value: Int32) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_outHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, port, size, value, userData))
if _outHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new OutHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddArg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 0, uint64 0, X86.UC_X86_INS_OUT) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_outHooks.Add(callback, userData)
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
member this.AddOutHook(callback: OutHook) =
this.AddOutHook(callback, null)
2015-10-14 08:55:50 +00:00
member this.AddSyscallHook(callback: SyscallHook, userData: Object) =
let trampoline(u: IntPtr) (user: IntPtr) =
2016-01-04 15:59:05 +00:00
_syscallHooks
|> Seq.iter(fun (callback, userData) -> callback.Invoke(this, userData))
if _syscallHooks |> Seq.isEmpty then
let funcPointer = Marshal.GetFunctionPointerForDelegate(new SyscallHookInternal(trampoline))
let hh = new UIntPtr(allocate(IntPtr.Size))
match binding.HookAddArg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), IntPtr.Zero, uint64 0, uint64 0, X86.UC_X86_INS_SYSCALL) |> this.CheckResult with
2016-01-04 15:59:05 +00:00
| Some e -> raise e | None -> ()
2015-10-14 08:55:50 +00:00
2016-01-04 15:59:05 +00:00
_syscallHooks.Add(callback, userData)
2016-01-04 10:30:11 +00:00
member this.AddSyscallHook(callback: SyscallHook) =
this.AddSyscallHook(callback, null)
2015-10-14 08:55:50 +00:00
member this.Version() =
let (major, minor) = (new UIntPtr(), new UIntPtr())
2016-01-04 10:30:11 +00:00
let combined = binding.Version(major, minor)
2015-10-14 08:55:50 +00:00
(major.ToUInt32(), minor.ToUInt32(), combined)
abstract Dispose : Boolean -> unit
default this.Dispose(disposing: Boolean) =
if (disposing) then
// free managed resources, this is the default dispose implementation pattern
()
_disposablePointers
|> Seq.filter(fun pointer -> pointer <> IntPtr.Zero)
|> Seq.iter Marshal.FreeHGlobal
2016-01-04 10:30:11 +00:00
_disposablePointers.Clear()
member this.Dispose() =
this.Dispose(true)
GC.SuppressFinalize(this)
override this.Finalize() =
this.Dispose(false)
interface IDisposable with
member this.Dispose() =
2016-01-04 10:30:11 +00:00
this.Dispose()