From 7b8832cebb1f37c5482b3e17751b14af5eb55a60 Mon Sep 17 00:00:00 2001 From: Vitor Kiguchi Date: Wed, 5 Apr 2023 13:08:44 -0300 Subject: [PATCH 1/4] externals: update zstd to v1.5.5 --- externals/zstd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/externals/zstd b/externals/zstd index e47e674cd..63779c798 160000 --- a/externals/zstd +++ b/externals/zstd @@ -1 +1 @@ -Subproject commit e47e674cd09583ff0503f0f6defd6d23d8b718d3 +Subproject commit 63779c798237346c2b245c546c40b72a5a5913fe From 8e8c8fa37a50910692ed44b500a1111b6351dc91 Mon Sep 17 00:00:00 2001 From: Vitor Kiguchi Date: Wed, 5 Apr 2023 14:50:51 -0300 Subject: [PATCH 2/4] savestates: log slot on load and save --- src/core/core.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index 4151cb4c6..97650b620 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -113,9 +113,10 @@ System::ResultStatus System::RunLoop(bool tight_loop) { case Signal::Shutdown: return ResultStatus::ShutdownRequested; case Signal::Load: { - LOG_INFO(Core, "Begin load"); + const u32 slot = param; + LOG_INFO(Core, "Begin load of slot {}", slot); try { - System::LoadState(param); + System::LoadState(slot); LOG_INFO(Core, "Load completed"); } catch (const std::exception& e) { LOG_ERROR(Core, "Error loading: {}", e.what()); @@ -126,9 +127,10 @@ System::ResultStatus System::RunLoop(bool tight_loop) { return ResultStatus::Success; } case Signal::Save: { - LOG_INFO(Core, "Begin save"); + const u32 slot = param; + LOG_INFO(Core, "Begin save to slot {}", slot); try { - System::SaveState(param); + System::SaveState(slot); LOG_INFO(Core, "Save completed"); } catch (const std::exception& e) { LOG_ERROR(Core, "Error saving: {}", e.what()); From c79acdd88d4baa9afc41aaf3ef9d4bac875d65b5 Mon Sep 17 00:00:00 2001 From: Vitor Kiguchi Date: Wed, 5 Apr 2023 15:28:56 -0300 Subject: [PATCH 3/4] savestates: validate states before load --- src/core/savestate.cpp | 54 +++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/core/savestate.cpp b/src/core/savestate.cpp index 1ab245570..b96f20ed1 100644 --- a/src/core/savestate.cpp +++ b/src/core/savestate.cpp @@ -49,6 +49,30 @@ std::string GetSaveStatePath(u64 program_id, u32 slot) { } } +static bool ValidateSaveState(const CSTHeader& header, SaveStateInfo& info, u64 program_id, + u32 slot) { + const auto path = GetSaveStatePath(program_id, slot); + if (header.filetype != header_magic_bytes) { + LOG_WARNING(Core, "Invalid save state file {}", path); + return false; + } + info.time = header.time; + + if (header.program_id != program_id) { + LOG_WARNING(Core, "Save state file isn't for the current game {}", path); + return false; + } + const std::string revision = fmt::format("{:02x}", fmt::join(header.revision, "")); + if (revision == Common::g_scm_rev) { + info.status = SaveStateInfo::ValidationStatus::OK; + } else { + LOG_WARNING(Core, "Save state file {} created from a different revision {}", path, + revision); + info.status = SaveStateInfo::ValidationStatus::RevisionDismatch; + } + return true; +} + std::vector ListSaveStates(u64 program_id) { std::vector result; for (u32 slot = 1; slot <= SaveStateSlotCount; ++slot) { @@ -74,24 +98,10 @@ std::vector ListSaveStates(u64 program_id) { LOG_ERROR(Core, "Could not read from file {}", path); continue; } - if (header.filetype != header_magic_bytes) { - LOG_WARNING(Core, "Invalid save state file {}", path); + if (!ValidateSaveState(header, info, program_id, slot)) { continue; } - info.time = header.time; - if (header.program_id != program_id) { - LOG_WARNING(Core, "Save state file isn't for the current game {}", path); - continue; - } - const std::string revision = fmt::format("{:02x}", fmt::join(header.revision, "")); - if (revision == Common::g_scm_rev) { - info.status = SaveStateInfo::ValidationStatus::OK; - } else { - LOG_WARNING(Core, "Save state file {} created from a different revision {}", path, - revision); - info.status = SaveStateInfo::ValidationStatus::RevisionDismatch; - } result.emplace_back(std::move(info)); } return result; @@ -146,7 +156,19 @@ void System::LoadState(u32 slot) { std::vector buffer(FileUtil::GetSize(path) - sizeof(CSTHeader)); FileUtil::IOFile file(path, "rb"); - file.Seek(sizeof(CSTHeader), SEEK_SET); // Skip header + + // load header + CSTHeader header; + if (file.ReadBytes(&header, sizeof(header)) != sizeof(header)) { + throw std::runtime_error("Could not read from file at " + path); + } + + // validate header + SaveStateInfo info; + if (!ValidateSaveState(header, info, title_id, slot)) { + throw std::runtime_error("Invalid savestate"); + } + if (file.ReadBytes(buffer.data(), buffer.size()) != buffer.size()) { throw std::runtime_error("Could not read from file at " + path); } From dccd89b796d1adb66234fb8b4b88f346573c5a73 Mon Sep 17 00:00:00 2001 From: Vitor Kiguchi Date: Wed, 5 Apr 2023 15:48:03 -0300 Subject: [PATCH 4/4] savestates: various changes The header doesn't need to be serialized. GetSaveStatePath may be static. Remove unused includes. Name the CryptoPP stringsource. --- src/core/savestate.cpp | 16 ++++------------ src/core/savestate.h | 2 -- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/core/savestate.cpp b/src/core/savestate.cpp index b96f20ed1..efbbe3eb4 100644 --- a/src/core/savestate.cpp +++ b/src/core/savestate.cpp @@ -3,18 +3,15 @@ // Refer to the license.txt file included. #include -#include #include #include "common/archives.h" #include "common/logging/log.h" #include "common/scm_rev.h" #include "common/zstd_compression.h" -#include "core/cheats/cheats.h" #include "core/core.h" #include "core/movie.h" #include "core/savestate.h" #include "network/network.h" -#include "video_core/video_core.h" namespace Core { @@ -25,19 +22,14 @@ struct CSTHeader { std::array revision; /// Git hash of the revision this savestate was created with u64_le time; /// The time when this save state was created - std::array reserved; /// Make heading 256 bytes so it has consistent size - - template - void serialize(Archive& ar, const unsigned int) { - ar& boost::serialization::binary_object(this, sizeof(CSTHeader)); - } + std::array reserved{}; /// Make heading 256 bytes so it has consistent size }; static_assert(sizeof(CSTHeader) == 256, "CSTHeader should be 256 bytes"); #pragma pack(pop) constexpr std::array header_magic_bytes{{'C', 'S', 'T', 0x1B}}; -std::string GetSaveStatePath(u64 program_id, u32 slot) { +static std::string GetSaveStatePath(u64 program_id, u32 slot) { const u64 movie_id = Movie::GetInstance().GetCurrentMovieID(); if (movie_id) { return fmt::format("{}{:016X}.movie{:016X}.{:02d}.cst", @@ -131,8 +123,8 @@ void System::SaveState(u32 slot) const { header.filetype = header_magic_bytes; header.program_id = title_id; std::string rev_bytes; - CryptoPP::StringSource(Common::g_scm_rev, true, - new CryptoPP::HexDecoder(new CryptoPP::StringSink(rev_bytes))); + CryptoPP::StringSource ss(Common::g_scm_rev, true, + new CryptoPP::HexDecoder(new CryptoPP::StringSink(rev_bytes))); std::memcpy(header.revision.data(), rev_bytes.data(), sizeof(header.revision)); header.time = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) diff --git a/src/core/savestate.h b/src/core/savestate.h index f67bee22f..2a1d1db52 100644 --- a/src/core/savestate.h +++ b/src/core/savestate.h @@ -9,8 +9,6 @@ namespace Core { -struct CSTHeader; - struct SaveStateInfo { u32 slot; u64 time;