Implemented uc_strerror and minor refactoring

This commit is contained in:
Antonio Parata 2015-10-15 11:24:20 +02:00
parent 03b2fbfbcb
commit c0ba09e0b8
4 changed files with 15 additions and 62 deletions

View file

@ -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

View file

@ -64,6 +64,9 @@ module NativeUnicornEngine =
[<DllImport("unicorn", CallingConvention = CallingConvention.Cdecl)>]
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")>]
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 arch_supported = fun(arch) -> Imported.uc_arch_supported(arch)
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_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)

View file

@ -55,7 +55,7 @@ and Unicorn(arch: Int32, mode: Int32) =
let mutable _eng = [|UIntPtr.Zero|]
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 counter = ref 0
@ -103,9 +103,9 @@ and Unicorn(arch: Int32, mode: Int32) =
member this.ErrNo() =
NativeUnicornEngine.errno(_eng.[0])
member this.StrError() =
// TODO: to be implemented
raise(NotImplementedException())
member this.StrError(errorNo: Int32) =
let errorStringPointer = NativeUnicornEngine.strerror(errorNo)
Marshal.PtrToStringAnsi(errorStringPointer)
member this.AddCodeHook(callback: CodeHook, userData: Object, beginAdd: UInt64, endAddr: UInt64) =
let trampoline(u: IntPtr) (addr: UInt64) (size: Int32) (user: IntPtr) =

View file

@ -53,7 +53,7 @@ namespace UnicornTests
{
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];
CheckError(u.RegRead(X86.UC_X86_REG_EIP, eipBuffer));
var effectiveSize = Math.Min(16, size);
var tmp = new Byte[effectiveSize];
CheckError(u.MemRead(addr, tmp));
@ -114,7 +114,7 @@ namespace UnicornTests
u.EmuStop();
break;
case 4: // sys_write
// ECX = buffer address
var ecxBuffer = new Byte[4];
@ -134,8 +134,8 @@ namespace UnicornTests
var content = Encoding.Default.GetString(buffer);
Console.WriteLine(
"Interrupt >>> 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
eip.ToString("X"),
"Interrupt >>> 0x{0}: num {1}, SYS_WRITE. buffer = 0x{2}, size = , content = '{3}'",
eip.ToString("X"),
ecx.ToString("X"),
edx.ToString("X"),
content);
@ -143,12 +143,12 @@ namespace UnicornTests
break;
}
}
static void Main(String[] args)
{
var u = new Unicorn(Common.UC_ARCH_X86, Common.UC_MODE_32);
Console.WriteLine("Unicorn version: {0}", u.Version());
// map 2MB of memory for this emulation
CheckError(u.MemMap(ADDRESS, new UIntPtr(2 * 1024 * 1024), Common.UC_PROT_ALL));