From 91020ac4faa84ce1731eb5fd80b4e07b7f7331f7 Mon Sep 17 00:00:00 2001 From: Sepalani Date: Sun, 19 Nov 2023 01:17:46 +0400 Subject: [PATCH] BBA/HLE: Fix random PCAP file corruption Concurrency between RecvHandlePacket and SendFromDirectFIFO --- Source/Core/Common/PcapFile.cpp | 1 + Source/Core/Core/NetworkCaptureLogger.cpp | 3 +++ Source/Core/Core/NetworkCaptureLogger.h | 2 ++ 3 files changed, 6 insertions(+) diff --git a/Source/Core/Common/PcapFile.cpp b/Source/Core/Common/PcapFile.cpp index 71fcdc9eee..0f248ce1b5 100644 --- a/Source/Core/Common/PcapFile.cpp +++ b/Source/Core/Common/PcapFile.cpp @@ -49,6 +49,7 @@ void PCAP::AddHeader(u32 link_type) m_fp->WriteBytes(&hdr, sizeof(hdr)); } +// Not thread-safe, concurrency between multiple calls to IOFile::WriteBytes. void PCAP::AddPacket(const u8* bytes, size_t size) { std::chrono::system_clock::time_point now(std::chrono::system_clock::now()); diff --git a/Source/Core/Core/NetworkCaptureLogger.cpp b/Source/Core/Core/NetworkCaptureLogger.cpp index 0b8e2bf5d8..d7da8e2469 100644 --- a/Source/Core/Core/NetworkCaptureLogger.cpp +++ b/Source/Core/Core/NetworkCaptureLogger.cpp @@ -123,6 +123,9 @@ void PCAPSSLCaptureLogger::LogBBA(const void* data, std::size_t length) { if (!Config::Get(Config::MAIN_NETWORK_DUMP_BBA)) return; + + // Concurrency between CEXIETHERNET's RecvHandlePacket and SendFromDirectFIFO + const std::lock_guard lock(m_io_mutex); m_file->AddPacket(static_cast(data), length); } diff --git a/Source/Core/Core/NetworkCaptureLogger.h b/Source/Core/Core/NetworkCaptureLogger.h index 3e45446ac1..89dd24030b 100644 --- a/Source/Core/Core/NetworkCaptureLogger.h +++ b/Source/Core/Core/NetworkCaptureLogger.h @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef _WIN32 #include @@ -111,6 +112,7 @@ private: const sockaddr_in& to); std::unique_ptr m_file; + std::mutex m_io_mutex; std::map m_read_sequence_number; std::map m_write_sequence_number; };