geode/loader/src/core/Windows.cpp

82 lines
2.1 KiB
C++
Raw Normal View History

2022-07-30 12:24:03 -04:00
#include "Windows.hpp"
2022-10-30 14:56:36 -04:00
2022-07-30 12:24:03 -04:00
#include "Core.hpp"
#ifdef GEODE_IS_WINDOWS
2022-10-30 14:56:36 -04:00
#include <Windows.h>
2022-07-30 12:24:03 -04:00
using namespace geode::core::hook;
using namespace geode::core::impl;
namespace {
LONG WINAPI signalHandler(EXCEPTION_POINTERS* info) {
2022-10-30 14:56:36 -04:00
#if defined(_WIN64)
2022-07-30 12:24:03 -04:00
auto current = reinterpret_cast<void*>(info->ContextRecord->Rip);
#elif defined(_WIN32)
auto current = reinterpret_cast<void*>(info->ContextRecord->Eip);
#endif
// handleContext(reinterpret_cast<void*>(info), current);
return EXCEPTION_CONTINUE_EXECUTION;
}
}
void* Windows::allocateVM(size_t size) {
2022-10-30 14:56:36 -04:00
return VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
2022-07-30 12:24:03 -04:00
}
std::vector<std::byte> Windows::jump(void* from, void* to) {
2022-10-30 14:56:36 -04:00
std::vector<std::byte> ret;
2022-07-30 12:24:03 -04:00
ret.resize(5, std::byte(0u));
2022-10-30 14:56:36 -04:00
ret[0] = std::byte(0xe9);
2022-07-30 12:24:03 -04:00
// target - src - 5
intptr_t offset = reinterpret_cast<intptr_t>(to) - reinterpret_cast<intptr_t>(from) - 5;
std::byte data[4];
std::memcpy(data, &offset, 4);
ret[1] = data[0];
ret[2] = data[1];
ret[3] = data[2];
ret[4] = data[3];
2022-10-30 14:56:36 -04:00
return ret;
2022-07-30 12:24:03 -04:00
}
bool Windows::enableSingleStep(void* vcontext) {
2022-10-30 14:56:36 -04:00
auto info = reinterpret_cast<EXCEPTION_POINTERS*>(vcontext);
#if defined(_WIN64)
2022-07-30 12:24:03 -04:00
info->ContextRecord->RFlags |= ((QWORD)0x100);
2022-10-30 14:56:36 -04:00
#elif defined(_WIN32)
2022-07-30 12:24:03 -04:00
info->ContextRecord->EFlags |= ((DWORD)0x100);
2022-10-30 14:56:36 -04:00
#endif
return true;
2022-07-30 12:24:03 -04:00
}
bool Windows::disableSingleStep(void* vcontext) {
2022-10-30 14:56:36 -04:00
auto info = reinterpret_cast<EXCEPTION_POINTERS*>(vcontext);
#if defined(_WIN64)
2022-07-30 12:24:03 -04:00
info->ContextRecord->RFlags &= ~((QWORD)0x100);
2022-10-30 14:56:36 -04:00
#elif defined(_WIN32)
2022-07-30 12:24:03 -04:00
info->ContextRecord->EFlags &= ~((DWORD)0x100);
2022-10-30 14:56:36 -04:00
#endif
return true;
2022-07-30 12:24:03 -04:00
}
bool Windows::writeMemory(void* to, void* from, size_t size) {
DWORD old;
VirtualProtect(to, size, PAGE_EXECUTE_READWRITE, &old);
auto res = WriteProcessMemory(GetCurrentProcess(), to, from, size, nullptr);
VirtualProtect(to, size, old, &old);
return res;
}
bool Windows::initialize() {
// return AddVectoredExceptionHandler(true, signalHandler);
return true;
}
#endif