Skip to content

Commit

Permalink
yeah someone has to cleanup this shit
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Jun 18, 2024
1 parent 9d07ec8 commit 6a86cd5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/assembler/X86Assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void X86Assembler::encodeModRM(X86Operand op, uint8_t digit) {
}

void X86Assembler::add(X86Register reg, int32_t value) {
if (value > -0x80 && value < 0x7f) {
if (value >= -0x80 && value <= 0x7f) {
this->write8(0x83);
this->write8(0xC0 | regIdx(reg));
this->write8(value);
Expand All @@ -96,7 +96,7 @@ void X86Assembler::add(X86Register reg, int32_t value) {
}

void X86Assembler::sub(X86Register reg, int32_t value) {
if (value > -0x80 && value < 0x7f) {
if (value >= -0x80 && value <= 0x7f) {
this->write8(0x83);
this->write8(0xE8 | regIdx(reg));
this->write8(value);
Expand Down
8 changes: 4 additions & 4 deletions src/convention/Windows64Convention.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void Windows64Convention::generateDefaultCleanup(BaseAssembler& a_, AbstractFunc
auto& a = static_cast<X64Assembler&>(a_);
using enum X64Register;

size_t paddedSize = getPaddedStackParamSize(function);
a.add(RSP, paddedSize + 0x20);
// size_t paddedSize = getPaddedStackParamSize(function);
// a.add(RSP, paddedSize + 0x20);
}

void Windows64Convention::generateIntoDefault(BaseAssembler& a_, AbstractFunction const& function) {
Expand All @@ -48,8 +48,8 @@ void Windows64Convention::generateIntoDefault(BaseAssembler& a_, AbstractFunctio
size_t stackParamSize = getStackParamSize(function);
auto const paddedSize = (stackParamSize % 16) ? stackParamSize + 8 : stackParamSize;
// + 0x20 for the shadow space before the first arg
a.sub(RSP, paddedSize + 0x20);
a.label("convention-alloc-small");
// a.sub(RSP, paddedSize + 0x20);
// a.label("convention-alloc-small");
if (stackParamSize > 0) {
// theres stack args, so we need to copy them over

Expand Down
67 changes: 40 additions & 27 deletions src/generator/X64Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ Result<FunctionData> X64HandlerGenerator::generateHandler() {
constexpr auto FIRST_PARAM = RDI;
#endif

for (size_t i = 0; i < 8; ++i) {
a.nop();
}
// for (size_t i = 0; i < 8; ++i) {
// a.nop();
// }

a.push(RBP);
a.label("handler-push");
Expand Down Expand Up @@ -240,8 +240,8 @@ Result<FunctionData> X64HandlerGenerator::generateHandler() {

auto const pushOffset = reinterpret_cast<uint64_t>(a.getLabel("handler-push")) & 0xffff;
auto const allocOffset = reinterpret_cast<uint64_t>(a.getLabel("handler-alloc-mid")) & 0xffff;
auto const conventionOffset = reinterpret_cast<uint64_t>(a.getLabel("convention-alloc-small")) & 0xffff;
auto const prologSize = conventionOffset;
// auto const conventionOffset = reinterpret_cast<uint64_t>(a.getLabel("convention-alloc-small")) & 0xffff;
auto const prologSize = allocOffset;


// RUNTIME_FUNCTION
Expand All @@ -257,19 +257,19 @@ Result<FunctionData> X64HandlerGenerator::generateHandler() {
0x0 // Flags : 5
);
a.write8(prologSize); // SizeOfProlog
a.write8(4); // CountOfUnwindCodes
a.write8(3); // CountOfUnwindCodes
a.write8(
0x0 | // FrameRegister : 4
0x0 // FrameOffset : 4
);
// UNWIND_CODE[]

auto padded = 0x20 + getPaddedStackParamSize(m_metadata.m_abstract);
a.write8(conventionOffset); // CodeOffset
a.write8(
(((padded >> 3) - 1) << 4) | // UnwindOp : 4
0x2 // OpInfo : 4
);
// auto padded = 0x20 + getPaddedStackParamSize(m_metadata.m_abstract);
// a.write8(conventionOffset); // CodeOffset
// a.write8(
// (((padded >> 3) - 1) << 4) | // UnwindOp : 4
// 0x2 // OpInfo : 4
// );

a.write8(allocOffset); // CodeOffset
a.write8(
Expand Down Expand Up @@ -476,6 +476,10 @@ Result<FunctionData> X64WrapperGenerator::generateWrapper() {
a.label("wrapper-push");
a.mov(RBP, RSP);

// shadow space
a.sub(RSP, 0xc0);
a.label("wrapper-alloc-mid");

m_metadata.m_convention->generateIntoOriginal(a, m_metadata.m_abstract);

auto difference = a.currentAddress() - reinterpret_cast<int64_t>(m_address) + 5;
Expand Down Expand Up @@ -507,8 +511,9 @@ Result<FunctionData> X64WrapperGenerator::generateWrapper() {
auto const offsetEnd = (address + a.m_buffer.size()) & 0xffff;

auto const pushOffset = reinterpret_cast<uint64_t>(a.getLabel("wrapper-push")) & 0xffff;
auto const conventionOffset = reinterpret_cast<uint64_t>(a.getLabel("convention-alloc-small")) & 0xffff;
auto const prologSize = conventionOffset;
auto const allocOffset = reinterpret_cast<uint64_t>(a.getLabel("wrapper-alloc-mid")) & 0xffff;
// auto const conventionOffset = reinterpret_cast<uint64_t>(a.getLabel("convention-alloc-small")) & 0xffff;
auto const prologSize = allocOffset;


// RUNTIME_FUNCTION
Expand All @@ -524,26 +529,25 @@ Result<FunctionData> X64WrapperGenerator::generateWrapper() {
0x0 // Flags : 5
);
a.write8(prologSize); // SizeOfProlog
a.write8(2); // CountOfUnwindCodes
a.write8(3); // CountOfUnwindCodes
a.write8(
0x0 | // FrameRegister : 4
0x0 // FrameOffset : 4
);
// UNWIND_CODE[]

auto padded = 0x20 + getPaddedStackParamSize(m_metadata.m_abstract);
a.write8(conventionOffset); // CodeOffset
a.write8(allocOffset); // CodeOffset
a.write8(
(((padded >> 3) - 1) << 4) | // UnwindOp : 4
0x2 // OpInfo : 4
0x0 | // UnwindOp : 4
0x1 // OpInfo : 4
);
a.write16(0xc0 >> 3); // UWOP_ALLOC_LARGE continuation

a.write8(pushOffset); // CodeOffset
a.write8(
0x50 | // UnwindOp : 4
0x0 // OpInfo : 4
);

}

#endif
Expand Down Expand Up @@ -580,11 +584,19 @@ Result<FunctionData> X64HandlerGenerator::generateTrampoline(uint64_t target) {
a.push(RBP);
a.label("trampoline-push");
a.mov(RBP, RSP);

// shadow space
a.sub(RSP, 0xc0);
a.label("trampoline-alloc-mid");

m_metadata.m_convention->generateIntoOriginal(a, m_metadata.m_abstract);

a.call("relocated");

m_metadata.m_convention->generateOriginalCleanup(a, m_metadata.m_abstract);

a.add(RSP, 0xc0);

a.pop(RBP);
a.ret();
}
Expand Down Expand Up @@ -622,8 +634,9 @@ Result<FunctionData> X64HandlerGenerator::generateTrampoline(uint64_t target) {
auto const offsetEnd = (address + a.m_buffer.size()) & 0xffff;

auto const pushOffset = reinterpret_cast<uint64_t>(a.getLabel("trampoline-push")) & 0xffff;
auto const conventionOffset = reinterpret_cast<uint64_t>(a.getLabel("convention-alloc-small")) & 0xffff;
auto const prologSize = conventionOffset;
auto const allocOffset = reinterpret_cast<uint64_t>(a.getLabel("trampoline-alloc-mid")) & 0xffff;
// auto const conventionOffset = reinterpret_cast<uint64_t>(a.getLabel("convention-alloc-small")) & 0xffff;
auto const prologSize = allocOffset;


// RUNTIME_FUNCTION
Expand All @@ -639,19 +652,19 @@ Result<FunctionData> X64HandlerGenerator::generateTrampoline(uint64_t target) {
0x0 // Flags : 5
);
a.write8(prologSize); // SizeOfProlog
a.write8(2); // CountOfUnwindCodes
a.write8(3); // CountOfUnwindCodes
a.write8(
0x0 | // FrameRegister : 4
0x0 // FrameOffset : 4
);
// UNWIND_CODE[]

auto padded = 0x20 + getPaddedStackParamSize(m_metadata.m_abstract);
a.write8(conventionOffset); // CodeOffset
a.write8(allocOffset); // CodeOffset
a.write8(
(((padded >> 3) - 1) << 4) | // UnwindOp : 4
0x2 // OpInfo : 4
0x0 | // UnwindOp : 4
0x1 // OpInfo : 4
);
a.write16(0xc0 >> 3); // UWOP_ALLOC_LARGE continuation

a.write8(pushOffset); // CodeOffset
a.write8(
Expand Down
38 changes: 38 additions & 0 deletions src/target/Windows64Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,44 @@ Target& Target::get() {
return ret;
}

PVOID CustomFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase) {
for (auto& [handle, handler] : Pool::get().m_handlers) {
auto handlerBegin = reinterpret_cast<uintptr_t>(handler->m_handler);
auto handlerEnd = handlerBegin + handler->m_handlerSize;

auto tramplineBegin = reinterpret_cast<uintptr_t>(handler->m_trampoline);
auto tramplineEnd = tramplineBegin + handler->m_trampolineSize;

if (AddrBase >= handlerBegin && AddrBase < handlerEnd) {
// std::stringstream ss;
// ss << "Control PC: " << std::hex << AddrBase << " Handler Begin: " << handlerBegin << " Handler End: " << handlerEnd;
// MessageBoxA(nullptr, ss.str().c_str(), "Error Loading Geode", MB_ICONERROR);
return reinterpret_cast<PVOID>(handlerEnd);
}

if (AddrBase >= tramplineBegin && AddrBase < tramplineEnd) {
// std::stringstream ss;
// ss << "Control PC: " << std::hex << AddrBase << " Trampline Begin: " << tramplineBegin << " Trampline End: " << tramplineEnd;
// MessageBoxA(nullptr, ss.str().c_str(), "Error Loading Geode", MB_ICONERROR);
return reinterpret_cast<PVOID>(tramplineEnd);
}
}

for (auto& [handle, wrapper] : Wrapper::get().m_wrappers) {
auto wrapperBegin = reinterpret_cast<uintptr_t>(wrapper.m_address);
auto wrapperEnd = wrapperBegin + wrapper.m_size;

if (AddrBase >= wrapperBegin && AddrBase < wrapperEnd) {
// std::stringstream ss;
// ss << "Control PC: " << std::hex << AddrBase << " Wrapper Begin: " << wrapperBegin << " Wrapper End: " << wrapperEnd;
// MessageBoxA(nullptr, ss.str().c_str(), "Error Loading Geode", MB_ICONERROR);
return reinterpret_cast<PVOID>(wrapperEnd);
}
}

return nullptr;
}

Result<> Windows64Target::allocatePage() {
m_allocatedPage = VirtualAlloc(nullptr, 0x10000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READ);

Expand Down

0 comments on commit 6a86cd5

Please sign in to comment.