FunctionPointerVariableHook: use VirtualAlloc instead of HeapAlloc

This commit is contained in:
Soreepeong 2023-03-20 00:43:54 +09:00
parent 8781183eaa
commit 325b3d551d
2 changed files with 8 additions and 8 deletions

View file

@ -58,7 +58,14 @@ internal class FunctionPointerVariableHook<T> : Hook<T>
unsafe unsafe
{ {
var pfnThunkBytes = (byte*)NativeFunctions.HeapAlloc(HookManager.NoFreeExecutableHeap, 0, 12); // Note: WINE seemingly tries to clean up all heap allocations on process exit.
// We want our allocation to be kept there forever, until no running thread remains.
// Therefore we're using VirtualAlloc instead of HeapCreate/HeapAlloc.
var pfnThunkBytes = (byte*)NativeFunctions.VirtualAlloc(
0,
12,
NativeFunctions.AllocationType.Reserve | NativeFunctions.AllocationType.Commit,
MemoryProtection.ExecuteReadWrite);
if (pfnThunkBytes == null) if (pfnThunkBytes == null)
{ {
throw new OutOfMemoryException("Failed to allocate memory for import hooks."); throw new OutOfMemoryException("Failed to allocate memory for import hooks.");

View file

@ -16,13 +16,6 @@ namespace Dalamud.Hooking.Internal;
[ServiceManager.EarlyLoadedService] [ServiceManager.EarlyLoadedService]
internal class HookManager : IDisposable, IServiceType internal class HookManager : IDisposable, IServiceType
{ {
/// <summary>
/// Handle to an executable heap that we shall never free anything unless we can be absolutely sure that nothing is
/// referencing to it anymore.
/// </summary>
internal static readonly nint NoFreeExecutableHeap =
NativeFunctions.HeapCreate(NativeFunctions.HeapOptions.CreateEnableExecute, 0, 0);
private static readonly ModuleLog Log = new("HM"); private static readonly ModuleLog Log = new("HM");
[ServiceManager.ServiceConstructor] [ServiceManager.ServiceConstructor]