IdManager collision threat fix

This commit is contained in:
Nekotekina 2015-07-09 22:55:50 +03:00
parent 4bacfcf847
commit 39629c5c7a
4 changed files with 20 additions and 13 deletions

View File

@ -20,13 +20,15 @@ class ID_data_t final
{
public:
const std::shared_ptr<void> data;
const std::size_t info;
const std::type_info& info;
const std::size_t hash;
const u32 type;
const u32 id;
template<typename T> force_inline ID_data_t(std::shared_ptr<T> 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<std::shared_ptr<void>&>(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<T>(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);
}

View File

@ -54,7 +54,8 @@ namespace vm
template<typename AT2 = AT> static std::enable_if_t<std::is_constructible<AT, AT2>::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<typename AT2 = AT> static std::enable_if_t<std::is_constructible<AT, AT2>::value, _ptr_base> make(const AT2& addr)
{
return{ addr };
const AT value = addr;
return{ value };
}
// defined in CB_FUNC.h, passing context is mandatory

View File

@ -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<u32>::make(phdr.p_vaddr.addr()), phdr.p_filesz / 4);
hook_ppu_funcs(vm::static_ptr_cast<be_t<u32>>(phdr.p_vaddr), phdr.p_filesz / 4);
}
}
break;

View File

@ -125,7 +125,8 @@ template<typename T> 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__)