Skip to content

Commit

Permalink
Added: FunctionPatcher in ASM Hook for Relative->Absolute Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Nov 25, 2022
1 parent 399266e commit 5120ed7
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions source/Reloaded.Hooks/AsmHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public unsafe class AsmHook : IAsmHook
private Patch _enableHookPatch;
private bool _activated = false;
private bool _is64Bit;

private List<Patch> _otherHookPatches;

/* Construction - Destruction */

Expand Down Expand Up @@ -105,6 +107,11 @@ public AsmHook(byte[] asmCode, nuint functionAddress, AsmHookOptions options = d
options.hookLength = Utilities.GetHookLength(functionAddress, options.MaxOpcodeSize, _is64Bit);

CurrentProcess.SafeReadRaw(functionAddress, out byte[] originalFunction, options.hookLength);
var functionPatcher = new FunctionPatcher(_is64Bit);
var functionPatch = functionPatcher.Patch(originalFunction.ToList(), functionAddress);
originalFunction = functionPatch.NewFunction.ToArray();
_otherHookPatches = functionPatch.Patches;

nuint jumpBackAddress = functionAddress + (nuint)options.hookLength;

/* Size calculations for buffer, must have sufficient space. */
Expand Down Expand Up @@ -135,10 +142,10 @@ public AsmHook(byte[] asmCode, nuint functionAddress, AsmHookOptions options = d
// Make Hook and Original Stub
buffer.SetAlignment(codeAlignment);
nuint hookStubAddr = MakeHookStub(buffer, patcher, asmCode, originalFunction, jumpBackAddress, options.Behaviour);
nuint hookStubAddr = MakeHookStub(buffer, patcher, asmCode, originalFunction.Length, jumpBackAddress, options.Behaviour);
buffer.SetAlignment(codeAlignment);
nuint originalStubAddr = MakeOriginalStub(buffer, patcher, originalFunction, jumpBackAddress);
nuint originalStubAddr = MakeOriginalStub(buffer, patcher, originalFunction.Length, jumpBackAddress);
// Make Jump to Entry, Original Stub
buffer.SetAlignment(codeAlignment);
Expand All @@ -161,9 +168,9 @@ public AsmHook(byte[] asmCode, nuint functionAddress, AsmHookOptions options = d
});
}

private nuint MakeHookStub(MemoryBuffer buffer, IcedPatcher patcher, byte[] asmCode, byte[] originalCode, nuint jumpBackAddress, AsmHookBehaviour behaviour)
private nuint MakeHookStub(MemoryBuffer buffer, IcedPatcher patcher, byte[] asmCode, int originalCodeLength, nuint jumpBackAddress, AsmHookBehaviour behaviour)
{
var bytes = new List<byte>(asmCode.Length + originalCode.Length);
var bytes = new List<byte>(asmCode.Length + originalCodeLength);

switch (behaviour)
{
Expand All @@ -190,9 +197,9 @@ private nuint MakeHookStub(MemoryBuffer buffer, IcedPatcher patcher, byte[] asmC
return buffer.Add(bytes.ToArray(), 1); // Buffer is pre-aligned
}

private nuint MakeOriginalStub(MemoryBuffer buffer, IcedPatcher patcher, byte[] originalCode, nuint jumpBackAddress)
private nuint MakeOriginalStub(MemoryBuffer buffer, IcedPatcher patcher, int originalCodeLength, nuint jumpBackAddress)
{
var bytes = new List<byte>(originalCode.Length);
var bytes = new List<byte>(originalCodeLength);
bytes.AddRange(patcher.EncodeForNewAddress(buffer.Properties.WritePointer));

var jmpBackBytes = Utilities.AssembleRelativeJump(buffer.Properties.WritePointer + (nuint)bytes.Count, jumpBackAddress, _is64Bit);
Expand All @@ -210,6 +217,9 @@ public IAsmHook Activate()
_activated = true;
_activateHookPatch.Apply();
_enableHookPatch.ApplyUnsafe();

foreach (var hookPatch in _otherHookPatches)
hookPatch.Apply();
}

return this;
Expand Down

0 comments on commit 5120ed7

Please sign in to comment.