mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-03-30 00:57:03 +00:00
Implemented IDisposable in order to disposed allocated unmanaged memory
This commit is contained in:
parent
06108ea908
commit
232cff02d2
|
@ -51,6 +51,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
let _inHooks = new Dictionary<IntPtr, (InHook * Object)>()
|
let _inHooks = new Dictionary<IntPtr, (InHook * Object)>()
|
||||||
let _outHooks = new Dictionary<IntPtr, (OutHook * Object)>()
|
let _outHooks = new Dictionary<IntPtr, (OutHook * Object)>()
|
||||||
let _syscallHooks = new Dictionary<IntPtr, (SyscallHook * Object)>()
|
let _syscallHooks = new Dictionary<IntPtr, (SyscallHook * Object)>()
|
||||||
|
let _disposablePointers = new List<nativeint>()
|
||||||
|
|
||||||
let mutable _eng = [|UIntPtr.Zero|]
|
let mutable _eng = [|UIntPtr.Zero|]
|
||||||
|
|
||||||
|
@ -66,10 +67,14 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
callbacks.Keys
|
callbacks.Keys
|
||||||
|> Seq.tryFind(fun k -> match callbacks.[k] with | (c, _) -> c = callback)
|
|> Seq.tryFind(fun k -> match callbacks.[k] with | (c, _) -> c = callback)
|
||||||
|> (fun k -> if k.IsSome then callbacks.Remove(k.Value) |> ignore)
|
|> (fun k -> if k.IsSome then callbacks.Remove(k.Value) |> ignore)
|
||||||
|
|
||||||
|
let allocate(size: Int32) =
|
||||||
|
let mem = Marshal.AllocHGlobal(size)
|
||||||
|
_disposablePointers.Add(mem)
|
||||||
|
mem.ToPointer()
|
||||||
|
|
||||||
do
|
do
|
||||||
let mem = Marshal.AllocHGlobal(IntPtr.Size)
|
_eng <- [|new UIntPtr(allocate(IntPtr.Size))|]
|
||||||
_eng <- [|new UIntPtr(mem.ToPointer())|]
|
|
||||||
let err = NativeUnicornEngine.uc_open(uint32 arch, uint32 mode, _eng)
|
let err = NativeUnicornEngine.uc_open(uint32 arch, uint32 mode, _eng)
|
||||||
checkResult(err, "Unable to open the Unicorn Engine")
|
checkResult(err, "Unable to open the Unicorn Engine")
|
||||||
|
|
||||||
|
@ -130,8 +135,8 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
let id = getId()
|
let id = getId()
|
||||||
_codeHooks.Add(id, (callback, userData))
|
_codeHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new CodeHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new CodeHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_CODE, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_CODE, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -147,7 +152,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_blockHooks.Add(id, (callback, userData))
|
_blockHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new BlockHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new BlockHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_BLOCK, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_BLOCK, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -163,7 +168,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_interruptHooks.Add(id, (callback, userData))
|
_interruptHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new InterruptHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new InterruptHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_noarg(_eng.[0], hh, Common.UC_HOOK_INTR, new UIntPtr(funcPointer.ToPointer()), id) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_noarg(_eng.[0], hh, Common.UC_HOOK_INTR, new UIntPtr(funcPointer.ToPointer()), id) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -179,7 +184,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_memReadHooks.Add(id, (callback, userData))
|
_memReadHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new MemReadHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new MemReadHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_MEM_READ, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_MEM_READ, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -195,7 +200,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_memWriteHooks.Add(id, (callback, userData))
|
_memWriteHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new MemWriteHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new MemWriteHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_MEM_WRITE, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0_arg1(_eng.[0], hh, Common.UC_HOOK_MEM_WRITE, new UIntPtr(funcPointer.ToPointer()), id, beginAdd, endAddr) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -212,7 +217,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_memEventHooks.Add(id, (callback, userData))
|
_memEventHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new EventMemHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new EventMemHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_noarg(_eng.[0], hh, check, new UIntPtr(funcPointer.ToPointer()), id) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_noarg(_eng.[0], hh, check, new UIntPtr(funcPointer.ToPointer()), id) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -241,7 +246,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_inHooks.Add(id, (callback, userData))
|
_inHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new InHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new InHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), id, new IntPtr(X86.UC_X86_INS_IN)) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), id, new IntPtr(X86.UC_X86_INS_IN)) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -254,7 +259,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_outHooks.Add(id, (callback, userData))
|
_outHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new OutHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new OutHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), id, new IntPtr(X86.UC_X86_INS_OUT)) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), id, new IntPtr(X86.UC_X86_INS_OUT)) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -267,7 +272,7 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
_syscallHooks.Add(id, (callback, userData))
|
_syscallHooks.Add(id, (callback, userData))
|
||||||
|
|
||||||
let funcPointer = Marshal.GetFunctionPointerForDelegate(new SyscallHookInternal(trampoline))
|
let funcPointer = Marshal.GetFunctionPointerForDelegate(new SyscallHookInternal(trampoline))
|
||||||
let hh = new UIntPtr(Marshal.AllocHGlobal(IntPtr.Size).ToPointer())
|
let hh = new UIntPtr(allocate(IntPtr.Size))
|
||||||
match NativeUnicornEngine.hook_add_arg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), id, new IntPtr(X86.UC_X86_INS_SYSCALL)) |> this.CheckResult with
|
match NativeUnicornEngine.hook_add_arg0(_eng.[0], hh, Common.UC_HOOK_INSN, new UIntPtr(funcPointer.ToPointer()), id, new IntPtr(X86.UC_X86_INS_SYSCALL)) |> this.CheckResult with
|
||||||
| Some e -> raise e | None -> ()
|
| Some e -> raise e | None -> ()
|
||||||
|
|
||||||
|
@ -275,3 +280,24 @@ and Unicorn(arch: Int32, mode: Int32) =
|
||||||
let (major, minor) = (new UIntPtr(), new UIntPtr())
|
let (major, minor) = (new UIntPtr(), new UIntPtr())
|
||||||
let combined = NativeUnicornEngine.version(major, minor)
|
let combined = NativeUnicornEngine.version(major, minor)
|
||||||
(major.ToUInt32(), minor.ToUInt32(), combined)
|
(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
|
||||||
|
|
||||||
|
member this.Dispose() =
|
||||||
|
this.Dispose(true)
|
||||||
|
GC.SuppressFinalize(this)
|
||||||
|
|
||||||
|
override this.Finalize() =
|
||||||
|
this.Dispose(false)
|
||||||
|
|
||||||
|
interface IDisposable with
|
||||||
|
member this.Dispose() =
|
||||||
|
this.Dispose()
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
<ProjectGuid>6f0e55fa-a056-45ff-bb24-641457b430a8</ProjectGuid>
|
<ProjectGuid>6f0e55fa-a056-45ff-bb24-641457b430a8</ProjectGuid>
|
||||||
<OutputType>Library</OutputType>
|
<OutputType>Library</OutputType>
|
||||||
<RootNamespace>UnicornSln</RootNamespace>
|
<RootNamespace>UnicornSln</RootNamespace>
|
||||||
<AssemblyName>UnicornSln</AssemblyName>
|
<AssemblyName>UnicornManaged</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
||||||
<TargetFSharpCoreVersion>4.3.0.0</TargetFSharpCoreVersion>
|
<TargetFSharpCoreVersion>4.4.0.0</TargetFSharpCoreVersion>
|
||||||
<Name>Unicorn</Name>
|
<Name>Unicorn</Name>
|
||||||
<TargetFrameworkProfile />
|
<TargetFrameworkProfile />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -22,8 +22,11 @@
|
||||||
<OutputPath>bin\Debug\</OutputPath>
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
<WarningLevel>3</WarningLevel>
|
<WarningLevel>3</WarningLevel>
|
||||||
<DocumentationFile>bin\Debug\UnicornSln.XML</DocumentationFile>
|
<DocumentationFile>bin\Debug\UnicornManaged.XML</DocumentationFile>
|
||||||
<PlatformTarget>x86</PlatformTarget>
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
|
<WarningsAsErrors />
|
||||||
|
<EnableUnmanagedDebugging>true</EnableUnmanagedDebugging>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<DebugType>pdbonly</DebugType>
|
<DebugType>pdbonly</DebugType>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="utf-8" ?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<configuration>
|
<configuration>
|
||||||
<startup>
|
<startup>
|
||||||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
|
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
|
||||||
</startup>
|
</startup>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
|
@ -34,6 +34,9 @@ namespace UnicornTests
|
||||||
// Run all shellcode tests
|
// Run all shellcode tests
|
||||||
ShellcodeTest.TestX86Code32Self();
|
ShellcodeTest.TestX86Code32Self();
|
||||||
ShellcodeTest.TestX86Code32();
|
ShellcodeTest.TestX86Code32();
|
||||||
|
|
||||||
|
Console.Write("Tests completed");
|
||||||
|
Console.ReadLine();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,33 +72,35 @@ namespace UnicornTests
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var u = new Unicorn(Common.UC_ARCH_X86, Common.UC_MODE_32);
|
using (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
|
||||||
u.MemMap(address, new UIntPtr(2 * 1024 * 1024), Common.UC_PROT_ALL);
|
u.MemMap(address, new UIntPtr(2 * 1024 * 1024), Common.UC_PROT_ALL);
|
||||||
|
|
||||||
// write machine code to be emulated to memory
|
// write machine code to be emulated to memory
|
||||||
u.MemWrite(address, code);
|
u.MemWrite(address, code);
|
||||||
|
|
||||||
// initialize machine registers
|
// initialize machine registers
|
||||||
u.RegWrite(X86.UC_X86_REG_ESP, Utils.Int64ToBytes(address + 0x200000));
|
u.RegWrite(X86.UC_X86_REG_ESP, Utils.Int64ToBytes(address + 0x200000));
|
||||||
|
|
||||||
// tracing all instructions by having @begin > @end
|
// tracing all instructions by having @begin > @end
|
||||||
u.AddCodeHook(CodeHookCallback, null, 1, 0);
|
u.AddCodeHook(CodeHookCallback, null, 1, 0);
|
||||||
|
|
||||||
// handle interrupt ourself
|
// handle interrupt ourself
|
||||||
u.AddInterruptHook(InterruptHookCallback, null);
|
u.AddInterruptHook(InterruptHookCallback, null);
|
||||||
|
|
||||||
// handle SYSCALL
|
// handle SYSCALL
|
||||||
u.AddSyscallHook(SyscallHookCallback, null);
|
u.AddSyscallHook(SyscallHookCallback, null);
|
||||||
|
|
||||||
Console.WriteLine(">>> Start tracing linux code");
|
Console.WriteLine(">>> Start tracing linux code");
|
||||||
|
|
||||||
// emulate machine code in infinite time
|
// emulate machine code in infinite time
|
||||||
u.EmuStart(address, address + (UInt64)code.Length, 0u, new UIntPtr(0));
|
u.EmuStart(address, address + (UInt64)code.Length, 0u, new UIntPtr(0));
|
||||||
|
|
||||||
Console.WriteLine(">>> Emulation Done!");
|
Console.WriteLine(">>> Emulation Done!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (UnicornEngineException ex)
|
catch (UnicornEngineException ex)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,9 +9,10 @@
|
||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
<RootNamespace>UnicornTests</RootNamespace>
|
<RootNamespace>UnicornTests</RootNamespace>
|
||||||
<AssemblyName>UnicornTests</AssemblyName>
|
<AssemblyName>UnicornTests</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
|
||||||
|
<TargetFrameworkProfile />
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
<PlatformTarget>x86</PlatformTarget>
|
<PlatformTarget>x86</PlatformTarget>
|
||||||
|
@ -22,6 +23,7 @@
|
||||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
<ErrorReport>prompt</ErrorReport>
|
<ErrorReport>prompt</ErrorReport>
|
||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||||
|
|
Loading…
Reference in a new issue