Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ jobs:
clang-format:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- name: clang-format
run: |
docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./source
build-binary:
runs-on: ubuntu-22.04
needs: clang-format
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- name: create version.h
run: |
git_hash=$(git rev-parse --short "$GITHUB_SHA")
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ jobs:
clang-format:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- name: clang-format
run: |
docker run --rm -v ${PWD}:/src ghcr.io/wiiu-env/clang-format:13.0.0-2 -r ./source
check-build-with-logging:
runs-on: ubuntu-22.04
needs: clang-format
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- name: build binary with logging
run: |
docker build . -t builder
Expand All @@ -25,7 +25,7 @@ jobs:
runs-on: ubuntu-22.04
needs: clang-format
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- name: create version.h
run: |
git_hash=$(git rev-parse --short "${{ github.event.pull_request.head.sha }}")
Expand Down
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM ghcr.io/wiiu-env/devkitppc:20240505
FROM ghcr.io/wiiu-env/devkitppc:20260204

COPY --from=ghcr.io/wiiu-env/libkernel:20230621 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20241012-5173f86 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/wiiumodulesystem:20240424 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libkernel:20260208 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20260208 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/wiiumodulesystem:20260126 /artifacts $DEVKITPRO

WORKDIR project
WORKDIR /project
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ INCLUDES := source
#-------------------------------------------------------------------------------
# options for code generation
#-------------------------------------------------------------------------------
CFLAGS := -Wall -Os -ffunction-sections\
CFLAGS := -Wall -Wextra -Werror -Os -ffunction-sections\
$(MACHDEP)

CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__
Expand Down
2 changes: 1 addition & 1 deletion source/FunctionAddressProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ uint32_t FunctionAddressProvider::getEffectiveAddressOfFunction(function_replace
err = OSDynLoad_IsModuleLoaded((char *) rplHandle.rplname, &rplHandle.handle);
}
if (err != OS_DYNLOAD_OK || !rplHandle.handle) {
DEBUG_FUNCTION_LINE_VERBOSE("%s is not loaded yet", rplHandle.rplname, err, rplHandle.handle);
DEBUG_FUNCTION_LINE_VERBOSE("%s is not loaded yet. Err %d for handle %p", rplHandle.rplname, err, rplHandle.handle);
return 0;
}
rpl_handle = rplHandle.handle;
Expand Down
9 changes: 6 additions & 3 deletions source/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#include "PatchedFunctionData.h"
#include "function_patcher.h"
#include "utils/globals.h"

#include <mutex>
#include <ranges>
#include <vector>

#include <wums/exports.h>

WUT_CHECK_OFFSET(function_replacement_data_v2_t, 0x00, VERSION);
Expand Down Expand Up @@ -50,7 +53,7 @@ FunctionPatcherStatus FPAddFunctionPatch(function_replacement_data_t *function_d
}

