fix: copy the whole module

This commit is contained in:
goat 2020-01-26 17:45:53 +09:00
parent e2f9773e33
commit 09e88f42cf

View file

@ -27,10 +27,14 @@ namespace Dalamud.Game {
public bool Is32BitProcess { get; } public bool Is32BitProcess { get; }
public IntPtr TextSectionBase { get; private set; } public IntPtr SearchBase => IsCopy ? this.moduleCopyPtr : Module.BaseAddress;
public IntPtr TextSectionBase => new IntPtr(SearchBase.ToInt64() + TextSectionOffset);
public long TextSectionOffset { get; private set; }
public int TextSectionSize { get; private set; } public int TextSectionSize { get; private set; }
public IntPtr DataSectionBase { get; private set; } public IntPtr DataSectionBase => new IntPtr(SearchBase.ToInt64() + DataSectionOffset);
public long DataSectionOffset { get; private set; }
public int DataSectionSize { get; private set; } public int DataSectionSize { get; private set; }
public ProcessModule Module { get; } public ProcessModule Module { get; }
@ -65,11 +69,11 @@ namespace Dalamud.Game {
// .text // .text
switch (sectionName) { switch (sectionName) {
case 0x747865742E: // .text case 0x747865742E: // .text
TextSectionBase = baseAddress + Marshal.ReadInt32(sectionCursor, 12); TextSectionOffset = Marshal.ReadInt32(sectionCursor, 12);
TextSectionSize = Marshal.ReadInt32(sectionCursor, 8); TextSectionSize = Marshal.ReadInt32(sectionCursor, 8);
break; break;
case 0x617461642E: // .data case 0x617461642E: // .data
DataSectionBase = baseAddress + Marshal.ReadInt32(sectionCursor, 12); DataSectionOffset = Marshal.ReadInt32(sectionCursor, 12);
DataSectionSize = Marshal.ReadInt32(sectionCursor, 8); DataSectionSize = Marshal.ReadInt32(sectionCursor, 8);
break; break;
} }
@ -78,58 +82,56 @@ namespace Dalamud.Game {
} }
} }
private IntPtr textCopyPtr; private IntPtr moduleCopyPtr;
private IntPtr dataCopyPtr; private long moduleCopyOffset;
private unsafe void SetupCopiedSegments() { private unsafe void SetupCopiedSegments() {
Log.Verbose("text copy START"); Log.Verbose("module copy START");
// .text // .text
this.textCopyPtr = Marshal.AllocHGlobal(TextSectionSize); this.moduleCopyPtr = Marshal.AllocHGlobal(Module.ModuleMemorySize);
Log.Verbose($"Alloc: {this.textCopyPtr.ToInt64():x}"); Log.Verbose($"Alloc: {this.moduleCopyPtr.ToInt64():x}");
Buffer.MemoryCopy(TextSectionBase.ToPointer(), this.textCopyPtr.ToPointer(), TextSectionSize, Buffer.MemoryCopy(Module.BaseAddress.ToPointer(), this.moduleCopyPtr.ToPointer(), Module.ModuleMemorySize,
TextSectionSize); Module.ModuleMemorySize);
Log.Verbose("data copy START"); this.moduleCopyOffset = this.moduleCopyPtr.ToInt64() - Module.BaseAddress.ToInt64();
// .data
this.dataCopyPtr = Marshal.AllocHGlobal(DataSectionSize);
Buffer.MemoryCopy(DataSectionBase.ToPointer(), this.dataCopyPtr.ToPointer(), DataSectionSize,
DataSectionSize);
Log.Verbose("copy OK!"); Log.Verbose("copy OK!");
} }
public void Dispose() { public void Dispose() {
Marshal.FreeHGlobal(this.textCopyPtr); Marshal.FreeHGlobal(this.moduleCopyPtr);
Marshal.FreeHGlobal(this.dataCopyPtr);
} }
public IntPtr ScanText(string signature) { public IntPtr ScanText(string signature) {
var mBase = IsCopy ? this.textCopyPtr : TextSectionBase; var mBase = IsCopy ? this.moduleCopyPtr : TextSectionBase;
var scanRet = Scan(mBase, TextSectionSize, signature); var scanRet = Scan(mBase, TextSectionSize, signature);
return IsCopy if (IsCopy)
? (new IntPtr(scanRet.ToInt64() - (this.textCopyPtr.ToInt64() - TextSectionBase.ToInt64()))) scanRet = new IntPtr(scanRet.ToInt64() - this.moduleCopyOffset);
: scanRet;
return scanRet;
} }
public IntPtr ScanData(string signature) { public IntPtr ScanData(string signature) {
var mBase = IsCopy ? this.dataCopyPtr : DataSectionBase;
var scanRet = Scan(DataSectionBase, DataSectionSize, signature); var scanRet = Scan(DataSectionBase, DataSectionSize, signature);
return IsCopy if (IsCopy)
? (new IntPtr(scanRet.ToInt64() - (this.textCopyPtr.ToInt64() - TextSectionBase.ToInt64()))) scanRet = new IntPtr(scanRet.ToInt64() - this.moduleCopyOffset);
: scanRet;
return scanRet;
} }
public IntPtr ScanModule(string signature) { public IntPtr ScanModule(string signature) {
// TODO: This does not respect the copy flag. var scanRet = Scan(SearchBase, Module.ModuleMemorySize, signature);
return Scan(Module.BaseAddress, Module.ModuleMemorySize, signature);
if (IsCopy)
scanRet = new IntPtr(scanRet.ToInt64() - this.moduleCopyOffset);
return scanRet;
} }
public IntPtr Scan(IntPtr baseAddress, int size, string signature) { public IntPtr Scan(IntPtr baseAddress, int size, string signature) {
Log.Verbose($"Scan at {baseAddress.ToInt64():x} with {size:x} for {signature}");
var needle = SigToNeedle(signature); var needle = SigToNeedle(signature);
unsafe { unsafe {