Implemented uc_strerror and minor refactoring
This commit is contained in:
@ -1,51 +0,0 @@
|
|||||||
(*
|
|
||||||
|
|
||||||
.NET bindings for the UnicornEngine Emulator Engine
|
|
||||||
|
|
||||||
Copyright(c) 2015 Antonio Parata
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU General Public License
|
|
||||||
version 2 as published by the Free Software Foundation.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
||||||
|
|
||||||
*)
|
|
||||||
|
|
||||||
namespace UnicornEngine.Const
|
|
||||||
|
|
||||||
open System
|
|
||||||
|
|
||||||
module UcError =
|
|
||||||
|
|
||||||
let toErrorDesc(err: Int32) =
|
|
||||||
match err with
|
|
||||||
| 0 -> "UC_ERR_OK"
|
|
||||||
| 1 -> "UC_ERR_NOMEM"
|
|
||||||
| 2 -> "UC_ERR_ARCH"
|
|
||||||
| 3 -> "UC_ERR_HANDLE"
|
|
||||||
| 4 -> "UC_ERR_MODE"
|
|
||||||
| 5 -> "UC_ERR_VERSION"
|
|
||||||
| 6 -> "UC_ERR_READ_INVALID"
|
|
||||||
| 7 -> "UC_ERR_WRITE_INVALID"
|
|
||||||
| 8 -> "UC_ERR_FETCH_INVALID"
|
|
||||||
| 9 -> "UC_ERR_CODE_INVALID"
|
|
||||||
| 10 -> "UC_ERR_HOOK"
|
|
||||||
| 11 -> "UC_ERR_INSN_INVALID"
|
|
||||||
| 12 -> "UC_ERR_MAP"
|
|
||||||
| 13 -> "UC_ERR_WRITE_PROT"
|
|
||||||
| 14 -> "UC_ERR_READ_PROT"
|
|
||||||
| 15 -> "UC_ERR_FETCH_PROT"
|
|
||||||
| 16 -> "UC_ERR_ARG"
|
|
||||||
| 17 -> "UC_ERR_READ_UNALIGNED"
|
|
||||||
| 18 -> "UC_ERR_WRITE_UNALIGNED"
|
|
||||||
| 19 -> "UC_ERR_FETCH_UNALIGNED"
|
|
||||||
| _ -> String.Empty
|
|
||||||
|
|
@ -64,6 +64,9 @@ module NativeUnicornEngine =
|
|||||||
[<DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)>]
|
[<DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)>]
|
||||||
extern Int32 uc_errno(UIntPtr eng)
|
extern Int32 uc_errno(UIntPtr eng)
|
||||||
|
|
||||||
|
[<DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)>]
|
||||||
|
extern IntPtr uc_strerror(Int32 err)
|
||||||
|
|
||||||
[<DllImport("unicorn", CallingConvention = CallingConvention.Cdecl, EntryPoint = "uc_hook_add")>]
|
[<DllImport("unicorn", CallingConvention = CallingConvention.Cdecl, EntryPoint = "uc_hook_add")>]
|
||||||
extern Int32 uc_hook_add_noarg(UIntPtr eng, UIntPtr hh, Int32 callbackType, UIntPtr callback, IntPtr userData)
|
extern Int32 uc_hook_add_noarg(UIntPtr eng, UIntPtr hh, Int32 callbackType, UIntPtr callback, IntPtr userData)
|
||||||
|
|
||||||
@ -86,6 +89,7 @@ module NativeUnicornEngine =
|
|||||||
let mutable emu_stop = fun(eng) -> Imported.uc_emu_stop(eng)
|
let mutable emu_stop = fun(eng) -> Imported.uc_emu_stop(eng)
|
||||||
let mutable arch_supported = fun(arch) -> Imported.uc_arch_supported(arch)
|
let mutable arch_supported = fun(arch) -> Imported.uc_arch_supported(arch)
|
||||||
let mutable errno = fun(eng) -> Imported.uc_errno(eng)
|
let mutable errno = fun(eng) -> Imported.uc_errno(eng)
|
||||||
|
let mutable strerror = fun(err) -> Imported.uc_strerror(err)
|
||||||
let mutable hook_add_noarg = fun(eng, hh, callbackType, callback, userData) -> Imported.uc_hook_add_noarg(eng, hh, callbackType, callback, userData)
|
let mutable hook_add_noarg = fun(eng, hh, callbackType, callback, userData) -> Imported.uc_hook_add_noarg(eng, hh, callbackType, callback, userData)
|
||||||
let mutable hook_add_arg0 = fun(eng, hh, callbackType, callback, userData, arg0) -> Imported.uc_hook_add_arg0(eng, hh, callbackType, callback, userData, arg0)
|
let mutable hook_add_arg0 = fun(eng, hh, callbackType, callback, userData, arg0) -> Imported.uc_hook_add_arg0(eng, hh, callbackType, callback, userData, arg0)
|
||||||
let mutable hook_add_arg0_arg1 = fun(eng, hh, callbackType, callback, userData, arg0, arg1) -> Imported.uc_hook_add_arg0_arg1(eng, hh, callbackType, callback, userData, arg0, arg1)
|
let mutable hook_add_arg0_arg1 = fun(eng, hh, callbackType, callback, userData, arg0, arg1) -> Imported.uc_hook_add_arg0_arg1(eng, hh, callbackType, callback, userData, arg0, arg1)
|
@ -55,7 +55,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
|||||||
let mutable _eng = [|UIntPtr.Zero|]
|
let mutable _eng = [|UIntPtr.Zero|]
|
||||||
|
|
||||||
let checkResult(errCode: Int32, errMsg: String) =
|
let checkResult(errCode: Int32, errMsg: String) =
|
||||||
if errCode <> Common.UC_ERR_OK then raise(ApplicationException(errMsg + ". Error: " + UcError.toErrorDesc(errCode)))
|
if errCode <> Common.UC_ERR_OK then raise(ApplicationException(String.Format("{0}. Error: {1}", errMsg, errCode)))
|
||||||
|
|
||||||
let getId =
|
let getId =
|
||||||
let counter = ref 0
|
let counter = ref 0
|
||||||
@ -103,9 +103,9 @@ and Unicorn(arch: Int32, mode: Int32) =
|
|||||||
member this.ErrNo() =
|
member this.ErrNo() =
|
||||||
NativeUnicornEngine.errno(_eng.[0])
|
NativeUnicornEngine.errno(_eng.[0])
|
||||||
|
|
||||||
member this.StrError() =
|
member this.StrError(errorNo: Int32) =
|
||||||
// TODO: to be implemented
|
let errorStringPointer = NativeUnicornEngine.strerror(errorNo)
|
||||||
raise(NotImplementedException())
|
Marshal.PtrToStringAnsi(errorStringPointer)
|
||||||
|
|
||||||
member this.AddCodeHook(callback: CodeHook, userData: Object, beginAdd: UInt64, endAddr: UInt64) =
|
member this.AddCodeHook(callback: CodeHook, userData: Object, beginAdd: UInt64, endAddr: UInt64) =
|
||||||
let trampoline(u: IntPtr) (addr: UInt64) (size: Int32) (user: IntPtr) =
|
let trampoline(u: IntPtr) (addr: UInt64) (size: Int32) (user: IntPtr) =
|
||||||
|
@ -53,7 +53,7 @@ namespace UnicornTests
|
|||||||
{
|
{
|
||||||
if (err != Common.UC_ERR_OK)
|
if (err != Common.UC_ERR_OK)
|
||||||
{
|
{
|
||||||
throw new ApplicationException("Operation failed, error: " + UcError.toErrorDesc(err));
|
throw new ApplicationException("Operation failed, error: " + err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +74,7 @@ namespace UnicornTests
|
|||||||
|
|
||||||
var eipBuffer = new Byte[4];
|
var eipBuffer = new Byte[4];
|
||||||
CheckError(u.RegRead(X86.UC_X86_REG_EIP, eipBuffer));
|
CheckError(u.RegRead(X86.UC_X86_REG_EIP, eipBuffer));
|
||||||
|
|
||||||
var effectiveSize = Math.Min(16, size);
|
var effectiveSize = Math.Min(16, size);
|
||||||
var tmp = new Byte[effectiveSize];
|
var tmp = new Byte[effectiveSize];
|
||||||
CheckError(u.MemRead(addr, tmp));
|
CheckError(u.MemRead(addr, tmp));
|
||||||
@ -114,7 +114,7 @@ namespace UnicornTests
|
|||||||
u.EmuStop();
|
u.EmuStop();
|
||||||
break;
|
break;
|
||||||
case 4: // sys_write
|
case 4: // sys_write
|
||||||
|
|
||||||
// ECX = buffer address
|
// ECX = buffer address
|
||||||
var ecxBuffer = new Byte[4];
|
var ecxBuffer = new Byte[4];
|
||||||
|
|
||||||
@ -134,8 +134,8 @@ namespace UnicornTests
|
|||||||
var content = Encoding.Default.GetString(buffer);
|
var content = Encoding.Default.GetString(buffer);
|
||||||
|
|
||||||
Console.WriteLine(
|
Console.WriteLine(
|
||||||
"Interrupt >>> 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
|
"Interrupt >>> 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
|
||||||
eip.ToString("X"),
|
eip.ToString("X"),
|
||||||
ecx.ToString("X"),
|
ecx.ToString("X"),
|
||||||
edx.ToString("X"),
|
edx.ToString("X"),
|
||||||
content);
|
content);
|
||||||
@ -143,12 +143,12 @@ namespace UnicornTests
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Main(String[] args)
|
static void Main(String[] args)
|
||||||
{
|
{
|
||||||
var u = new Unicorn(Common.UC_ARCH_X86, Common.UC_MODE_32);
|
var u = new Unicorn(Common.UC_ARCH_X86, Common.UC_MODE_32);
|
||||||
Console.WriteLine("Unicorn version: {0}", u.Version());
|
Console.WriteLine("Unicorn version: {0}", u.Version());
|
||||||
|
|
||||||
// map 2MB of memory for this emulation
|
// map 2MB of memory for this emulation
|
||||||
CheckError(u.MemMap(ADDRESS, new UIntPtr(2 * 1024 * 1024), Common.UC_PROT_ALL));
|
CheckError(u.MemMap(ADDRESS, new UIntPtr(2 * 1024 * 1024), Common.UC_PROT_ALL));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user