fix: update Iced in Injector, fix build

This commit is contained in:
goat 2022-07-17 14:46:48 +02:00
parent 8d9070a1b3
commit 8c066451ec
No known key found for this signature in database
GPG key ID: 7773BB5B43BA52E5
6 changed files with 44 additions and 54 deletions

View file

@ -60,7 +60,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Iced" Version="1.13.0" /> <PackageReference Include="Iced" Version="1.17.0" />
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" /> <PackageReference Include="JetBrains.Annotations" Version="2022.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="PeNet" Version="2.6.4" /> <PackageReference Include="PeNet" Version="2.6.4" />

View file

@ -731,7 +731,7 @@ namespace Dalamud.Injector
using var startInfoBuffer = new MemoryBufferHelper(process).CreatePrivateMemoryBuffer(startInfoBytes.Length + 0x8); using var startInfoBuffer = new MemoryBufferHelper(process).CreatePrivateMemoryBuffer(startInfoBytes.Length + 0x8);
var startInfoAddress = startInfoBuffer.Add(startInfoBytes); var startInfoAddress = startInfoBuffer.Add(startInfoBytes);
if (startInfoAddress == IntPtr.Zero) if (startInfoAddress == 0)
throw new Exception("Unable to allocate start info JSON"); throw new Exception("Unable to allocate start info JSON");
injector.GetFunctionAddress(bootModule, "Initialize", out var initAddress); injector.GetFunctionAddress(bootModule, "Initialize", out var initAddress);

View file

