From b845992d44d5a05a6a5bf6a3c8df8ea903bf9aa0 Mon Sep 17 00:00:00 2001 From: Maschell Date: Mon, 14 Feb 2022 19:25:54 +0100 Subject: [PATCH] Guard access to global variables with mutex --- src/FileWrapper.cpp | 12 +++++++++--- src/fileinfos.cpp | 28 ++++++++++++++++++++++------ src/main.cpp | 14 ++++++++++++-- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/FileWrapper.cpp b/src/FileWrapper.cpp index ec8cfa7..aeb358c 100644 --- a/src/FileWrapper.cpp +++ b/src/FileWrapper.cpp @@ -2,6 +2,7 @@ #include "fileinfos.h" #include "utils/logger.h" #include +#include #include #include #include @@ -9,15 +10,20 @@ FileHandleWrapper gFileHandleWrapper[FILE_WRAPPER_SIZE] __attribute__((section(".data"))); +extern OSMutex fileWrapperMutex; + int FileHandleWrapper_GetSlot() { + OSLockMutex(&fileWrapperMutex); + int res = -1; for (int i = 0; i < FILE_WRAPPER_SIZE; i++) { if (!gFileHandleWrapper[i].inUse) { gFileHandleWrapper[i].inUse = true; - DCFlushRange(&gFileHandleWrapper[i], sizeof(FileHandleWrapper)); - return i; + res = i; } } - return -1; + OSMemoryBarrier(); + OSUnlockMutex(&fileWrapperMutex); + return res; } int OpenFileForID(int id, const char *filepath, int *handle) { diff --git a/src/fileinfos.cpp b/src/fileinfos.cpp index a5e59af..c2a5172 100644 --- a/src/fileinfos.cpp +++ b/src/fileinfos.cpp @@ -1,24 +1,33 @@ #include "fileinfos.h" #include "utils/logger.h" +#include +#include +#include #include #include -#include FileInfos gFileInfos[FILE_INFO_SIZE] __attribute__((section(".data"))); +extern OSMutex fileinfoMutex; int32_t getIDByLowerTitleID(uint32_t lowerTitleID) { + OSLockMutex(&fileinfoMutex); + int res = -1; for (int i = 0; i < FILE_INFO_SIZE; i++) { if (strlen(gFileInfos[i].path) > 0 && gFileInfos[i].lowerTitleID == lowerTitleID) { - return i; + res = i; + break; } } - return -1; + OSMemoryBarrier(); + OSUnlockMutex(&fileinfoMutex); + return res; } void unmountRomfs(uint32_t id) { if (id >= FILE_INFO_SIZE) { return; } + OSLockMutex(&fileinfoMutex); if (gFileInfos[id].romfsMounted) { char romName[10]; snprintf(romName, 10, "%08X", id); @@ -27,6 +36,8 @@ void unmountRomfs(uint32_t id) { DEBUG_FUNCTION_LINE("res: %d", res); gFileInfos[id].romfsMounted = false; } + OSMemoryBarrier(); + OSUnlockMutex(&fileinfoMutex); } void unmountAllRomfs() { @@ -40,6 +51,8 @@ bool mountRomfs(uint32_t id) { DEBUG_FUNCTION_LINE("HANDLE WAS TOO BIG %d", id); return false; } + OSLockMutex(&fileinfoMutex); + bool result = false; if (!gFileInfos[id].romfsMounted) { char buffer[256]; snprintf(buffer, 256, "/vol/external01/%s", gFileInfos[id].path); @@ -50,11 +63,14 @@ bool mountRomfs(uint32_t id) { if ((res = RL_MountBundle(romName, buffer, BundleSource_FileDescriptor_CafeOS)) == 0) { DEBUG_FUNCTION_LINE("Mounted successfully "); gFileInfos[id].romfsMounted = true; - return true; + result = true; } else { DEBUG_FUNCTION_LINE("Mounting failed %d", res); - return false; + result = false; } } - return true; + + OSMemoryBarrier(); + OSUnlockMutex(&fileinfoMutex); + return result; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8358729..8aa0750 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -49,11 +50,16 @@ extern "C" void _SYSLaunchTitleWithStdArgsInNoSplash(uint64_t, uint32_t); WUPS_USE_WUT_DEVOPTAB(); +OSMutex fileWrapperMutex; +OSMutex fileinfoMutex; + INITIALIZE_PLUGIN() { memset((void *) ¤t_launched_title_info, 0, sizeof(current_launched_title_info)); memset((void *) &gLaunchXML, 0, sizeof(gLaunchXML)); memset((void *) &gFileInfos, 0, sizeof(gFileInfos)); gHomebrewLaunched = FALSE; + OSInitMutex(&fileWrapperMutex); + OSInitMutex(&fileinfoMutex); } ON_APPLICATION_START() { @@ -266,6 +272,7 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount, int32_t result = real_MCP_TitleList(handle, outTitleCount, titleList, size); uint32_t titlecount = *outTitleCount; + OSLockMutex(&fileinfoMutex); for (auto &gFileInfo : gFileInfos) { if (gFileInfo.lowerTitleID == 0) { break; @@ -273,6 +280,7 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount, memcpy(&(titleList[titlecount]), &(gFileInfo.titleInfo), sizeof(gFileInfo.titleInfo)); titlecount++; } + OSUnlockMutex(&fileinfoMutex); *outTitleCount = titlecount; @@ -343,8 +351,9 @@ DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFile if (handle == 0x13371338) { return FS_STATUS_OK; } else if ((handle & 0xFF000000) == 0xFF000000) { - int32_t fd = (handle & 0x00000FFF); - int32_t romid = (handle & 0x00FFF000) >> 12; + int32_t fd = (handle & 0x00000FFF); + int32_t romid = (handle & 0x00FFF000) >> 12; + OSLockMutex(&fileinfoMutex); uint32_t rl_handle = gFileHandleWrapper[fd].handle; RL_FileClose(rl_handle); if (gFileInfos[romid].openedFiles--) { @@ -354,6 +363,7 @@ DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFile unmountRomfs(romid); } } + OSUnlockMutex(&fileinfoMutex); return FS_STATUS_OK; } return real_FSCloseFile(client, block, handle, flags);