From b50b6f6d482d88e895d15fd052c1b0d720592a53 Mon Sep 17 00:00:00 2001 From: DH Date: Wed, 5 Jul 2023 23:08:51 +0300 Subject: [PATCH] [rpcsx-os] Protect vm with mutex --- rpcsx-os/vm.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/rpcsx-os/vm.cpp b/rpcsx-os/vm.cpp index b4edc3ff3..938828339 100644 --- a/rpcsx-os/vm.cpp +++ b/rpcsx-os/vm.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,8 @@ bool unmap(void *address, std::size_t size) { } // namespace } // namespace utils +static std::mutex g_mtx; + std::string rx::vm::mapFlagsToString(std::int32_t flags) { std::string result; @@ -687,6 +690,8 @@ void *rx::vm::map(void *addr, std::uint64_t len, std::int32_t prot, } } + std::lock_guard lock(g_mtx); + std::uint64_t address = 0; if ((flags & kMapFlagFixed) == kMapFlagFixed) { address = hitAddress; @@ -840,6 +845,7 @@ bool rx::vm::unmap(void *addr, std::uint64_t size) { __builtin_trap(); } + std::lock_guard lock(g_mtx); gBlocks[(address >> kBlockShift) - kFirstBlock].removeFlags( (address & kBlockMask) >> kPageShift, pages, ~0); rx::bridge.sendMemoryProtect(reinterpret_cast(addr), size, 0); @@ -868,6 +874,8 @@ bool rx::vm::protect(void *addr, std::uint64_t size, std::int32_t prot) { std::abort(); } + std::lock_guard lock(g_mtx); + gBlocks[(address >> kBlockShift) - kFirstBlock].setFlags( (address & kBlockMask) >> kPageShift, pages, kAllocated | (prot & (kMapProtCpuAll | kMapProtGpuAll))); @@ -884,8 +892,9 @@ bool rx::vm::queryProtection(const void *addr, std::uint64_t *startAddress, bool rx::vm::virtualQuery(const void *addr, std::int32_t flags, VirtualQueryInfo *info) { - auto address = reinterpret_cast(addr); + std::lock_guard lock(g_mtx); + auto address = reinterpret_cast(addr); auto it = gVirtualAllocations.lower_bound(address); if (it == gVirtualAllocations.end()) {