@ -32,11 +32,11 @@ namespace Dalamud.Injector
private readonly CircularBuffer circularBuffer; private readonly CircularBuffer circularBuffer;
private readonly PrivateMemoryBuffer memoryBuffer; private readonly PrivateMemoryBuffer memoryBuffer;
private IntPtr loadLibraryShellPtr; private nuint loadLibraryShellPtr;
private IntPtr loadLibraryRetPtr; private nuint loadLibraryRetPtr;
private IntPtr getProcAddressShellPtr; private nuint getProcAddressShellPtr;
private IntPtr getProcAddressRetPtr; private nuint getProcAddressRetPtr;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Injector"/> class. /// Initializes a new instance of the <see cref="Injector"/> class.
@ -85,11 +85,11 @@ namespace Dalamud.Injector
{ {
var lpParameter = this.WriteNullTerminatedUnicodeString(modulePath); var lpParameter = this.WriteNullTerminatedUnicodeString(modulePath);
if (lpParameter == IntPtr.Zero) if (lpParameter == 0)
throw new Exception("Unable to allocate LoadLibraryW parameter"); throw new Exception("Unable to allocate LoadLibraryW parameter");
this.CallRemoteFunction(this.loadLibraryShellPtr, lpParameter, out var err); this.CallRemoteFunction(this.loadLibraryShellPtr, lpParameter, out var err);
address = this.extMemory.Read<IntPtr>(this.loadLibraryRetPtr); this.extMemory.Read<IntPtr>(this.loadLibraryRetPtr, out address);
if (address == IntPtr.Zero) if (address == IntPtr.Zero)
throw new Exception($"LoadLibraryW(\"{modulePath}\") failure: {new Win32Exception((int)err).Message} ({err})"); throw new Exception($"LoadLibraryW(\"{modulePath}\") failure: {new Win32Exception((int)err).Message} ({err})");
} }
@ -100,17 +100,17 @@ namespace Dalamud.Injector
/// <param name="module">Module address.</param> /// <param name="module">Module address.</param>
/// <param name="functionName">Name of the exported method.</param> /// <param name="functionName">Name of the exported method.</param>
/// <param name="address">Address to the function.</param> /// <param name="address">Address to the function.</param>
public void GetFunctionAddress(IntPtr module, string functionName, out IntPtr address) public void GetFunctionAddress(IntPtr module, string functionName, out nuint address)
{ {
var functionNamePtr = this.WriteNullTerminatedASCIIString(functionName); var functionNamePtr = this.WriteNullTerminatedASCIIString(functionName);
var getProcAddressParams = new GetProcAddressParams(module, functionNamePtr); var getProcAddressParams = new GetProcAddressParams(module, functionNamePtr);
var lpParameter = this.circularBuffer.Add(ref getProcAddressParams); var lpParameter = this.circularBuffer.Add(ref getProcAddressParams);
if (lpParameter == IntPtr.Zero) if (lpParameter == 0)
throw new Exception("Unable to allocate GetProcAddress parameter ptr"); throw new Exception("Unable to allocate GetProcAddress parameter ptr");
this.CallRemoteFunction(this.getProcAddressShellPtr, lpParameter, out var err); this.CallRemoteFunction(this.getProcAddressShellPtr, lpParameter, out var err);
address = this.extMemory.Read<IntPtr>(this.getProcAddressRetPtr); this.extMemory.Read<nuint>(this.getProcAddressRetPtr, out address);
if (address == IntPtr.Zero) if (address == 0)
throw new Exception($"GetProcAddress(0x{module:X}, \"{functionName}\") failure: {new Win32Exception((int)err).Message} ({err})"); throw new Exception($"GetProcAddress(0x{module:X}, \"{functionName}\") failure: {new Win32Exception((int)err).Message} ({err})");
} }
@ -120,7 +120,7 @@ namespace Dalamud.Injector
/// <param name="methodAddress">Method address.</param> /// <param name="methodAddress">Method address.</param>
/// <param name="parameterAddress">Parameter address.</param> /// <param name="parameterAddress">Parameter address.</param>
/// <param name="exitCode">Thread exit code.</param> /// <param name="exitCode">Thread exit code.</param>
public void CallRemoteFunction(IntPtr methodAddress, IntPtr parameterAddress, out uint exitCode) public void CallRemoteFunction(nuint methodAddress, nuint parameterAddress, out uint exitCode)
{ {
// Create and initialize a thread at our address and parameter address. // Create and initialize a thread at our address and parameter address.
var threadHandle = CreateRemoteThread( var threadHandle = CreateRemoteThread(
@ -151,26 +151,23 @@ namespace Dalamud.Injector
Log.Verbose($"LoadLibraryW: 0x{functionAddr.ToInt64():X}"); Log.Verbose($"LoadLibraryW: 0x{functionAddr.ToInt64():X}");
var functionPtr = this.memoryBuffer.Add(ref functionAddr); var functionPtr = this.memoryBuffer.Add(ref functionAddr);
Log.Verbose($"LoadLibraryPtr: 0x{functionPtr.ToInt64():X}"); Log.Verbose($"LoadLibraryPtr: 0x{functionPtr:X}");
if (functionPtr == IntPtr.Zero) if (functionPtr == 0)
throw new Exception("Unable to allocate LoadLibraryW function ptr"); throw new Exception("Unable to allocate LoadLibraryW function ptr");
var dummy = IntPtr.Zero; var dummy = IntPtr.Zero;
this.loadLibraryRetPtr = this.memoryBuffer.Add(ref dummy); this.loadLibraryRetPtr = this.memoryBuffer.Add(ref dummy);
Log.Verbose($"LoadLibraryRetPtr: 0x{this.loadLibraryRetPtr.ToInt64():X}"); Log.Verbose($"LoadLibraryRetPtr: 0x{this.loadLibraryRetPtr:X}");
if (this.loadLibraryRetPtr == IntPtr.Zero) if (this.loadLibraryRetPtr == 0)
throw new Exception("Unable to allocate LoadLibraryW return value"); throw new Exception("Unable to allocate LoadLibraryW return value");
var func = functionPtr.ToInt64();
var retVal = this.loadLibraryRetPtr.ToInt64();
var asm = new Assembler(64); var asm = new Assembler(64);
asm.sub(rsp, 40); // sub rsp, 40 // Re-align stack to 16 byte boundary + shadow space. asm.sub(rsp, 40); // sub rsp, 40 // Re-align stack to 16 byte boundary + shadow space.
asm.call(__qword_ptr[__qword_ptr[func]]); // call qword [qword func] // CreateRemoteThread lpParameter with string already in ECX. asm.call(__qword_ptr[__qword_ptr[functionPtr]]); // call qword [qword func] // CreateRemoteThread lpParameter with string already in ECX.
asm.mov(__qword_ptr[__qword_ptr[retVal]], rax); // mov qword [qword retVal], rax // asm.mov(__qword_ptr[__qword_ptr[this.loadLibraryRetPtr]], rax); // mov qword [qword retVal], rax //
asm.add(rsp, 40); // add rsp, 40 // Re-align stack to 16 byte boundary + shadow space. asm.add(rsp, 40); // add rsp, 40 // Re-align stack to 16 byte boundary + shadow space.
asm.mov(rax, (ulong)getLastErrorAddr); // mov rax, pfnGetLastError // Change return address to GetLastError. asm.mov(rax, (ulong)getLastErrorAddr); // mov rax, pfnGetLastError // Change return address to GetLastError.
asm.push(rax); // push rax // asm.push(rax); // push rax //
@ -178,18 +175,18 @@ namespace Dalamud.Injector
var bytes = this.Assemble(asm); var bytes = this.Assemble(asm);
this.loadLibraryShellPtr = this.memoryBuffer.Add(bytes); this.loadLibraryShellPtr = this.memoryBuffer.Add(bytes);
Log.Verbose($"LoadLibraryShellPtr: 0x{this.loadLibraryShellPtr.ToInt64():X}"); Log.Verbose($"LoadLibraryShellPtr: 0x{this.loadLibraryShellPtr:X}");
if (this.loadLibraryShellPtr == IntPtr.Zero) if (this.loadLibraryShellPtr == 0)
throw new Exception("Unable to allocate LoadLibraryW shellcode"); throw new Exception("Unable to allocate LoadLibraryW shellcode");
this.extMemory.ChangePermission(this.loadLibraryShellPtr, bytes.Length, Reloaded.Memory.Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE); this.extMemory.ChangePermission(this.loadLibraryShellPtr, bytes.Length, Reloaded.Memory.Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE);
#if DEBUG #if DEBUG
var outFunctionPtr = this.extMemory.Read<IntPtr>(functionPtr); this.extMemory.Read<IntPtr>(functionPtr, out var outFunctionPtr);
Log.Verbose($"LoadLibraryPtr: {this.GetResultMarker(outFunctionPtr == functionAddr)}"); Log.Verbose($"LoadLibraryPtr: {this.GetResultMarker(outFunctionPtr == functionAddr)}");
var outRetPtr = this.extMemory.Read<IntPtr>(this.loadLibraryRetPtr); this.extMemory.Read<IntPtr>(this.loadLibraryRetPtr, out var outRetPtr);
Log.Verbose($"LoadLibraryRet: {this.GetResultMarker(dummy == outRetPtr)}"); Log.Verbose($"LoadLibraryRet: {this.GetResultMarker(dummy == outRetPtr)}");
this.extMemory.ReadRaw(this.loadLibraryShellPtr, out var outBytes, bytes.Length); this.extMemory.ReadRaw(this.loadLibraryShellPtr, out var outBytes, bytes.Length);
@ -207,28 +204,25 @@ namespace Dalamud.Injector
Log.Verbose($"GetProcAddress: 0x{functionAddr.ToInt64():X}"); Log.Verbose($"GetProcAddress: 0x{functionAddr.ToInt64():X}");
var functionPtr = this.memoryBuffer.Add(ref functionAddr); var functionPtr = this.memoryBuffer.Add(ref functionAddr);
Log.Verbose($"GetProcAddressPtr: 0x{functionPtr.ToInt64():X}"); Log.Verbose($"GetProcAddressPtr: 0x{functionPtr:X}");
if (functionPtr == IntPtr.Zero) if (functionPtr == 0)
throw new Exception("Unable to allocate GetProcAddress function ptr"); throw new Exception("Unable to allocate GetProcAddress function ptr");
var dummy = IntPtr.Zero; var dummy = IntPtr.Zero;
this.getProcAddressRetPtr = this.memoryBuffer.Add(ref dummy); this.getProcAddressRetPtr = this.memoryBuffer.Add(ref dummy);
Log.Verbose($"GetProcAddressRetPtr: 0x{this.loadLibraryRetPtr.ToInt64():X}"); Log.Verbose($"GetProcAddressRetPtr: 0x{this.loadLibraryRetPtr:X}");
if (this.getProcAddressRetPtr == IntPtr.Zero) if (this.getProcAddressRetPtr == 0)
throw new Exception("Unable to allocate GetProcAddress return value"); throw new Exception("Unable to allocate GetProcAddress return value");
var func = functionPtr.ToInt64();
var retVal = this.getProcAddressRetPtr.ToInt64();
var asm = new Assembler(64); var asm = new Assembler(64);
asm.sub(rsp, 40); // sub rsp, 40 // Re-align stack to 16 byte boundary +32 shadow space asm.sub(rsp, 40); // sub rsp, 40 // Re-align stack to 16 byte boundary +32 shadow space
asm.mov(rdx, __qword_ptr[__qword_ptr[rcx + 8]]); // mov rdx, qword [qword rcx + 8] // lpProcName asm.mov(rdx, __qword_ptr[__qword_ptr[rcx + 8]]); // mov rdx, qword [qword rcx + 8] // lpProcName
asm.mov(rcx, __qword_ptr[__qword_ptr[rcx + 0]]); // mov rcx, qword [qword rcx + 0] // hModule asm.mov(rcx, __qword_ptr[__qword_ptr[rcx + 0]]); // mov rcx, qword [qword rcx + 0] // hModule
asm.call(__qword_ptr[__qword_ptr[func]]); // call qword [qword func] // asm.call(__qword_ptr[__qword_ptr[functionPtr]]); // call qword [qword func] //
asm.mov(__qword_ptr[__qword_ptr[retVal]], rax); // mov qword [qword retVal] // asm.mov(__qword_ptr[__qword_ptr[this.getProcAddressRetPtr]], rax); // mov qword [qword retVal] //
asm.add(rsp, 40); // add rsp, 40 // Re-align stack to 16 byte boundary + shadow space. asm.add(rsp, 40); // add rsp, 40 // Re-align stack to 16 byte boundary + shadow space.
asm.mov(rax, (ulong)getLastErrorAddr); // mov rax, pfnGetLastError // Change return address to GetLastError. asm.mov(rax, (ulong)getLastErrorAddr); // mov rax, pfnGetLastError // Change return address to GetLastError.
asm.push(rax); // push rax // asm.push(rax); // push rax //
@ -236,18 +230,18 @@ namespace Dalamud.Injector
var bytes = this.Assemble(asm); var bytes = this.Assemble(asm);
this.getProcAddressShellPtr = this.memoryBuffer.Add(bytes); this.getProcAddressShellPtr = this.memoryBuffer.Add(bytes);
Log.Verbose($"GetProcAddressShellPtr: 0x{this.getProcAddressShellPtr.ToInt64():X}"); Log.Verbose($"GetProcAddressShellPtr: 0x{this.getProcAddressShellPtr:X}");
if (this.getProcAddressShellPtr == IntPtr.Zero) if (this.getProcAddressShellPtr == 0)
throw new Exception("Unable to allocate GetProcAddress shellcode"); throw new Exception("Unable to allocate GetProcAddress shellcode");
this.extMemory.ChangePermission(this.getProcAddressShellPtr, bytes.Length, Reloaded.Memory.Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE); this.extMemory.ChangePermission(this.getProcAddressShellPtr, bytes.Length, Reloaded.Memory.Kernel32.Kernel32.MEM_PROTECTION.PAGE_EXECUTE_READWRITE);
#if DEBUG #if DEBUG
var outFunctionPtr = this.extMemory.Read<IntPtr>(functionPtr); this.extMemory.Read<IntPtr>(functionPtr, out var outFunctionPtr);
Log.Verbose($"GetProcAddressPtr: {this.GetResultMarker(outFunctionPtr == functionAddr)}"); Log.Verbose($"GetProcAddressPtr: {this.GetResultMarker(outFunctionPtr == functionAddr)}");
var outRetPtr = this.extMemory.Read<IntPtr>(this.loadLibraryRetPtr); this.extMemory.Read<IntPtr>(this.loadLibraryRetPtr, out var outRetPtr);
Log.Verbose($"GetProcAddressRet: {this.GetResultMarker(dummy == outRetPtr)}"); Log.Verbose($"GetProcAddressRet: {this.GetResultMarker(dummy == outRetPtr)}");
this.extMemory.ReadRaw(this.getProcAddressShellPtr, out var outBytes, bytes.Length); this.extMemory.ReadRaw(this.getProcAddressShellPtr, out var outBytes, bytes.Length);
@ -298,33 +292,33 @@ namespace Dalamud.Injector
return exportFunction.Address; return exportFunction.Address;
} }
private IntPtr WriteNullTerminatedASCIIString(string value) private nuint WriteNullTerminatedASCIIString(string value)
{ {
var bytes = Encoding.ASCII.GetBytes(value + '\0'); var bytes = Encoding.ASCII.GetBytes(value + '\0');
var address = this.circularBuffer.Add(bytes); var address = this.circularBuffer.Add(bytes);
if (address == IntPtr.Zero) if (address == 0)
throw new Exception("Unable to write ASCII string to buffer"); throw new Exception("Unable to write ASCII string to buffer");
#if DEBUG #if DEBUG
this.extMemory.ReadRaw(address, out var outBytes, bytes.Length); this.extMemory.ReadRaw(address, out var outBytes, bytes.Length);
Log.Verbose($"WriteASCII: {this.GetResultMarker(Enumerable.SequenceEqual(bytes, outBytes))} 0x{address.ToInt64():X} {value}"); Log.Verbose($"WriteASCII: {this.GetResultMarker(Enumerable.SequenceEqual(bytes, outBytes))} 0x{address:X} {value}");
#endif #endif
return address; return address;
} }
private IntPtr WriteNullTerminatedUnicodeString(string value) private nuint WriteNullTerminatedUnicodeString(string value)
{ {
var bytes = Encoding.Unicode.GetBytes(value + '\0'); var bytes = Encoding.Unicode.GetBytes(value + '\0');
var address = this.circularBuffer.Add(bytes); var address = this.circularBuffer.Add(bytes);
if (address == IntPtr.Zero) if (address == 0)
throw new Exception("Unable to write Unicode string to buffer"); throw new Exception("Unable to write Unicode string to buffer");
#if DEBUG #if DEBUG
this.extMemory.ReadRaw(address, out var outBytes, bytes.Length); this.extMemory.ReadRaw(address, out var outBytes, bytes.Length);
Log.Verbose($"WriteUnicode: {this.GetResultMarker(Enumerable.SequenceEqual(bytes, outBytes))} 0x{address.ToInt64():X} {value}"); Log.Verbose($"WriteUnicode: {this.GetResultMarker(Enumerable.SequenceEqual(bytes, outBytes))} 0x{address:X} {value}");
#endif #endif
return address; return address;
@ -337,15 +331,15 @@ namespace Dalamud.Injector
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
private struct GetProcAddressParams private struct GetProcAddressParams
{ {
public GetProcAddressParams(IntPtr hModule, IntPtr lPProcName) public GetProcAddressParams(IntPtr hModule, nuint lPProcName)
{ {
this.HModule = hModule.ToInt64(); this.HModule = hModule.ToInt64();
this.LPProcName = lPProcName.ToInt64(); this.LPProcName = lPProcName;
} }
public long HModule { get; set; } public long HModule { get; set; }
public long LPProcName { get; set; } public nuint LPProcName { get; set; }
} }
} }
} }

