From 605daf879b11a350bf2e38ea8a29632d8356b680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 7 Apr 2025 15:42:51 +0200 Subject: [PATCH 1/4] Minor cleanup. Fix file handle leak when opening zip files from the main screen. --- Core/Loaders.cpp | 1 + Core/Util/GameManager.cpp | 4 +++- GPU/Debugger/Playback.cpp | 14 ++++++++++++++ UI/InstallZipScreen.cpp | 9 +++++---- UI/MainScreen.cpp | 5 ++--- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Core/Loaders.cpp b/Core/Loaders.cpp index e9e6166cdc..157a0f2d1c 100644 --- a/Core/Loaders.cpp +++ b/Core/Loaders.cpp @@ -45,6 +45,7 @@ FileLoader *ConstructFileLoader(const Path &filename) { } // TODO : improve, look in the file more +// Does not take ownership. IdentifiedFileType Identify_File(FileLoader *fileLoader, std::string *errorString) { errorString->clear(); if (fileLoader == nullptr) { diff --git a/Core/Util/GameManager.cpp b/Core/Util/GameManager.cpp index 11e633f635..cfda50e21a 100644 --- a/Core/Util/GameManager.cpp +++ b/Core/Util/GameManager.cpp @@ -52,6 +52,7 @@ GameManager g_GameManager; +// Close the return value with ZipClose (if non-null, of course). struct zip *ZipOpenPath(Path fileName) { int error = 0; // Need to special case for content URI here, similar to OpenCFile. @@ -73,7 +74,8 @@ struct zip *ZipOpenPath(Path fileName) { } void ZipClose(struct zip *z) { - zip_close(z); + if (z) + zip_close(z); } GameManager::GameManager() { diff --git a/GPU/Debugger/Playback.cpp b/GPU/Debugger/Playback.cpp index 7bfb9e6f19..7481cdd01f 100644 --- a/GPU/Debugger/Playback.cpp +++ b/GPU/Debugger/Playback.cpp @@ -63,6 +63,18 @@ enum class OpType { Done, }; +static const char *OpTypeToString(OpType type) { + switch (type) { + case OpType::None: return "None"; + case OpType::UpdateStallAddr: return "UpdateStallAddr"; + case OpType::EnqueueList: return "EnqueueList"; + case OpType::ListSync: return "ListSync"; + case OpType::ReapplyGfxState: return "ReapplyGfxState"; + case OpType::Done: return "Done"; + default: return "N/A"; + } +} + struct Operation { OpType type; u32 listID; // also listPC in EnqueueList @@ -888,6 +900,8 @@ static u32 LoadReplay(const std::string &filename) { } void Replay_Unload() { + // TODO: Hm, we have a problem here. We might be paused inside a replay and exiting - in this case, the thread is still running. + _dbg_assert_(!replayThread.joinable()); lastExecFilename.clear(); diff --git a/UI/InstallZipScreen.cpp b/UI/InstallZipScreen.cpp index 7256295e42..15ef41c57e 100644 --- a/UI/InstallZipScreen.cpp +++ b/UI/InstallZipScreen.cpp @@ -59,7 +59,7 @@ void InstallZipScreen::CreateViews() { std::string shortFilename = zipPath_.GetFilename(); // TODO: Do in the background? - struct zip *z = ZipOpenPath(zipPath_); + struct zip *zipFile = ZipOpenPath(zipPath_); bool showDeleteCheckbox = false; returnToHomebrew_ = false; @@ -71,8 +71,8 @@ void InstallZipScreen::CreateViews() { std::vector destOptions; - if (z) { - DetectZipFileContents(z, &zipFileInfo_); // Even if this fails, it sets zipInfo->contents. + if (zipFile) { + DetectZipFileContents(zipFile, &zipFileInfo_); // Even if this fails, it sets zipInfo->contents. if (zipFileInfo_.contents == ZipFileContents::ISO_FILE || zipFileInfo_.contents == ZipFileContents::PSP_GAME_DIR) { std::string_view question = iz->T("Install game from ZIP file?"); @@ -120,7 +120,7 @@ void InstallZipScreen::CreateViews() { leftColumn->Add(new TextView(zipFileInfo_.gameTitle + ": " + zipFileInfo_.savedataDir)); Path savedataDir = GetSysDirectory(DIRECTORY_SAVEDATA); - bool overwrite = !CanExtractWithoutOverwrite(z, savedataDir, 50); + bool overwrite = !CanExtractWithoutOverwrite(zipFile, savedataDir, 50); destFolders_.push_back(savedataDir); @@ -163,6 +163,7 @@ void InstallZipScreen::CreateViews() { } else { leftColumn->Add(new TextView(iz->T("Zip file does not contain PSP software"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE))); } + ZipClose(zipFile); } else { leftColumn->Add(new TextView(er->T("The file is not a valid zip file"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE))); } diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 5dd649fcfa..bf79356b05 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -77,14 +77,13 @@ bool MainScreen::showHomebrewTab = false; bool LaunchFile(ScreenManager *screenManager, const Path &path) { // Depending on the file type, we don't want to launch EmuScreen at all. - auto loader = ConstructFileLoader(path); + std::unique_ptr loader(ConstructFileLoader(path)); if (!loader) { return false; } std::string errorString; - IdentifiedFileType type = Identify_File(loader, &errorString); - delete loader; + IdentifiedFileType type = Identify_File(loader.get(), &errorString); switch (type) { case IdentifiedFileType::ARCHIVE_ZIP: From 4d215b1f5477a27e4caed3d88be47a7b3a81a290 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 7 Apr 2025 16:33:43 +0200 Subject: [PATCH 2/4] Cleaner exit from when stepping through a frame dump --- GPU/Debugger/Playback.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/GPU/Debugger/Playback.cpp b/GPU/Debugger/Playback.cpp index 7481cdd01f..c2ac5c045f 100644 --- a/GPU/Debugger/Playback.cpp +++ b/GPU/Debugger/Playback.cpp @@ -91,7 +91,7 @@ static std::vector lastExecPushbuf; static std::thread replayThread; static std::mutex opStartLock; -static std::condition_variable opStartWait; +static std::condition_variable g_condOpStartWait; static std::mutex opFinishLock; static std::condition_variable opFinishWait; @@ -107,7 +107,7 @@ u32 ExecuteOnMain(Operation opToExec) { g_opToExec = opToExec; g_retVal = 0; g_opDone = false; - opStartWait.notify_one(); + g_condOpStartWait.notify_one(); } // now wait for completion. At that point, noone cares about g_opToExec anymore, and we can safely @@ -900,7 +900,16 @@ static u32 LoadReplay(const std::string &filename) { } void Replay_Unload() { - // TODO: Hm, we have a problem here. We might be paused inside a replay and exiting - in this case, the thread is still running. + // We might be paused inside a replay - in this case, the thread is still running and we need to tell it to stop. + if (replayThread.joinable()) { + { + std::unique_lock waitLock(opFinishLock); + // Just override the next op to bail. + g_opToExec = Operation{ OpType::Done }; + opFinishWait.notify_one(); + } + replayThread.join(); + } _dbg_assert_(!replayThread.joinable()); @@ -981,7 +990,7 @@ ReplayResult RunMountedReplay(const std::string &filename) { // OK, now wait for and perform the desired action. { std::unique_lock lock(opStartLock); - opStartWait.wait(lock, []() { return g_opToExec.type != OpType::None; }); + g_condOpStartWait.wait(lock, []() { return g_opToExec.type != OpType::None; }); } switch (g_opToExec.type) { From 370a521381ba11927302df9ef806fee14fa2a9bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 7 Apr 2025 16:46:04 +0200 Subject: [PATCH 3/4] Move UPnP init/shutdown to app init/shutdown instead of game init/shutdown --- Core/HLE/sceNet.cpp | 10 +++++++--- Core/Util/PortManager.cpp | 4 ++-- Core/Util/PortManager.h | 2 +- UI/NativeApp.cpp | 5 +++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Core/HLE/sceNet.cpp b/Core/HLE/sceNet.cpp index adc332f8ea..386fee5624 100644 --- a/Core/HLE/sceNet.cpp +++ b/Core/HLE/sceNet.cpp @@ -515,8 +515,11 @@ void __NetInit() { INFO_LOG(Log::sceNet, "LocalHost IP will be %s [%s]", ip2str(g_localhostIP.in.sin_addr).c_str(), mac2str(&mac).c_str()); __PlatformNetInit(); - // TODO: May be we should initialize & cleanup somewhere else than here for PortManager to be used as general purpose for whatever port forwarding PPSSPP needed - __UPnPInit(); + + // For libretro we don't have a better place. On other platforms, we just init/shutdown it with the rest of the emu (NativeInit / NativeShutdown). +#ifdef __LIBRETRO__ + __UPnPInit(2000); +#endif __ResetInitNetLib(); __NetApctlInit(); @@ -541,8 +544,9 @@ void __NetShutdown() { __NetApctlShutdown(); __ResetInitNetLib(); - // Since PortManager supposed to be general purpose for whatever port forwarding PPSSPP needed, may be we shouldn't clear & restore ports in here? it will be cleared and restored by PortManager's destructor when exiting PPSSPP anyway +#ifdef __LIBRETRO__ __UPnPShutdown(); +#endif __PlatformNetShutdown(); diff --git a/Core/Util/PortManager.cpp b/Core/Util/PortManager.cpp index 96fa9a8f59..ac6b17d291 100644 --- a/Core/Util/PortManager.cpp +++ b/Core/Util/PortManager.cpp @@ -523,10 +523,10 @@ int upnpService(const unsigned int timeout) { return 0; } -void __UPnPInit(const unsigned int timeout) { +void __UPnPInit(const int timeout_ms) { if (!upnpServiceRunning) { upnpServiceRunning = true; - upnpServiceThread = std::thread(upnpService, timeout); + upnpServiceThread = std::thread(upnpService, timeout_ms); } } diff --git a/Core/Util/PortManager.h b/Core/Util/PortManager.h index 039d1ea52d..c3788fea31 100644 --- a/Core/Util/PortManager.h +++ b/Core/Util/PortManager.h @@ -116,7 +116,7 @@ private: extern PortManager g_PortManager; -void __UPnPInit(const unsigned int timeout = 2000); +void __UPnPInit(const int timeout_ms); void __UPnPShutdown(); // Add a port & protocol (TCP, UDP or vendor-defined) to map for forwarding (intport = 0 : same as [external] port) diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 47ec95b391..a04cf94815 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -339,6 +339,9 @@ static void ClearFailedGPUBackends() { void NativeInit(int argc, const char *argv[], const char *savegame_dir, const char *external_dir, const char *cache_dir) { net::Init(); // This needs to happen before we load the config. So on Windows we also run it in Main. It's fine to call multiple times. + // Probably an excessive timeout. it only causes delays on shutdown, though. + __UPnPInit(2000); + ShaderTranslationInit(); g_threadManager.Init(cpu_info.num_cores, cpu_info.logical_cpu_count); @@ -1499,6 +1502,8 @@ void NativeShutdown() { System_ExitApp(); #endif + __UPnPShutdown(); + g_PortManager.Shutdown(); net::Shutdown(); From 651cefb6fc53a62126bacc9a0a1a4e2c579ce357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 7 Apr 2025 16:48:08 +0200 Subject: [PATCH 4/4] Remove redundant functions --- Core/HLE/sceNet.cpp | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/Core/HLE/sceNet.cpp b/Core/HLE/sceNet.cpp index 386fee5624..c03ecbc555 100644 --- a/Core/HLE/sceNet.cpp +++ b/Core/HLE/sceNet.cpp @@ -414,25 +414,6 @@ void InitLocalhostIP() { isLocalServer = (!strcasecmp(serverStr.c_str(), "localhost") || serverStr.find("127.") == 0); } -static bool __PlatformNetInit() { -#ifdef _WIN32 - WSADATA wsaData; - if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { - // TODO: log - return false; - } -#endif - return true; -} - -static bool __PlatformNetShutdown() { -#ifdef _WIN32 - return WSACleanup() == 0; -#else - return true; -#endif -} - static void __ApctlState(u64 userdata, int cyclesLate) { SceUID threadID = userdata >> 32; int uid = (int)(userdata & 0xFFFFFFFF); @@ -514,8 +495,6 @@ void __NetInit() { getLocalMac(&mac); INFO_LOG(Log::sceNet, "LocalHost IP will be %s [%s]", ip2str(g_localhostIP.in.sin_addr).c_str(), mac2str(&mac).c_str()); - __PlatformNetInit(); - // For libretro we don't have a better place. On other platforms, we just init/shutdown it with the rest of the emu (NativeInit / NativeShutdown). #ifdef __LIBRETRO__ __UPnPInit(2000); @@ -548,8 +527,6 @@ void __NetShutdown() { __UPnPShutdown(); #endif - __PlatformNetShutdown(); - free(dummyPeekBuf64k); }