summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAli Mohammad Pur <ali.mpfard@gmail.com>2022-02-27 23:56:46 +0330
committerAndreas Kling <kling@serenityos.org>2022-03-04 20:07:05 +0100
commitf6e82a8e0a19b9d575f462bd5d34d831dd268bb3 (patch)
tree3ddded564b3a519132f67969039c1e8b78a74300 /Userland
parente08cf8f554323716e8a6ba45766ef29f65791a5e (diff)
downloadserenity-f6e82a8e0a19b9d575f462bd5d34d831dd268bb3.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/DevTools/UserspaceEmulator/MmapRegion.cpp3
-rw-r--r--Userland/DevTools/UserspaceEmulator/SimpleRegion.cpp2
-rw-r--r--Userland/DevTools/UserspaceEmulator/SoftCPU.cpp39
-rw-r--r--Userland/DevTools/UserspaceEmulator/SoftCPU.h6
-rw-r--r--Userland/DevTools/UserspaceEmulator/SoftFPU.cpp12
-rw-r--r--Userland/DevTools/UserspaceEmulator/SoftMMU.cpp4
-rw-r--r--Userland/DevTools/UserspaceEmulator/ValueWithShadow.h148
7 files changed, 114 insertions, 100 deletions
diff --git a/Userland/DevTools/UserspaceEmulator/MmapRegion.cpp b/Userland/DevTools/UserspaceEmulator/MmapRegion.cpp
index 4351e0763f..7a2d6a78be 100644
--- a/Userland/DevTools/UserspaceEmulator/MmapRegion.cpp
+++ b/Userland/DevTools/UserspaceEmulator/MmapRegion.cpp
@@ -6,6 +6,7 @@
#include "MmapRegion.h"
#include "Emulator.h"
+#include <AK/ByteReader.h>
#include <string.h>
#include <sys/mman.h>
@@ -196,7 +197,7 @@ void MmapRegion::write8(u32 offset, ValueWithShadow<u8> value)
VERIFY(offset < size());
m_data[offset] = value.value();
- m_shadow_data[offset] = value.shadow();
+ m_shadow_data[offset] = value.shadow()[0];
}
void MmapRegion::write16(u32 offset, ValueWithShadow<u16> value)
diff --git a/Userland/DevTools/UserspaceEmulator/SimpleRegion.cpp b/Userland/DevTools/UserspaceEmulator/SimpleRegion.cpp
index e353e94d1f..963e021c29 100644
--- a/Userland/DevTools/UserspaceEmulator/SimpleRegion.cpp
+++ b/Userland/DevTools/UserspaceEmulator/SimpleRegion.cpp
@@ -85,7 +85,7 @@ void SimpleRegion::write8(u32 offset, ValueWithShadow<u8> value)
{
VERIFY(offset < size());
m_data[offset] = value.value();
- m_shadow_data[offset] = value.shadow();
+ m_shadow_data[offset] = value.shadow()[0];
}
void SimpleRegion::write16(u32 offset, ValueWithShadow<u16> value)
diff --git a/Userland/DevTools/UserspaceEmulator/SoftCPU.cpp b/Userland/DevTools/UserspaceEmulator/SoftCPU.cpp
index 9d66b07411..1dba8f21cf 100644
--- a/Userland/DevTools/UserspaceEmulator/SoftCPU.cpp
+++ b/Userland/DevTools/UserspaceEmulator/SoftCPU.cpp
@@ -10,6 +10,7 @@
#include <AK/Assertions.h>
#include <AK/BuiltinWrappers.h>
#include <AK/Debug.h>
+#include <AK/Format.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@@ -87,8 +88,8 @@ void SoftCPU::dump() const
{
outln(" eax={:p} ebx={:p} ecx={:p} edx={:p} ebp={:p} esp={:p} esi={:p} edi={:p} o={:d} s={:d} z={:d} a={:d} p={:d} c={:d}",
eax(), ebx(), ecx(), edx(), ebp(), esp(), esi(), edi(), of(), sf(), zf(), af(), pf(), cf());
- outln("#eax={:p} #ebx={:p} #ecx={:p} #edx={:p} #ebp={:p} #esp={:p} #esi={:p} #edi={:p} #f={}",
- eax().shadow(), ebx().shadow(), ecx().shadow(), edx().shadow(), ebp().shadow(), esp().shadow(), esi().shadow(), edi().shadow(), m_flags_tainted);
+ outln("#eax={:hex-dump} #ebx={:hex-dump} #ecx={:hex-dump} #edx={:hex-dump} #ebhex-dump={:hex-dump} #eshex-dump={:hex-dump} #esi={:hex-dump} #edi={:hex-dump} #f={}",
+ eax().shadow().span(), ebx().shadow().span(), ecx().shadow().span(), edx().shadow().span(), ebp().shadow().span(), esp().shadow().span(), esi().shadow().span(), edi().shadow().span(), m_flags_tainted);
fflush(stdout);
}
@@ -112,7 +113,7 @@ ValueWithShadow<u8> SoftCPU::read_memory8(X86::LogicalAddress address)
{
VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
auto value = m_emulator.mmu().read8(address);
- outln_if(MEMORY_DEBUG, "\033[36;1mread_memory8: @{:#04x}:{:p} -> {:#02x} ({:#02x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mread_memory8: @{:#04x}:{:p} -> {:#02x} ({:#02x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
return value;
}
@@ -120,7 +121,7 @@ ValueWithShadow<u16> SoftCPU::read_memory16(X86::LogicalAddress address)
{
VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
auto value = m_emulator.mmu().read16(address);
- outln_if(MEMORY_DEBUG, "\033[36;1mread_memory16: @{:#04x}:{:p} -> {:#04x} ({:#04x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mread_memory16: @{:#04x}:{:p} -> {:#04x} ({:#04x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
return value;
}
@@ -128,7 +129,7 @@ ValueWithShadow<u32> SoftCPU::read_memory32(X86::LogicalAddress address)
{
VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
auto value = m_emulator.mmu().read32(address);
- outln_if(MEMORY_DEBUG, "\033[36;1mread_memory32: @{:#04x}:{:p} -> {:#08x} ({:#08x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mread_memory32: @{:#04x}:{:p} -> {:#08x} ({:#08x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
return value;
}
@@ -136,7 +137,7 @@ ValueWithShadow<u64> SoftCPU::read_memory64(X86::LogicalAddress address)
{
VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
auto value = m_emulator.mmu().read64(address);
- outln_if(MEMORY_DEBUG, "\033[36;1mread_memory64: @{:#04x}:{:p} -> {:#016x} ({:#016x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mread_memory64: @{:#04x}:{:p} -> {:#016x} ({:#016x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
return value;
}
@@ -144,62 +145,62 @@ ValueWithShadow<u128> SoftCPU::read_memory128(X86::LogicalAddress address)
{
VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
auto value = m_emulator.mmu().read128(address);
- outln_if(MEMORY_DEBUG, "\033[36;1mread_memory128: @{:#04x}:{:p} -> {:#032x} ({:#032x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mread_memory128: @{:#04x}:{:p} -> {:#032x} ({:#032x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
return value;
}
ValueWithShadow<u256> SoftCPU::read_memory256(X86::LogicalAddress address)
{
VERIFY(address.selector() == 0x1b || address.selector() == 0x23 || address.selector() == 0x2b);
auto value = m_emulator.mmu().read256(address);
- outln_if(MEMORY_DEBUG, "\033[36;1mread_memory256: @{:#04x}:{:p} -> {:#064x} ({:#064x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mread_memory256: @{:#04x}:{:p} -> {:#064x} ({:#064x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
return value;
}
void SoftCPU::write_memory8(X86::LogicalAddress address, ValueWithShadow<u8> value)
{
VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
- outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory8: @{:#04x}:{:p} <- {:#02x} ({:#02x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory8: @{:#04x}:{:p} <- {:#02x} ({:#02x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
m_emulator.mmu().write8(address, value);
}
void SoftCPU::write_memory16(X86::LogicalAddress address, ValueWithShadow<u16> value)
{
VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
- outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory16: @{:#04x}:{:p} <- {:#04x} ({:#04x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory16: @{:#04x}:{:p} <- {:#04x} ({:#04x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
m_emulator.mmu().write16(address, value);
}
void SoftCPU::write_memory32(X86::LogicalAddress address, ValueWithShadow<u32> value)
{
VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
- outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory32: @{:#04x}:{:p} <- {:#08x} ({:#08x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory32: @{:#04x}:{:p} <- {:#08x} ({:#08x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
m_emulator.mmu().write32(address, value);
}
void SoftCPU::write_memory64(X86::LogicalAddress address, ValueWithShadow<u64> value)
{
VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
- outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory64: @{:#04x}:{:p} <- {:#016x} ({:#016x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory64: @{:#04x}:{:p} <- {:#016x} ({:#016x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
m_emulator.mmu().write64(address, value);
}
void SoftCPU::write_memory128(X86::LogicalAddress address, ValueWithShadow<u128> value)
{
VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
- outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory128: @{:#04x}:{:p} <- {:#032x} ({:#032x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory128: @{:#04x}:{:p} <- {:#032x} ({:#032x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
m_emulator.mmu().write128(address, value);
}
void SoftCPU::write_memory256(X86::LogicalAddress address, ValueWithShadow<u256> value)
{
VERIFY(address.selector() == 0x23 || address.selector() == 0x2b);
- outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory256: @{:#04x}:{:p} <- {:#064x} ({:#064x})\033[0m", address.selector(), address.offset(), value, value.shadow());
+ outln_if(MEMORY_DEBUG, "\033[36;1mwrite_memory256: @{:#04x}:{:p} <- {:#064x} ({:#064x})\033[0m", address.selector(), address.offset(), value, value.shadow_as_value());
m_emulator.mmu().write256(address, value);
}
void SoftCPU::push_string(StringView string)
{
- size_t space_to_allocate = round_up_to_power_of_two(string.length() + 1, 16);
+ u32 space_to_allocate = round_up_to_power_of_two(string.length() + 1, 16);
set_esp({ esp().value() - space_to_allocate, esp().shadow() });
m_emulator.mmu().copy_to_vm(esp().value(), string.characters_without_null_termination(), string.length());
m_emulator.mmu().write8({ 0x23, esp().value() + string.length() }, shadow_wrap_as_initialized((u8)'\0'));
@@ -1040,7 +1041,7 @@ void SoftCPU::BSR_reg32_RM32(const X86::Instruction& insn)
void SoftCPU::BSWAP_reg32(const X86::Instruction& insn)
{
- gpr32(insn.reg32()) = { __builtin_bswap32(gpr32(insn.reg32()).value()), __builtin_bswap32(gpr32(insn.reg32()).shadow()) };
+ gpr32(insn.reg32()) = { __builtin_bswap32(gpr32(insn.reg32()).value()), __builtin_bswap32(gpr32(insn.reg32()).shadow_as_value()) };
}
template<typename T>
@@ -1970,19 +1971,19 @@ void SoftCPU::MOVSX_reg32_RM8(const X86::Instruction& insn)
void SoftCPU::MOVZX_reg16_RM8(const X86::Instruction& insn)
{
auto src = insn.modrm().read8(*this, insn);
- gpr16(insn.reg16()) = ValueWithShadow<u16>(src.value(), 0x0100 | (src.shadow() & 0xff));
+ gpr16(insn.reg16()) = ValueWithShadow<u16>(src.value(), 0x0100 | (src.shadow_as_value() & 0xff));
}
void SoftCPU::MOVZX_reg32_RM16(const X86::Instruction& insn)
{
auto src = insn.modrm().read16(*this, insn);
- gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010000 | (src.shadow() & 0xffff));
+ gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010000 | (src.shadow_as_value() & 0xffff));
}
void SoftCPU::MOVZX_reg32_RM8(const X86::Instruction& insn)
{
auto src = insn.modrm().read8(*this, insn);
- gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010100 | (src.shadow() & 0xff));
+ gpr32(insn.reg32()) = ValueWithShadow<u32>(src.value(), 0x01010100 | (src.shadow_as_value() & 0xff));
}
void SoftCPU::MOV_AL_moff8(const X86::Instruction& insn)
diff --git a/Userland/DevTools/UserspaceEmulator/SoftCPU.h b/Userland/DevTools/UserspaceEmulator/SoftCPU.h
index f4bfa6f9b9..99c6155cb7 100644
--- a/Userland/DevTools/UserspaceEmulator/SoftCPU.h
+++ b/Userland/DevTools/UserspaceEmulator/SoftCPU.h
@@ -176,21 +176,21 @@ public:
{
if (a32)
return esi();
- return { si().value(), (u32)si().shadow() & 0xffff };
+ return { si().value(), (u32)si().shadow_as_value() & 0xffff };
}
ValueWithShadow<u32> destination_index(bool a32) const
{
if (a32)
return edi();
- return { di().value(), (u32)di().shadow() & 0xffff };
+ return { di().value(), (u32)di().shadow_as_value() & 0xffff };
}
ValueWithShadow<u32> loop_index(bool a32) const
{
if (a32)
return ecx();
- return { cx().value(), (u32)cx().shadow() & 0xffff };
+ return { cx().value(), (u32)cx().shadow_as_value() & 0xffff };
}
bool decrement_loop_index(bool a32)
diff --git a/Userland/DevTools/UserspaceEmulator/SoftFPU.cpp b/Userland/DevTools/UserspaceEmulator/SoftFPU.cpp
index 91f9d4dd9c..8b475ad280 100644
--- a/Userland/DevTools/UserspaceEmulator/SoftFPU.cpp
+++ b/Userland/DevTools/UserspaceEmulator/SoftFPU.cpp
@@ -300,7 +300,7 @@ void SoftFPU::FSTP_RM80(const X86::Instruction& insn)
f80 = insn.modrm().read128(m_cpu, insn);
*(long double*)value.bytes().data() = fpu_pop();
memcpy(f80.value().bytes().data(), &value, 10); // copy
- memset(f80.shadow().bytes().data(), 0x01, 10); // mark as initialized
+ f80.set_initialized();
insn.modrm().write128(m_cpu, insn, f80);
}
}
@@ -688,7 +688,7 @@ void SoftFPU::FDIVR_RM64(const X86::Instruction& insn)
{
if (insn.modrm().is_register()) {
// XXX this is FDIVR, Instruction decodes this weirdly
- //fpu_set(insn.modrm().register_index(), fpu_get(0) / fpu_get(insn.modrm().register_index()));
+ // fpu_set(insn.modrm().register_index(), fpu_get(0) / fpu_get(insn.modrm().register_index()));
fpu_set(insn.modrm().register_index(), fpu_get(insn.modrm().register_index()) / fpu_get(0));
} else {
auto new_f64 = insn.modrm().read64(m_cpu, insn);
@@ -1167,13 +1167,13 @@ void SoftFPU::FNSTENV(const X86::Instruction& insn)
* | | TW | 8
* +----------------+---------------+
* | FIP | 12
- * +----+-----------+---------------+
+ * +----+-----------+---------------+
* |0000|fpuOp[10:0]| FIP_sel | 16
- * +----+-----------+---------------+
+ * +----+-----------+---------------+
* | FDP | 20
- * +----------------+---------------+
+ * +----------------+---------------+
* | | FDP_ds | 24
- * +----------------|---------------+
+ * +----------------|---------------+
* */
auto address = insn.modrm().resolve(m_cpu, insn);
diff --git a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp
index e1017a3a88..bf9e75d553 100644
--- a/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp
+++ b/Userland/DevTools/UserspaceEmulator/SoftMMU.cpp
@@ -347,7 +347,7 @@ bool SoftMMU::fast_fill_memory8(X86::LogicalAddress address, size_t size, ValueW
size_t offset_in_region = address.offset() - region->base();
memset(region->data() + offset_in_region, value.value(), size);
- memset(region->shadow_data() + offset_in_region, value.shadow(), size);
+ memset(region->shadow_data() + offset_in_region, value.shadow()[0], size);
return true;
}
@@ -372,7 +372,7 @@ bool SoftMMU::fast_fill_memory32(X86::LogicalAddress address, size_t count, Valu
size_t offset_in_region = address.offset() - region->base();
fast_u32_fill((u32*)(region->data() + offset_in_region), value.value(), count);
- fast_u32_fill((u32*)(region->shadow_data() + offset_in_region), value.shadow(), count);
+ fast_u32_fill((u32*)(region->shadow_data() + offset_in_region), value.shadow_as_value(), count);
return true;
}
diff --git a/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h b/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h
index 06733ce65b..7c64d3d6a1 100644
--- a/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h
+++ b/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h
@@ -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 shadow_as_value() const requires(IsTriviallyConstructible<T>)
+ {
+ return *bit_cast<T const*>(m_shadow.data());
+ }
+
T& value() { return m_value; }
- T& shadow() { return m_shadow; }
+ ShadowType& shadow() { return m_shadow; }
- const T& value() const { return m_value; }
- const T& shadow() const { 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