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"
|
|
|
|
#include <coreinit/memexpheap.h>
|
|
|
|
#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();
|
|
|
|
|
|
|
|
WUMS_INITIALIZE() {
|
|
|
|
// 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");
|
|
|
|
OSFatal("Failed to acquire coreinit.rpl");
|
|
|
|
}
|
|
|
|
/* Memory allocation functions */
|
|
|
|
uint32_t *allocPtr, *freePtr;
|
|
|
|
/* Memory allocation functions */
|
|
|
|
if (OSDynLoad_FindExport(coreinitModule, true, "MEMAllocFromDefaultHeapEx", reinterpret_cast<void **>(&allocPtr)) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_FindExport for MEMAllocFromDefaultHeapEx");
|
|
|
|
OSFatal("OSDynLoad_FindExport for MEMAllocFromDefaultHeapEx");
|
|
|
|
}
|
|
|
|
if (OSDynLoad_FindExport(coreinitModule, true, "MEMFreeToDefaultHeap", reinterpret_cast<void **>(&freePtr)) != OS_DYNLOAD_OK) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("OSDynLoad_FindExport for MEMFreeToDefaultHeap");
|
|
|
|
OSFatal("OSDynLoad_FindExport for MEMFreeToDefaultHeap");
|
|
|
|
}
|
|
|
|
|
|
|
|
gRealMEMAllocFromDefaultHeapEx = (void *(*) (uint32_t, int) ) * allocPtr;
|
|
|
|
gMEMFreeToDefaultHeap = (void (*)(void *)) * freePtr;
|
|
|
|
OSDynLoad_Release(coreinitModule);
|
|
|
|
|
|
|
|
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");
|
|
|
|
OSFatal("Failed to create heap for jump data");
|
|
|
|
}
|
|
|
|
|
|
|
|
gFunctionAddressProvider = make_shared_nothrow<FunctionAddressProvider>();
|
|
|
|
if (!gFunctionAddressProvider) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to create gFunctionAddressProvider");
|
|
|
|
OSFatal("Failed to create gFunctionAddressProvider");
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:52:59 +01:00
|
|
|
initLogging();
|
2022-05-08 18:51:05 +02:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
|
|
|
|
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Reset patch status");
|
|
|
|
// Reset all dynamic functions
|
|
|
|
for (auto &cur : gPatchedFunctions) {
|
|
|
|
if (cur->isDynamicFunction()) {
|
|
|
|
if (cur->functionName) {
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("%s is dynamic, reset patched status", cur->functionName->c_str());
|
|
|
|
} else {
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("is dynamic, reset patched status");
|
|
|
|
}
|
|
|
|
cur->isPatched = false;
|
|
|
|
} else {
|
|
|
|
if (cur->functionName) {
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Skip %s for targetProcess %d", cur->functionName->c_str(), cur->targetProcess);
|
|
|
|
} else {
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Skip %08X for targetProcess %d", cur->realEffectiveFunctionAddress, cur->targetProcess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OSMemoryBarrier();
|
|
|
|
|
|
|
|
DEBUG_FUNCTION_LINE_VERBOSE("Patch all functions");
|
|
|
|
for (auto &cur : gPatchedFunctions) {
|
|
|
|
PatchFunction(cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
OSDynLoad_AddNotifyCallback(notify_callback, nullptr);
|
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);
|
2022-05-08 18:51:05 +02:00
|
|
|
WUMS_EXPORT_FUNCTION(FunctionPatcherRestoreFunction);
|