From 0baa521ba34d2cd4e0f732f83d23b807605786a2 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Sat, 16 Nov 2019 10:27:06 -0800 Subject: More and more progress. Split out functions in serializer Figured out how to allocate memory (for sequences) --- Source/Memory.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'Source/Memory.cpp') diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 98b06f9..bc0725b 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -70,7 +70,6 @@ void Memory::Heartbeat(HWND window) { PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::Running); } - [[nodiscard]] bool Memory::Initialize() { // First, get the handle of the process @@ -106,6 +105,7 @@ bool Memory::Initialize() { std::cerr << "Couldn't locate base address" << std::endl; return false; } + return true; } @@ -161,7 +161,7 @@ void Memory::ThrowError() { } void* Memory::ComputeOffset(std::vector offsets) { - // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. + // Leave off the last offset, since it will be either read/write, and may not be of type uintptr_t. int final_offset = offsets.back(); offsets.pop_back(); @@ -176,6 +176,9 @@ void* Memory::ComputeOffset(std::vector offsets) { if (bool result = !ReadProcessMemory(_handle, reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { ThrowError(); } + if (computedAddress == 0) { // Attempting to dereference a nullptr + ThrowError(); + } _computedAddresses[cumulativeAddress] = computedAddress; } @@ -183,3 +186,21 @@ void* Memory::ComputeOffset(std::vector offsets) { } return reinterpret_cast(cumulativeAddress + final_offset); } + +uintptr_t Memory::Allocate(size_t bytes) { + uintptr_t current = _freeMem; + _freeMem += bytes; + + if (_freeMem > _freeMemEnd) { + // If we don't have enough space at our current location, go allocate some more space. + // Note that the remaining space in our current page is unused. Oh well. + _freeMem = reinterpret_cast(::VirtualAllocEx(_handle, NULL, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + _freeMemEnd = _freeMem + 0x1000; + + current = _freeMem; + _freeMem += bytes; + assert(_freeMem <= _freeMemEnd); // Don't allocate data > 0x1000 at a time. Duh. + } + + return current; +} -- cgit 1.4.1