From 39629c5c7a9779101c4f37345f54478f2166ebee Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Thu, 9 Jul 2015 22:55:50 +0300 Subject: [PATCH] IdManager collision threat fix --- rpcs3/Emu/IdManager.h | 22 +++++++++++++--------- rpcs3/Emu/Memory/vm_ptr.h | 6 ++++-- rpcs3/Loader/ELF64.cpp | 2 +- rpcs3/stdafx.h | 3 ++- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index aec7469f8f..1b8994f4eb 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -20,13 +20,15 @@ class ID_data_t final { public: const std::shared_ptr data; - const std::size_t info; + const std::type_info& info; + const std::size_t hash; const u32 type; const u32 id; template force_inline ID_data_t(std::shared_ptr data, u32 type, u32 id) : data(std::move(data)) - , info(typeid(T).hash_code()) + , info(typeid(T)) + , hash(typeid(T).hash_code()) , type(type) , id(id) { @@ -35,6 +37,7 @@ public: ID_data_t(const ID_data_t& right) : data(right.data) , info(right.info) + , hash(right.hash) , type(right.type) , id(right.id) { @@ -45,6 +48,7 @@ public: ID_data_t(ID_data_t&& right) : data(std::move(const_cast&>(right.data))) , info(right.info) + , hash(right.hash) , type(right.type) , id(right.id) { @@ -68,7 +72,7 @@ public: auto f = m_id_map.find(id); - return f != m_id_map.end() && f->second.info == typeid(T).hash_code(); + return f != m_id_map.end() && f->second.info == typeid(T); } // check if ID exists and has specified type @@ -139,7 +143,7 @@ public: auto f = m_id_map.find(id); - if (f == m_id_map.end() || f->second.info != typeid(Orig).hash_code()) + if (f == m_id_map.end() || f->second.info != typeid(Orig)) { return nullptr; } @@ -158,7 +162,7 @@ public: for (auto& v : m_id_map) { - if (v.second.info == hash) + if (v.second.hash == hash && v.second.info == typeid(Orig)) { result.emplace_back(std::static_pointer_cast(v.second.data)); } @@ -173,7 +177,7 @@ public: auto item = m_id_map.find(id); - if (item == m_id_map.end() || item->second.info != typeid(T).hash_code()) + if (item == m_id_map.end() || item->second.info != typeid(T)) { return false; } @@ -193,7 +197,7 @@ public: for (auto& v : m_id_map) { - if (v.second.info == hash) + if (v.second.hash == hash && v.second.info == typeid(T)) { result++; } @@ -230,7 +234,7 @@ public: for (auto& v : m_id_map) { - if (v.second.info == hash) + if (v.second.hash == hash && v.second.info == typeid(T)) { result.insert(v.first); } @@ -267,7 +271,7 @@ public: for (auto& v : m_id_map) { - if (v.second.info == hash) + if (v.second.hash == hash && v.second.info == typeid(T)) { result.emplace_back(v.second); } diff --git a/rpcs3/Emu/Memory/vm_ptr.h b/rpcs3/Emu/Memory/vm_ptr.h index 8dd8fd83a6..66a68f9976 100644 --- a/rpcs3/Emu/Memory/vm_ptr.h +++ b/rpcs3/Emu/Memory/vm_ptr.h @@ -54,7 +54,8 @@ namespace vm template static std::enable_if_t::value, _ptr_base> make(const AT2& addr) { - return{ addr }; + const AT value = addr; + return{ value }; } T* get_ptr() const @@ -129,7 +130,8 @@ namespace vm template static std::enable_if_t::value, _ptr_base> make(const AT2& addr) { - return{ addr }; + const AT value = addr; + return{ value }; } // defined in CB_FUNC.h, passing context is mandatory diff --git a/rpcs3/Loader/ELF64.cpp b/rpcs3/Loader/ELF64.cpp index 23623b4f3d..ab31f086e5 100644 --- a/rpcs3/Loader/ELF64.cpp +++ b/rpcs3/Loader/ELF64.cpp @@ -606,7 +606,7 @@ namespace loader { m_stream->Seek(handler::get_stream_offset() + phdr.p_offset); m_stream->Read(phdr.p_vaddr.get_ptr(), phdr.p_filesz); - hook_ppu_funcs(vm::ptr::make(phdr.p_vaddr.addr()), phdr.p_filesz / 4); + hook_ppu_funcs(vm::static_ptr_cast>(phdr.p_vaddr), phdr.p_filesz / 4); } } break; diff --git a/rpcs3/stdafx.h b/rpcs3/stdafx.h index 59c87aadf6..12dfe69691 100644 --- a/rpcs3/stdafx.h +++ b/rpcs3/stdafx.h @@ -125,7 +125,8 @@ template struct ID_type; #define CHECK_MAX_SIZE(type, size) static_assert(sizeof(type) <= size, #type " type size is too big") #define CHECK_SIZE_ALIGN(type, size, align) CHECK_SIZE(type, size); CHECK_ALIGN(type, align) -#define WRAP_EXPR(expr) [&]{ return (expr); } +#define WRAP_EXPR(expr) [&]{ return expr; } +#define COPY_EXPR(expr) [=]{ return expr; } #define EXCEPTION(text, ...) fmt::exception(__FILE__, __LINE__, __FUNCTION__, text, ##__VA_ARGS__) #define VM_CAST(value) vm::impl_cast(value, __FILE__, __LINE__, __FUNCTION__)