2022-05-08 18:51:05 +02:00
|
|
|
#include "FunctionAddressProvider.h"
|
|
|
|
#include "export.h"
|
2020-06-06 22:15:47 +02:00
|
|
|
#include "function_patcher.h"
|
2022-05-08 18:51:05 +02:00
|
|
|
#include "utils/globals.h"
|
2022-05-07 23:20:46 +02:00
|
|
|
#include "utils/logger.h"
|
2022-05-08 18:51:05 +02:00
|
|
|
#include "utils/utils.h"
|
2022-07-26 23:11:47 +02:00
|
|
|
#include <coreinit/memdefaultheap.h>
|
2022-05-08 18:51:05 +02:00
|
|
|
#include <coreinit/memexpheap.h>
|
2023-01-02 14:47:19 +01:00
|
|
|
#include <ranges>
|
|
|
|
#include <set>
|
2022-05-08 18:51:05 +02:00
|
|
|
#include <wums.h>
|
2021-09-24 14:49:20 +02:00
|
|
|
|
2020-06-06 22:15:47 +02:00
|
|
|
WUMS_MODULE_EXPORT_NAME("homebrew_functionpatcher");
|
2022-05-08 18:51:05 +02:00
|
|
|
WUMS_MODULE_INIT_BEFORE_RELOCATION_DONE_HOOK();
|
2023-01-07 23:31:19 +01:00
|
|
|
WUMS_DEPENDS_ON(homebrew_kernel);
|
2022-05-08 18:51:05 +02:00
|
|
|
|
2022-07-26 23:11:47 +02:00
|
|
|
void UpdateFunctionPointer() {
|
2022-05-08 18:51:05 +02:00
|
|
|
// We need the real MEMAllocFromDefaultHeapEx/MEMFreeToDefaultHeap function pointer to force-allocate memory on the default heap.
|
|
|
|
// Our custom heap doesn't work (yet) for threads and causes an app panic.
|
|
|
|
OSDynLoad_Module coreinitModule;
|
|
|
|
if (OSDynLoad_Acquire("coreinit", &coreinitModule) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to acquire coreinit.rpl");
|
2023-01-06 14:15:59 +01:00
|
|
|
OSFatal("FunctionPatcherModule: Failed to acquire coreinit.rpl");
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
/* Memory allocation functions */
|
|
|
|
uint32_t *allocPtr, *freePtr;
|
|
|
|
if (OSDynLoad_FindExport(coreinitModule, true, "MEMAllocFromDefaultHeapEx", reinterpret_cast<void **>(&allocPtr)) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_FindExport for MEMAllocFromDefaultHeapEx");
|
2023-01-06 14:15:59 +01:00
|
|
|
OSFatal("FunctionPatcherModule: OSDynLoad_FindExport for MEMAllocFromDefaultHeapEx");
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
if (OSDynLoad_FindExport(coreinitModule, true, "MEMFreeToDefaultHeap", reinterpret_cast<void **>(&freePtr)) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_FindExport for MEMFreeToDefaultHeap");
|
2023-01-06 14:15:59 +01:00
|
|
|
OSFatal("FunctionPatcherModule: OSDynLoad_FindExport for MEMFreeToDefaultHeap");
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 23:11:47 +02:00
|
|
|
gMEMAllocFromDefaultHeapExForThreads = (void *(*) (uint32_t, int) ) * allocPtr;
|
|
|
|
gMEMFreeToDefaultHeapForThreads = (void (*)(void *)) * freePtr;
|
|
|
|
|
2022-05-08 18:51:05 +02:00
|
|
|
OSDynLoad_Release(coreinitModule);
|
2022-07-26 23:11:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 14:47:19 +01:00
|
|
|
void CheckIfPatchedFunctionsAreStillInMemory() {
|
|
|
|
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
|
|
|
|
// Check if rpl has been unloaded by comparing the instruction.
|
|
|
|
std::set<uint32_t> physicalAddressesUnchanged;
|
|
|
|
std::set<uint32_t> physicalAddressesChanged;
|
|
|
|
// Restore function patches that were done after the patch we actually want to restore.
|
|
|
|
for (auto &cur : std::ranges::reverse_view(gPatchedFunctions)) {
|
|
|
|
if (!cur->isPatched || physicalAddressesUnchanged.contains(cur->realPhysicalFunctionAddress)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (physicalAddressesChanged.contains(cur->realPhysicalFunctionAddress)) {
|
|
|
|
cur->isPatched = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if patched instruction is still loaded.
|
|
|
|
uint32_t currentInstruction;
|
|
|
|
if (!ReadFromPhysicalAddress(cur->realPhysicalFunctionAddress, ¤tInstruction)) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to read instruction.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentInstruction == cur->replaceWithInstruction) {
|
|
|
|
physicalAddressesUnchanged.insert(cur->realPhysicalFunctionAddress);
|
|
|
|
} else {
|
|
|
|
cur->isPatched = false;
|
|
|
|
physicalAddressesChanged.insert(cur->realPhysicalFunctionAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 14:47:19 +02:00
|
|
|
|
2022-07-26 23:11:47 +02:00
|
|
|
WUMS_INITIALIZE() {
|
|
|
|
UpdateFunctionPointer();
|
2022-05-08 18:51:05 +02:00
|
|
|
|
|
|
|
memset(gJumpHeapData, 0, JUMP_HEAP_DATA_SIZE);
|
|
|
|
gJumpHeapHandle = MEMCreateExpHeapEx((void *) (gJumpHeapData), JUMP_HEAP_DATA_SIZE, 1);
|
|
|
|
if (gJumpHeapHandle == nullptr) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to create heap for jump data");
|
2023-01-06 14:15:59 +01:00
|
|
|
OSFatal("FunctionPatcherModule: Failed to create heap for jump data");
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gFunctionAddressProvider = make_shared_nothrow<FunctionAddressProvider>();
|
|
|
|
if (!gFunctionAddressProvider) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to create gFunctionAddressProvider");
|
2023-01-06 14:15:59 +01:00
|
|
|
OSFatal("FunctionPatcherModule: Failed to create gFunctionAddressProvider");
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 22:15:47 +02:00
|
|
|
|
2022-05-08 18:51:05 +02:00
|
|
|
void notify_callback(OSDynLoad_Module module,
|
|
|
|
void *userContext,
|
|
|
|
OSDynLoad_NotifyReason reason,
|
|
|
|
OSDynLoad_NotifyData *infos) {
|
|
|
|
if (reason == OS_DYNLOAD_NOTIFY_LOADED) {
|
|
|
|
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
|
|
|
|
for (auto &cur : gPatchedFunctions) {
|
|
|
|
PatchFunction(cur);
|
|
|
|
}
|
2023-01-02 16:24:33 +01:00
|
|
|
} else if (reason == OS_DYNLOAD_NOTIFY_UNLOADED) {
|
|
|
|
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
|
|
|
|
auto library = gFunctionAddressProvider->getTypeForHandle(module);
|
|
|
|
if (library != LIBRARY_OTHER) {
|
|
|
|
for (auto &cur : gPatchedFunctions) {
|
|
|
|
if (cur->library == library) {
|
|
|
|
cur->isPatched = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-02 16:24:38 +01:00
|
|
|
gFunctionAddressProvider->resetHandle(module);
|
2023-01-02 16:24:33 +01:00
|
|
|
CheckIfPatchedFunctionsAreStillInMemory();
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-12 23:07:09 +01:00
|
|
|
|
2020-06-06 22:15:47 +02:00
|
|
|
WUMS_APPLICATION_STARTS() {
|
2022-05-08 18:51:05 +02:00
|
|
|
uint32_t upid = OSGetUPID();
|
|
|
|
if (upid != 2 && upid != 15) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-04 22:34:03 +01:00
|
|
|
OSReport("Running FunctionPatcherModule " MODULE_VERSION_FULL "\n");
|
|
|
|
|
2022-07-26 23:11:47 +02:00
|
|
|
// Now we can update the pointer with the "real" functions
|
|
|
|
gMEMAllocFromDefaultHeapExForThreads = MEMAllocFromDefaultHeapEx;
|
|
|
|
gMEMFreeToDefaultHeapForThreads = MEMFreeToDefaultHeap;
|
|
|
|
|
2022-01-24 18:52:59 +01:00
|
|
|
initLogging();
|
2023-01-02 14:47:19 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
|
|
|
|
// reset function patch status if the rpl they were patching has been unloaded from memory.
|
|
|
|
CheckIfPatchedFunctionsAreStillInMemory();
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Patch all functions");
|
2022-08-19 14:47:19 +02:00
|
|
|
for (auto &cur : gPatchedFunctions) {
|
2023-01-02 14:47:19 +01:00
|
|
|
PatchFunction(cur);
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 14:47:19 +01:00
|
|
|
OSMemoryBarrier();
|
|
|
|
OSDynLoad_AddNotifyCallback(notify_callback, nullptr);
|
2022-05-08 18:51:05 +02:00
|
|
|
}
|
2022-01-12 23:07:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WUMS_APPLICATION_REQUESTS_EXIT() {
|
2022-01-24 18:52:59 +01:00
|
|
|
deinitLogging();
|
2020-06-06 22:15:47 +02:00
|
|
|
}
|
2022-05-08 18:51:05 +02:00
|
|
|
WUMS_APPLICATION_ENDS() {
|
|
|
|
gFunctionAddressProvider->resetHandles();
|
|
|
|
}
|
2020-06-06 22:15:47 +02:00
|
|
|
|
|
|
|
WUMS_EXPORT_FUNCTION(FunctionPatcherPatchFunction);
|
2023-01-06 14:15:59 +01:00
|
|
|
WUMS_EXPORT_FUNCTION(FunctionPatcherRestoreFunction);
|