View file

@ -661,8 +661,8 @@ namespace Dalamud.Injector
IntPtr hProcess, IntPtr hProcess,
IntPtr lpThreadAttributes, IntPtr lpThreadAttributes,
UIntPtr dwStackSize, UIntPtr dwStackSize,
IntPtr lpStartAddress, nuint lpStartAddress,
IntPtr lpParameter, nuint lpParameter,
CreateThreadFlags dwCreationFlags, CreateThreadFlags dwCreationFlags,
out uint lpThreadId); out uint lpThreadId);

View file

@ -28,7 +28,6 @@ namespace Dalamud
#region Internals #region Internals
private readonly ManualResetEvent unloadSignal; private readonly ManualResetEvent unloadSignal;
private bool hasDisposedPlugins = false;
#endregion #endregion
@ -117,8 +116,6 @@ namespace Dalamud
/// </summary> /// </summary>
public void DisposePlugins() public void DisposePlugins()
{ {
this.hasDisposedPlugins = true;
// this must be done before unloading interface manager, in order to do rebuild // this must be done before unloading interface manager, in order to do rebuild
// the correct cascaded WndProc (IME -> RawDX11Scene -> Game). Otherwise the game // the correct cascaded WndProc (IME -> RawDX11Scene -> Game). Otherwise the game
// will not receive any windows messages // will not receive any windows messages

View file

@ -10,7 +10,6 @@ using Dalamud.Configuration.Internal;
using Dalamud.Logging.Internal; using Dalamud.Logging.Internal;
using Dalamud.Support; using Dalamud.Support;
using Dalamud.Utility; using Dalamud.Utility;
using ImGuiNET;
using Newtonsoft.Json; using Newtonsoft.Json;
using PInvoke; using PInvoke;
using Serilog; using Serilog;