Patch Process.Handle

This commit is contained in:
Raymond 2021-09-03 22:07:30 -04:00
parent d23b710edf
commit d0c2c1bf9d

View file

@ -1,4 +1,6 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
@ -19,6 +21,7 @@ using Dalamud.Interface.Internal;
using Dalamud.IoC.Internal;
using Dalamud.Plugin.Internal;
using Dalamud.Plugin.Ipc.Internal;
using HarmonyLib;
using Serilog;
using Serilog.Core;
@ -52,6 +55,8 @@ namespace Dalamud
/// <param name="configuration">The Dalamud configuration.</param>
public Dalamud(DalamudStartInfo info, LoggingLevelSwitch loggingLevelSwitch, ManualResetEvent finishSignal, DalamudConfiguration configuration)
{
this.ApplyProcessPatch();
Service<Dalamud>.Set(this);
Service<DalamudStartInfo>.Set(info);
Service<DalamudConfiguration>.Set(configuration);
@ -363,5 +368,34 @@ namespace Dalamud
var oldFilter = NativeFunctions.SetUnhandledExceptionFilter(releaseFilter);
Log.Debug("Reset ExceptionFilter, old: {0}", oldFilter);
}
/// <summary>
/// Patch method for the class Process.Handle. This patch facilitates fixing Reloaded so that it
/// uses pseudo-handles to access memory, to prevent permission errors.
/// It should never be called manually.
/// </summary>
/// <param name="__instance">The equivalent of `this`.</param>
/// <param name="__result">The result from the original method.</param>
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Enforced naming for special injected parameters")]
private static void ProcessHandlePatch(Process __instance, ref IntPtr __result)
{
if (__instance.Id == Environment.ProcessId)
{
__result = (IntPtr)0xFFFFFFFF;
}
Log.Verbose($"Process.Handle // {__instance.ProcessName} // {__result:X}");
}
private void ApplyProcessPatch()
{
var harmony = new Harmony("goatcorp.dalamud");
var targetType = typeof(Process);
var handleTarget = AccessTools.PropertyGetter(targetType, nameof(Process.Handle));
var handlePatch = AccessTools.Method(typeof(Dalamud), nameof(Dalamud.ProcessHandlePatch));
harmony.Patch(handleTarget, postfix: new(handlePatch));
}
}
}