summaryrefslogtreecommitdiff
path: root/Userland/DevTools/UserspaceEmulator/ValueWithShadow.h
blob: d5233c936ebb37f3682d9931b4d0fe6e4fe7b2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Format.h>
#include <AK/Platform.h>
#include <AK/UFixedBigInt.h>
#include <string.h>

namespace UserspaceEmulator {

template<typename T>
class ValueAndShadowReference;

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)
    {
    }

    static ValueWithShadow create_initialized(T value)
    {
        ShadowType shadow;
        shadow.fill(0x01);
        return {
            value,
            shadow,
        };
    }

    ValueWithShadow(ValueAndShadowReference<T> const&);

    T value() const { return m_value; }
    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
    {
        for (size_t i = 0; i < sizeof(ShadowType); ++i) {
            if ((m_shadow[i] & 0x01) != 0x01)
                return true;
        }
        return false;
    }

    void set_initialized()
    {
        m_shadow.fill(0x01);
    }

private:
    T m_value {};
    ShadowType m_shadow {};
};

template<typename T>
class ValueAndShadowReference {
public:
    using ValueType = T;
    using ShadowType = Array<u8, sizeof(T)>;

    ValueAndShadowReference(T& value, ShadowType& shadow)
        : m_value(value)
        , m_shadow(shadow)
    {
    }

    bool is_uninitialized() const
    {
        for (size_t i = 0; i < sizeof(ShadowType); ++i) {
            if ((m_shadow[i] & 0x01) != 0x01)
                return true;
        }
        return false;
    }

    ValueAndShadowReference<T>& operator=(ValueWithShadow<T> const&);

    T shadow_as_value() const
    requires(IsTriviallyConstructible<T>)
    {
        return *bit_cast<T const*>(m_shadow.data());
    }

    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;
    ShadowType& m_shadow;
};

template<typename T>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_as_initialized(T value)
{
    return ValueWithShadow<T>::create_initialized(value);
}

template<typename T, typename U>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, U const& taint_a)
{
    if (taint_a.is_uninitialized())
        return { value, 0 };
    return shadow_wrap_as_initialized(value);
}

template<typename T, typename U, typename V>
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 };
    return shadow_wrap_as_initialized(value);
}

template<typename T, typename U, typename V, typename X>
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 };
    return shadow_wrap_as_initialized(value);
}

template<typename T>
inline ValueWithShadow<T>::ValueWithShadow(ValueAndShadowReference<T> const& other)
    : m_value(other.value())
    , m_shadow(other.shadow())
{
}

template<typename T>
inline ValueAndShadowReference<T>& ValueAndShadowReference<T>::operator=(ValueWithShadow<T> const& other)
{
    m_value = other.value();
    m_shadow = other.shadow();
    return *this;
}

}

template<typename T>
struct AK::Formatter<UserspaceEmulator::ValueWithShadow<T>> : AK::Formatter<T> {
    ErrorOr<void> format(FormatBuilder& builder, UserspaceEmulator::ValueWithShadow<T> value)
    {
        return Formatter<T>::format(builder, value.value());
    }
};