{
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard lock(gPatchedFunctionsMutex);
gPatchedFunctions.push_back(std::move(functionData));

OSMemoryBarrier();
Expand All @@ -64,7 +67,7 @@ bool FunctionPatcherPatchFunction(function_replacement_data_t *function_data, Pa
}

FunctionPatcherStatus FPRemoveFunctionPatch(PatchedFunctionHandle handle) {
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard lock(gPatchedFunctionsMutex);
std::vector<std::shared_ptr<PatchedFunctionData>> toBeTempRestored;
bool found = false;
int32_t erasePosition = 0;
Expand Down Expand Up @@ -132,7 +135,7 @@ FunctionPatcherStatus FPIsFunctionPatched(PatchedFunctionHandle handle, bool *ou
if (outIsFunctionPatched == nullptr) {
return FUNCTION_PATCHER_RESULT_INVALID_ARGUMENT;
}
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard lock(gPatchedFunctionsMutex);
for (auto &cur : gPatchedFunctions) {
if (cur->getHandle() == handle) {
*outIsFunctionPatched = cur->isPatched;
Expand Down
18 changes: 18 additions & 0 deletions source/function_patcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
#include "utils/CThread.h"
#include "utils/logger.h"
#include "utils/utils.h"

#include <coreinit/cache.h>
#include <coreinit/debug.h>
#include <coreinit/memorymap.h>

#include <kernel/kernel.h>

#include <memory>
#include <mutex>

static void writeDataAndFlushIC(CThread *thread, void *arg) {
(void) thread;
auto *data = (PatchedFunctionData *) arg;

uint32_t replace_instruction = data->replaceWithInstruction;
Expand All @@ -21,6 +26,19 @@ static void writeDataAndFlushIC(CThread *thread, void *arg) {

auto replace_instruction_physical = (uint32_t) &replace_instruction;

if (data->jumpData) {
DCFlushRange(data->jumpData, data->jumpDataSize * sizeof(uint32_t));
ICInvalidateRange(data->jumpData, data->jumpDataSize * sizeof(uint32_t));
}
if (data->jumpToOriginal) {
DCFlushRange(data->jumpToOriginal, 5 * sizeof(uint32_t));
ICInvalidateRange(data->jumpToOriginal, 5 * sizeof(uint32_t));
}
if (data->realCallFunctionAddressPtr) {
DCFlushRange(data->realCallFunctionAddressPtr, sizeof(uint32_t));
ICInvalidateRange(data->realCallFunctionAddressPtr, sizeof(uint32_t));
}

if (replace_instruction_physical < 0x00800000 || replace_instruction_physical >= 0x01000000) {
replace_instruction_physical = OSEffectiveToPhysical(replace_instruction_physical);
} else {
Expand Down
12 changes: 8 additions & 4 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include "utils/globals.h"
#include "utils/logger.h"
#include "utils/utils.h"

#include <coreinit/memdefaultheap.h>
#include <coreinit/memexpheap.h>
#include <kernel/kernel.h>
#include <mutex>
#include <ranges>
#include <set>
#include <wums.h>
Expand Down Expand Up @@ -41,7 +43,7 @@ void UpdateFunctionPointer() {
}

void CheckIfPatchedFunctionsAreStillInMemory() {
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard lock(gPatchedFunctionsMutex);
// Check if rpl has been unloaded by comparing the instruction.
std::set<uint32_t> physicalAddressesUnchanged;
std::set<uint32_t> physicalAddressesChanged;
Expand Down Expand Up @@ -128,13 +130,15 @@ void notify_callback(OSDynLoad_Module module,
void *userContext,
OSDynLoad_NotifyReason reason,
OSDynLoad_NotifyData *infos) {
(void) userContext;
(void) infos;
if (reason == OS_DYNLOAD_NOTIFY_LOADED) {
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard lock(gPatchedFunctionsMutex);
for (auto &cur : gPatchedFunctions) {
PatchFunction(cur);
}
} else if (reason == OS_DYNLOAD_NOTIFY_UNLOADED) {
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard lock(gPatchedFunctionsMutex);
auto library = gFunctionAddressProvider->getTypeForHandle(module);
if (library != LIBRARY_OTHER) {
for (auto &cur : gPatchedFunctions) {
Expand Down Expand Up @@ -162,7 +166,7 @@ WUMS_APPLICATION_STARTS() {

initLogging();
{
std::lock_guard<std::mutex> lock(gPatchedFunctionsMutex);
std::lock_guard 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");
Expand Down
1 change: 1 addition & 0 deletions source/utils/CThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class CThread {

private:
static int32_t threadCallback(int32_t argc, void *arg) {
(void) argc;
//! After call to start() continue with the internal function
((CThread *) arg)->executeThread();
return 0;
Expand Down
2 changes: 1 addition & 1 deletion source/utils/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ char gJumpHeapData[JUMP_HEAP_DATA_SIZE] __attribute__((section(".data")));
MEMHeapHandle gJumpHeapHandle __attribute__((section(".data")));

std::shared_ptr<FunctionAddressProvider> gFunctionAddressProvider;
std::mutex gPatchedFunctionsMutex;
std::recursive_mutex gPatchedFunctionsMutex;
std::vector<std::shared_ptr<PatchedFunctionData>> gPatchedFunctions;

void *(*gMEMAllocFromDefaultHeapExForThreads)(uint32_t size, int align);
Expand Down
3 changes: 2 additions & 1 deletion source/utils/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "version.h"
#include <coreinit/memheap.h>
#include <memory>
#include <mutex>
#include <vector>

#define MODULE_VERSION "v0.2.3"
Expand All @@ -13,7 +14,7 @@ extern char gJumpHeapData[];
extern MEMHeapHandle gJumpHeapHandle;

extern std::shared_ptr<FunctionAddressProvider> gFunctionAddressProvider;
extern std::mutex gPatchedFunctionsMutex;
extern std::recursive_mutex gPatchedFunctionsMutex;
extern std::vector<std::shared_ptr<PatchedFunctionData>> gPatchedFunctions;

extern void *(*gMEMAllocFromDefaultHeapExForThreads)(uint32_t size, int align);
Expand Down