Guard access to global variables with mutex

This commit is contained in:
Maschell 2022-02-14 19:25:54 +01:00
parent 8c107d78a3
commit b845992d44
3 changed files with 43 additions and 11 deletions

View File

@ -2,6 +2,7 @@
#include "fileinfos.h"
#include "utils/logger.h"
#include <coreinit/cache.h>
#include <coreinit/mutex.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
@ -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) {

View File

@ -1,24 +1,33 @@
#include "fileinfos.h"
#include "utils/logger.h"
#include <coreinit/cache.h>
#include <coreinit/mutex.h>
#include <cstdio>
#include <cstring>
#include <rpxloader.h>
#include <stdio.h>
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;
}

View File

@ -8,6 +8,7 @@
#include <coreinit/debug.h>
#include <coreinit/filesystem.h>
#include <coreinit/mcp.h>
#include <coreinit/mutex.h>
#include <coreinit/systeminfo.h>
#include <coreinit/title.h>
#include <cstring>
@ -49,11 +50,16 @@ extern "C" void _SYSLaunchTitleWithStdArgsInNoSplash(uint64_t, uint32_t);
WUPS_USE_WUT_DEVOPTAB();
OSMutex fileWrapperMutex;
OSMutex fileinfoMutex;
INITIALIZE_PLUGIN() {
memset((void *) &current_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;
@ -345,6 +353,7 @@ DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFile
} else if ((handle & 0xFF000000) == 0xFF000000) {
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);