1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:17:35 +00:00

UserspaceEmulator: Make it possible to wrap PODs in ValueWithShadow

Instead of making it hold the shadow data as another `T`, make it hold
the data as a byte array, and allow it to read the byte array as `T`.
This makes it much easier to make a "read_typed" function in the MMU.
This commit is contained in:
Ali Mohammad Pur 2022-02-27 23:56:46 +03:30 committed by Andreas Kling
parent e08cf8f554
commit f6e82a8e0a
7 changed files with 115 additions and 101 deletions

View file

@ -13,10 +13,6 @@
namespace UserspaceEmulator {
constexpr u64 _initialized_64 = 0x01010101'01010101LLU;
constexpr u128 _initialized_128 = u128(_initialized_64, _initialized_64);
constexpr u256 _initialized_256 = u256(_initialized_128, _initialized_128);
template<typename T>
class ValueAndShadowReference;
@ -24,114 +20,134 @@ template<typename T>
class ValueWithShadow {
public:
using ValueType = T;
using ShadowType = Array<u8, sizeof(T)>;
ValueWithShadow() = default;
ValueWithShadow(T value, T shadow)
: m_value(value)
{
ReadonlyBytes { &shadow, sizeof(shadow) }.copy_to(m_shadow);
}
ValueWithShadow(T value, ShadowType shadow)
: m_value(value)
, m_shadow(shadow)
{
}
ValueWithShadow(const ValueAndShadowReference<T>&);
static ValueWithShadow create_initialized(T value)
{
ShadowType shadow;
shadow.fill(0x01);
return {
value,
shadow,
};
}
ValueWithShadow(ValueAndShadowReference<T> const&);
T value() const { return m_value; }
T shadow() const { return m_shadow; }
ShadowType const& shadow() const { return m_shadow; }
T shadow_as_value() const requires(IsTriviallyConstructible<T>)
{
return *bit_cast<T const*>(m_shadow.data());
}
template<auto member>
auto reference_to() requires(IsClass<T> || IsUnion<T>)
{
using ResultType = ValueAndShadowReference<RemoveReference<decltype(declval<T>().*member)>>;
return ResultType {
m_value.*member,
*bit_cast<typename ResultType::ShadowType*>(m_shadow.span().offset_pointer(bit_cast<u8*>(member) - bit_cast<u8*>(nullptr))),
};
}
template<auto member>
auto slice() const requires(IsClass<T> || IsUnion<T>)
{
using ResultType = ValueWithShadow<RemoveReference<decltype(declval<T>().*member)>>;
return ResultType {
m_value.*member,
*bit_cast<typename ResultType::ShadowType*>(m_shadow.span().offset_pointer(bit_cast<u8*>(member) - bit_cast<u8*>(nullptr))),
};
}
bool is_uninitialized() const
{
if constexpr (sizeof(T) == 32)
return (m_shadow & _initialized_256) != _initialized_256;
if constexpr (sizeof(T) == 16)
return (m_shadow & _initialized_128) != _initialized_128;
if constexpr (sizeof(T) == 8)
return (m_shadow & _initialized_64) != _initialized_64;
if constexpr (sizeof(T) == 4)
return (m_shadow & 0x01010101) != 0x01010101;
if constexpr (sizeof(T) == 2)
return (m_shadow & 0x0101) != 0x0101;
if constexpr (sizeof(T) == 1)
return (m_shadow & 0x01) != 0x01;
for (size_t i = 0; i < sizeof(ShadowType); ++i) {
if ((m_shadow[i] & 0x01) != 0x01)
return true;
}
return false;
}
void set_initialized()
{
if constexpr (sizeof(T) == 32)
m_shadow = _initialized_256;
if constexpr (sizeof(T) == 16)
m_shadow = _initialized_128;
if constexpr (sizeof(T) == 8)
m_shadow = _initialized_64;
if constexpr (sizeof(T) == 4)
m_shadow = 0x01010101;
if constexpr (sizeof(T) == 2)
m_shadow = 0x0101;
if constexpr (sizeof(T) == 1)
m_shadow = 0x01;
m_shadow.fill(0x01);
}
private:
T m_value;
T m_shadow;
T m_value {};
ShadowType m_shadow {};
};
template<typename T>
class ValueAndShadowReference {
public:
using ValueType = T;
using ShadowType = Array<u8, sizeof(T)>;
ValueAndShadowReference(T& value, T& shadow)
ValueAndShadowReference(T& value, ShadowType& shadow)
: m_value(value)
, m_shadow(shadow)
{
}
ValueAndShadowReference(T& value, T& shadow)
: m_value(value)
, m_shadow(*bit_cast<ShadowType*>(&shadow))
{
}
bool is_uninitialized() const
{
if constexpr (sizeof(T) == 32)
return (m_shadow & _initialized_256) != _initialized_256;
if constexpr (sizeof(T) == 16)
return (m_shadow & _initialized_128) != _initialized_128;
if constexpr (sizeof(T) == 8)
return (m_shadow & _initialized_64) != _initialized_64;
if constexpr (sizeof(T) == 4)
return (m_shadow & 0x01010101) != 0x01010101;
if constexpr (sizeof(T) == 2)
return (m_shadow & 0x0101) != 0x0101;
if constexpr (sizeof(T) == 1)
return (m_shadow & 0x01) != 0x01;
for (size_t i = 0; i < sizeof(ShadowType); ++i) {
if ((m_shadow[i] & 0x01) != 0x01)
return true;
}
return false;
}
ValueAndShadowReference<T>& operator=(const ValueWithShadow<T>&);
T& value() { return m_value; }
T& shadow() { return m_shadow; }
T shadow_as_value() const requires(IsTriviallyConstructible<T>)
{
return *bit_cast<T const*>(m_shadow.data());
}
const T& value() const { return m_value; }
const T& shadow() const { return m_shadow; }
T& value() { return m_value; }
ShadowType& shadow() { return m_shadow; }
T const& value() const { return m_value; }
ShadowType const& shadow() const { return m_shadow; }
private:
T& m_value;
T& m_shadow;
ShadowType& m_shadow;
};
template<typename T>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_as_initialized(T value)
{
if constexpr (sizeof(T) == 32)
return { value, _initialized_256 };
if constexpr (sizeof(T) == 16)
return { value, _initialized_128 };
if constexpr (sizeof(T) == 8)
return { value, _initialized_64 };
if constexpr (sizeof(T) == 4)
return { value, 0x01010101 };
if constexpr (sizeof(T) == 2)
return { value, 0x0101 };
if constexpr (sizeof(T) == 1)
return { value, 0x01 };
return ValueWithShadow<T>::create_initialized(value);
}
template<typename T, typename U>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, const U& taint_a)
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, U const& taint_a)
{
if (taint_a.is_uninitialized())
return { value, 0 };
@ -139,7 +155,7 @@ ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, const U& t
}
template<typename T, typename U, typename V>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, const U& taint_a, const V& taint_b)
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, U const& taint_a, V const& taint_b)
{
if (taint_a.is_uninitialized() || taint_b.is_uninitialized())
return { value, 0 };
@ -147,7 +163,7 @@ ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, const U& t
}
template<typename T, typename U, typename V, typename X>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, const U& taint_a, const V& taint_b, const X& taint_c)
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, U const& taint_a, V const& taint_b, X const& taint_c)
{
if (taint_a.is_uninitialized() || taint_b.is_uninitialized() || taint_c.is_uninitialized())
return { value, 0 };
@ -178,7 +194,3 @@ struct AK::Formatter<UserspaceEmulator::ValueWithShadow<T>> : AK::Formatter<T> {
return Formatter<T>::format(builder, value.value());
}
};
#undef INITIALIZED_64
#undef INITIALIZED_128
#undef INITIALIZED_256