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
|
/*
* 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 {
constexpr u64 _inititalized_64 = 0x01010101'01010101LLU;
constexpr u128 _initialized_128 = u128(_inititalized_64, _inititalized_64);
constexpr u256 _initialized_256 = u256(_initialized_128, _initialized_128);
template<typename T>
class ValueAndShadowReference;
template<typename T>
class ValueWithShadow {
public:
using ValueType = T;
ValueWithShadow(T value, T shadow)
: m_value(value)
, m_shadow(shadow)
{
}
ValueWithShadow(const ValueAndShadowReference<T>&);
T value() const { return m_value; }
T shadow() const { return m_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 & _inititalized_64) != _inititalized_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;
}
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 = _inititalized_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;
}
private:
T m_value;
T m_shadow;
};
template<typename T>
class ValueAndShadowReference {
public:
using ValueType = T;
ValueAndShadowReference(T& value, T& shadow)
: m_value(value)
, m_shadow(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 & _inititalized_64) != _inititalized_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;
}
void operator=(const ValueWithShadow<T>&);
T& value() { return m_value; }
T& shadow() { return m_shadow; }
const T& value() const { return m_value; }
const T& shadow() const { return m_shadow; }
private:
T& m_value;
T& 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, _inititalized_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 };
}
template<typename T, typename U>
ALWAYS_INLINE ValueWithShadow<T> shadow_wrap_with_taint_from(T value, const U& 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, const U& taint_a, const V& 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, const U& taint_a, const V& taint_b, const X& 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(const ValueAndShadowReference<T>& other)
: m_value(other.value())
, m_shadow(other.shadow())
{
}
template<typename T>
inline void ValueAndShadowReference<T>::operator=(const ValueWithShadow<T>& other)
{
m_value = other.value();
m_shadow = other.shadow();
}
}
template<typename T>
struct AK::Formatter<UserspaceEmulator::ValueWithShadow<T>> : AK::Formatter<T> {
void format(FormatBuilder& builder, UserspaceEmulator::ValueWithShadow<T> value)
{
return Formatter<T>::format(builder, value.value());
}
};
#undef INITIALIZED_64
#undef INITIALIZED_128
#undef INITIALIZED_256
|