Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor instruction meta #7

Merged
merged 15 commits into from
Jul 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix 32 bit and refactor some types
  • Loading branch information
ZehMatt committed Jul 15, 2021
commit c3c79208bbc930321ce4bd0a1fa6165c399af403
51 changes: 40 additions & 11 deletions bin/dotplugins/myplugin/AssemblerTest.cs
Original file line number Diff line number Diff line change
@@ -11,8 +11,15 @@ public AssemblerTest()
[Command("TestAssembler")]
public void BasicAssembly(string[] args)
{
using (var asm = new Assembler((nuint)Thread.Active.Rip))
#if _X64_
nuint ip = Thread.Active.Rip;
#else
nuint ip = Thread.Active.Eip;
#endif

using (var asm = new Assembler(ip))
{
#if _X64_
// Create some assembly.
asm
.Mov(R9, R10)
@@ -31,13 +38,24 @@ public void BasicAssembly(string[] args)
.Pop(R10)
;

#else
asm
.Mov(Eax, Edx)
.Shl(Edx, Imm(1))
.Push(Eax)
.Pop(Edx)
.Lea(Esp, Ptr(Esp, -4))
.Xchg(Eax, Edx)
.Ret()
;
#endif
// Serialize the nodes into x86.
asm.Finalize();

// Write into process.
var bytes = asm.GetData();

var bytesWritten = Memory.Write(Thread.Active.Rip, bytes);
var bytesWritten = Memory.Write(ip, bytes);
Console.WriteLine($"Wrote {bytesWritten} bytes");

UI.Disassembly.Update();
@@ -49,9 +67,14 @@ public void BasicAssembly(string[] args)
public void EncodeIntoAssembler(string[] args)
{
var decoder = Decoder.Create();
var asm = new Assembler((nuint)Thread.Active.Rip);

var instr = decoder.Decode(Thread.Active.Rip);
#if _X64_
nuint ip = Thread.Active.Rip;
#else
nuint ip = Thread.Active.Eip;
#endif
var asm = new Assembler(ip);

var instr = decoder.Decode(ip);
asm.Emit(instr);

// Serialize the nodes into x86.
@@ -60,7 +83,7 @@ public void EncodeIntoAssembler(string[] args)
// Write into process.
var bytes = asm.GetData();

var bytesWritten = Memory.Write(Thread.Active.Rip, bytes);
var bytesWritten = Memory.Write(ip, bytes);
Console.WriteLine($"Wrote {bytesWritten} bytes");

UI.Disassembly.Update();
@@ -69,13 +92,19 @@ public void EncodeIntoAssembler(string[] args)
[Command("AssembleWithLabel")]
public void AssemblerWithLabels(string[] args)
{
var asm = new Assembler((nuint)Thread.Active.Rip);
#if _X64_
nuint ip = Thread.Active.Rip;
#else
nuint ip = Thread.Active.Eip;
#endif

var asm = new Assembler(ip);

var myLabel = asm.CreateLabel();

asm.Mov(Rax, Imm(12))
.Xor(Rdx, Rdx)
.Cmp(Rax, Rdx)
asm.Mov(Eax, Imm(12))
.Xor(Edx, Edx)
.Cmp(Eax, Edx)
.Jmp(myLabel)
.Nop()
.Nop()
@@ -89,7 +118,7 @@ public void AssemblerWithLabels(string[] args)
// Write into process.
var bytes = asm.GetData();

var bytesWritten = Memory.Write(Thread.Active.Rip, bytes);
var bytesWritten = Memory.Write(ip, bytes);
Console.WriteLine($"Wrote {bytesWritten} bytes");

UI.Disassembly.Update();
6 changes: 6 additions & 0 deletions bin/dotplugins/myplugin/MyPlugin.Expressions.cs
Original file line number Diff line number Diff line change
@@ -8,7 +8,13 @@ public nuint MyExpr1()
{
var th = Thread.Active;
if (th != null)
{
#if _X64_
return (nuint)th.Rip;
#else
return (nuint)th.Eip;
#endif
}
else
Console.WriteLine("No active thread");

24 changes: 19 additions & 5 deletions bin/dotplugins/myplugin/MyPlugin.cs
Original file line number Diff line number Diff line change
@@ -80,13 +80,20 @@ public void OnHotload()

Thread.SetActive(Thread.GetMain());

#if _X64_
mainThread.Rax = 0xFFFFFFFFFFFFFFFF;
Console.WriteLine($"Rax = {mainThread.Rax:X}");

Console.WriteLine($"Eax = {mainThread.Eax:X}");
mainThread.Eax++;
Console.WriteLine($"Eax = {mainThread.Eax:X}");

#else
mainThread.Eax = 0xFFFFFFF;
Console.WriteLine($"Rax = {mainThread.Eax:X}");
#endif


Console.WriteLine($"Ax = {mainThread.Ax:X}");
mainThread.Ax++;
Console.WriteLine($"Ax = {mainThread.Ax:X}");
@@ -99,11 +106,18 @@ public void OnHotload()
mainThread.Al++;
Console.WriteLine($"Al = {mainThread.Al:X}");

#if _X64_
Console.WriteLine($"Rax = {mainThread.Rax:X}");


var res = Memory.Read(mainThread.Rip, 22);
Console.WriteLine("Data: {0}", res);
#else
Console.WriteLine($"Rax = {mainThread.Eax:X}");
#endif

#if _X64_
var res = Memory.Read(mainThread.Rip, 22);
#else
var res = Memory.Read(mainThread.Eip, 22);
#endif
Console.WriteLine("Data: {0}", res);
}

// Value X and Y have will be 100, 300
25 changes: 13 additions & 12 deletions src/Bindings/Breakpoints.cpp
Original file line number Diff line number Diff line change
@@ -25,40 +25,41 @@ namespace Dotx64Dbg::Native
HardwareExecute
};

static bool SetBreakpoint(duint address)
static bool SetBreakpoint(System::UIntPtr address)
{
return Script::Debug::SetBreakpoint(address);
return Script::Debug::SetBreakpoint(static_cast<duint>(address.ToUInt64()));
}

static bool DeleteBreakpoint(duint address)
static bool DeleteBreakpoint(System::UIntPtr address)
{
return Script::Debug::DeleteBreakpoint(address);
return Script::Debug::DeleteBreakpoint(static_cast<duint>(address.ToUInt64()));
}

static bool DisableBreakpoint(duint address)
static bool DisableBreakpoint(System::UIntPtr address)
{
return Script::Debug::DisableBreakpoint(address);
return Script::Debug::DisableBreakpoint(static_cast<duint>(address.ToUInt64()));
}

static bool SetHardwareBreakpoint(duint address, HardwareType type)
static bool SetHardwareBreakpoint(System::UIntPtr address, HardwareType type)
{
auto va = static_cast<duint>(address.ToUInt64());
switch (type)
{
case HardwareType::HardwareAccess:
return Script::Debug::SetHardwareBreakpoint(address, Script::Debug::HardwareType::HardwareAccess);
return Script::Debug::SetHardwareBreakpoint(va, Script::Debug::HardwareType::HardwareAccess);
case HardwareType::HardwareWrite:
return Script::Debug::SetHardwareBreakpoint(address, Script::Debug::HardwareType::HardwareWrite);
return Script::Debug::SetHardwareBreakpoint(va, Script::Debug::HardwareType::HardwareWrite);
case HardwareType::HardwareExecute:
return Script::Debug::SetHardwareBreakpoint(address, Script::Debug::HardwareType::HardwareExecute);
return Script::Debug::SetHardwareBreakpoint(va, Script::Debug::HardwareType::HardwareExecute);
default:
break;
}
return false;
}

static bool DeleteHardwareBreakpoint(duint address)
static bool DeleteHardwareBreakpoint(System::UIntPtr address)
{
return Script::Debug::DeleteHardwareBreakpoint(address);
return Script::Debug::DeleteHardwareBreakpoint(static_cast<duint>(address.ToUInt64()));
}
};
}
3 changes: 2 additions & 1 deletion src/Bindings/Decoder.cpp
Original file line number Diff line number Diff line change
@@ -83,8 +83,9 @@ namespace Dotx64Dbg {
return Convert(instr, address);
}

Instruction^ Decode(uintptr_t va)
Instruction^ Decode(System::UIntPtr address)
{
auto va = static_cast<duint>(address.ToUInt64());
duint readSize = 0;

uint8_t buf[15];
10 changes: 6 additions & 4 deletions src/Bindings/Encoder.cpp
Original file line number Diff line number Diff line change
@@ -28,9 +28,9 @@ namespace Dotx64Dbg {
delete _code;
}

static Encoder^ Create(uintptr_t baseVA)
static Encoder^ Create(System::UIntPtr baseVA)
{
return gcnew Encoder(baseVA);
return gcnew Encoder(static_cast<uintptr_t>(baseVA.ToUInt64()));
}

void Reset()
@@ -221,9 +221,11 @@ namespace Dotx64Dbg {
return true;
}

bool RelocateTo(uintptr_t newBaseVA)
bool RelocateTo(System::UIntPtr newBaseVA)
{
if (auto res = _code->relocateToBase(newBaseVA); res != asmjit::kErrorOk)
auto va = static_cast<uintptr_t>(newBaseVA.ToUInt64());

if (auto res = _code->relocateToBase(va); res != asmjit::kErrorOk)
{
return false;
}
12 changes: 8 additions & 4 deletions src/Bindings/Memory.cpp
Original file line number Diff line number Diff line change
@@ -16,15 +16,17 @@ namespace Dotx64Dbg::Native
/// <param name="addr">Virtual address in the debugged process space</param>
/// <param name="length">Amount of bytes to read</param>
/// <returns>The bytes read from the process</returns>
static array<System::Byte>^ Read(uintptr_t addr, int length)
static array<System::Byte>^ Read(System::UIntPtr addr, int length)
{
auto va = static_cast<duint>(addr.ToUInt64());

array<System::Byte>^ res = gcnew array<System::Byte>((int)length);

pin_ptr<uint8_t> ptr = &res[0];
uint8_t* buf = ptr;

duint readSize = 0;
if (!Script::Memory::Read(addr, buf, length, &readSize))
if (!Script::Memory::Read(va, buf, length, &readSize))
{
array<System::Byte>::Resize(res, 0);
return res;
@@ -41,8 +43,10 @@ namespace Dotx64Dbg::Native
/// <param name="data">The bytes to be written</param>
/// <param name="length">The maximum amount of bytes to write, can not be bigger than `data`</param>
/// <returns>The amount of bytes written</returns>
static int Write(uintptr_t addr, array<System::Byte>^ data, int length)
static int Write(System::UIntPtr addr, array<System::Byte>^ data, int length)
{
auto va = static_cast<duint>(addr.ToUInt64());

duint maxLength = data->Length < length ? data->Length : length;
if (maxLength <= 0)
return 0;
@@ -51,7 +55,7 @@ namespace Dotx64Dbg::Native
const uint8_t* buf = ptr;

duint bytesWritten = 0;
if (!Script::Memory::Write(addr, buf, maxLength, &bytesWritten))
if (!Script::Memory::Write(va, buf, maxLength, &bytesWritten))
{
return 0;
}
35 changes: 23 additions & 12 deletions src/Bindings/Module.cpp
Original file line number Diff line number Diff line change
@@ -54,43 +54,54 @@ namespace Dotx64Dbg::Native
return Script::Module::BaseFromName(cstr);
}

static duint GetSize(duint base)
static duint GetSize(System::UIntPtr base)
{
return Script::Module::SizeFromAddr(base);
auto va = static_cast<duint>(base.ToUInt64());
return Script::Module::SizeFromAddr(va);
}

static System::String^ GetPath(duint base)
static System::String^ GetPath(System::UIntPtr base)
{
auto va = static_cast<duint>(base.ToUInt64());

char modPath[4096]{};
if (!Script::Module::PathFromAddr(base, modPath))
if (!Script::Module::PathFromAddr(va, modPath))
return nullptr;

return gcnew System::String(modPath);
}

static System::String^ GetName(duint base)
static System::String^ GetName(System::UIntPtr base)
{
auto va = static_cast<duint>(base.ToUInt64());

char modName[4096]{};
if (!Script::Module::NameFromAddr(base, modName))
if (!Script::Module::NameFromAddr(va, modName))
return nullptr;

return gcnew System::String(modName);
}

static duint GetEntrypoint(duint base)
static duint GetEntrypoint(System::UIntPtr base)
{
return Script::Module::EntryFromAddr(base);
auto va = static_cast<duint>(base.ToUInt64());

return Script::Module::EntryFromAddr(va);
}

static int GetSectionCount(duint base)
static int GetSectionCount(System::UIntPtr base)
{
return Script::Module::SectionCountFromAddr(base);
auto va = static_cast<duint>(base.ToUInt64());

return Script::Module::SectionCountFromAddr(va);
}

static Section^ GetSection(duint base, int index)
static Section^ GetSection(System::UIntPtr base, int index)
{
auto va = static_cast<duint>(base.ToUInt64());

Script::Module::ModuleSectionInfo sect{};
if (!Script::Module::SectionFromAddr(base, index, &sect))
if (!Script::Module::SectionFromAddr(va, index, &sect))
return nullptr;

auto res = gcnew Section();
2 changes: 1 addition & 1 deletion src/Dotx64Managed/API/Assembler.cs
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ public class NodeLabel : NodeList.NodeKind<Label>

public Assembler()
{
Encoder = Dotx64Dbg.Encoder.Create(0);
Encoder = Dotx64Dbg.Encoder.Create((nuint)0);
}

public Assembler(nuint baseVA = 0)