Compare commits

...

3 commits

Author SHA1 Message Date
Actions User
eec8ee7094 [CI] Updating repo.json for 1.5.1.13
Some checks failed
.NET Build / build (push) Has been cancelled
2026-01-27 15:30:23 +00:00
Marc-Aurel Zent
13500264b7 Use iced to create AsmHooks in PapRewriter.
Some checks failed
.NET Build / build (push) Has been cancelled
2025-12-22 15:31:12 +01:00
Actions User
6ba735eefb [CI] Updating repo.json for 1.5.1.12
Some checks failed
.NET Build / build (push) Has been cancelled
2025-12-20 20:53:36 +00:00
2 changed files with 59 additions and 36 deletions

View file

@ -1,6 +1,7 @@
using System.Text.Unicode; using System.Text.Unicode;
using Dalamud.Hooking; using Dalamud.Hooking;
using Iced.Intel; using Iced.Intel;
using static Iced.Intel.AssemblerRegisters;
using OtterGui.Extensions; using OtterGui.Extensions;
using Penumbra.String.Classes; using Penumbra.String.Classes;
using Swan; using Swan;
@ -46,36 +47,32 @@ public sealed class PapRewriter(PeSigScanner sigScanner, PapRewriter.PapResource
stackAccesses.RemoveAll(instr => instr.IP == hp.IP); stackAccesses.RemoveAll(instr => instr.IP == hp.IP);
var detourPointer = Marshal.GetFunctionPointerForDelegate(papResourceHandler); var detourPointer = Marshal.GetFunctionPointerForDelegate(papResourceHandler);
var targetRegister = hookPoint.Op0Register.ToString().ToLower(); var targetRegister = GetRegister64(hookPoint.Op0Register);
var hookAddress = new IntPtr((long)detourPoint.IP); var hookAddress = new IntPtr((long)detourPoint.IP);
var caveAllocation = NativeAllocCave(16); var caveAllocation = NativeAllocCave(16);
var hook = new AsmHook( var assembler = new Assembler(64);
hookAddress, assembler.mov(targetRegister, stringAllocation); // Move our char *path into the relevant register (rdx)
[
"use64",
$"mov {targetRegister}, 0x{stringAllocation:x8}", // Move our char *path into the relevant register (rdx)
// After this asm stub, we have a call to Crc32(); since r9 is a volatile, unused register, we can use it ourselves // After this asm stub, we have a call to Crc32(); since r9 is a volatile, unused register, we can use it ourselves
// We're essentially storing the original 2 arguments ('this', 'path'), in case they get mangled in our call // We're essentially storing the original 2 arguments ('this', 'path'), in case they get mangled in our call
// We technically don't need to save rdx ('path'), since it'll be stringLoc, but eh // We technically don't need to save rdx ('path'), since it'll be stringLoc, but eh
$"mov r9, 0x{caveAllocation:x8}", assembler.mov(r9, caveAllocation);
"mov [r9], rcx", assembler.mov(__qword_ptr[r9], rcx);
"mov [r9+0x8], rdx", assembler.mov(__qword_ptr[r9 + 8], rdx);
// We can use 'rax' here too since it's also volatile, and it'll be overwritten by Crc32()'s return anyway // We can use 'rax' here too since it's also volatile, and it'll be overwritten by Crc32()'s return anyway
$"mov rax, 0x{detourPointer:x8}", // Get a pointer to our detour in place assembler.mov(rax, detourPointer);
"call rax", // Call detour assembler.call(rax);
// Do the reverse process and retrieve the stored stuff // Do the reverse process and retrieve the stored stuff
$"mov r9, 0x{caveAllocation:x8}", assembler.mov(r9, caveAllocation);
"mov rcx, [r9]", assembler.mov(rcx, __qword_ptr[r9]);
"mov rdx, [r9+0x8]", assembler.mov(rdx, __qword_ptr[r9 + 8]);
// Plop 'rax' (our return value, the path size) into r8, so it's the third argument for the subsequent Crc32() call // Plop 'rax' (our return value, the path size) into r8, so it's the third argument for the subsequent Crc32() call
"mov r8, rax", assembler.mov(r8, rax);
], $"{name}.PapRedirection" var hook = new AsmHook(hookAddress, AssembleToBytes(assembler), $"{name}.PapRedirection");
);
_hooks.Add(hookAddress, hook); _hooks.Add(hookAddress, hook);
hook.Enable(); hook.Enable();
@ -95,19 +92,45 @@ public sealed class PapRewriter(PeSigScanner sigScanner, PapRewriter.PapResource
if (_hooks.ContainsKey(hookAddress)) if (_hooks.ContainsKey(hookAddress))
continue; continue;
var targetRegister = stackAccess.Op0Register.ToString().ToLower(); var targetRegister = GetRegister64(stackAccess.Op0Register);
var hook = new AsmHook( var assembler = new Assembler(64);
hookAddress, assembler.mov(targetRegister, stringAllocation);
[ var hook = new AsmHook(hookAddress, AssembleToBytes(assembler), $"{name}.PapStackAccess[{index}]");
"use64",
$"mov {targetRegister}, 0x{stringAllocation:x8}",
], $"{name}.PapStackAccess[{index}]"
);
_hooks.Add(hookAddress, hook); _hooks.Add(hookAddress, hook);
hook.Enable(); hook.Enable();
} }
} }
private static AssemblerRegister64 GetRegister64(Register reg)
=> reg switch
{
Register.RAX => rax,
Register.RCX => rcx,
Register.RDX => rdx,
Register.RBX => rbx,
Register.RSP => rsp,
Register.RBP => rbp,
Register.RSI => rsi,
Register.RDI => rdi,
Register.R8 => r8,
Register.R9 => r9,
Register.R10 => r10,
Register.R11 => r11,
Register.R12 => r12,
Register.R13 => r13,
Register.R14 => r14,
Register.R15 => r15,
_ => throw new ArgumentOutOfRangeException(nameof(reg), reg, "Unsupported register."),
};
private static byte[] AssembleToBytes(Assembler assembler)
{
using var stream = new MemoryStream();
var writer = new StreamCodeWriter(stream);
assembler.Assemble(writer, 0);
return stream.ToArray();
}
private static IEnumerable<Instruction> ScanStackAccesses(IEnumerable<Instruction> instructions, Instruction hookPoint) private static IEnumerable<Instruction> ScanStackAccesses(IEnumerable<Instruction> instructions, Instruction hookPoint)
{ {

View file

@ -5,8 +5,8 @@
"Punchline": "Runtime mod loader and manager.", "Punchline": "Runtime mod loader and manager.",
"Description": "Runtime mod loader and manager.", "Description": "Runtime mod loader and manager.",
"InternalName": "Penumbra", "InternalName": "Penumbra",
"AssemblyVersion": "1.5.1.11", "AssemblyVersion": "1.5.1.13",
"TestingAssemblyVersion": "1.5.1.11", "TestingAssemblyVersion": "1.5.1.13",
"RepoUrl": "https://github.com/xivdev/Penumbra", "RepoUrl": "https://github.com/xivdev/Penumbra",
"ApplicableVersion": "any", "ApplicableVersion": "any",
"DalamudApiLevel": 14, "DalamudApiLevel": 14,
@ -18,9 +18,9 @@
"LoadPriority": 69420, "LoadPriority": 69420,
"LoadRequiredState": 2, "LoadRequiredState": 2,
"LoadSync": true, "LoadSync": true,
"DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.5.1.11/Penumbra.zip", "DownloadLinkInstall": "https://github.com/xivdev/Penumbra/releases/download/1.5.1.13/Penumbra.zip",
"DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.5.1.11/Penumbra.zip", "DownloadLinkTesting": "https://github.com/xivdev/Penumbra/releases/download/1.5.1.13/Penumbra.zip",
"DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.5.1.11/Penumbra.zip", "DownloadLinkUpdate": "https://github.com/xivdev/Penumbra/releases/download/1.5.1.13/Penumbra.zip",
"IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png" "IconUrl": "https://raw.githubusercontent.com/xivdev/Penumbra/master/images/icon.png"
} }
] ]