-
Notifications
You must be signed in to change notification settings - Fork 850
Expand file tree
/
Copy pathruntime-memory.cpp
More file actions
316 lines (291 loc) · 10 KB
/
runtime-memory.cpp
File metadata and controls
316 lines (291 loc) · 10 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* Copyright 2026 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ir/runtime-memory.h"
#include "fp16.h"
#include "interpreter/exception.h"
#include <iostream>
namespace wasm {
namespace {
[[noreturn]] void trap(std::string_view reason) {
std::cout << "[trap " << reason << "]\n";
throw TrapException{};
}
void checkAtomicAddress(const RuntimeMemory& runtimeMemory,
Address finalAddr,
Index bytes) {
// Unaligned atomics trap.
if (bytes > 1) {
if (finalAddr & (bytes - 1)) {
trap("unaligned atomic operation");
}
}
}
template<typename T> bool aligned(const uint8_t* address) {
static_assert(!(sizeof(T) & (sizeof(T) - 1)), "must be a power of 2");
return 0 == (reinterpret_cast<uintptr_t>(address) & (sizeof(T) - 1));
}
} // namespace
RealRuntimeMemory::RealRuntimeMemory(Memory memory)
: RuntimeMemory(std::move(memory)) {
resize(memoryDefinition.initialByteSize());
}
Literal RealRuntimeMemory::load(Address addr,
Address offset,
uint8_t byteCount,
MemoryOrder order,
Type type,
bool signed_) const {
Address final = validateAddress(addr, offset, byteCount);
if (order != MemoryOrder::Unordered) {
checkAtomicAddress(*this, final, byteCount);
}
switch (type.getBasic()) {
case Type::i32: {
switch (byteCount) {
case 1:
return signed_ ? Literal((int32_t)get<int8_t>(final))
: Literal((int32_t)get<uint8_t>(final));
case 2:
return signed_ ? Literal((int32_t)get<int16_t>(final))
: Literal((int32_t)get<uint16_t>(final));
case 4:
return Literal((int32_t)get<int32_t>(final));
default:
WASM_UNREACHABLE("invalid size");
}
}
case Type::i64: {
switch (byteCount) {
case 1:
return signed_ ? Literal((int64_t)get<int8_t>(final))
: Literal((int64_t)get<uint8_t>(final));
case 2:
return signed_ ? Literal((int64_t)get<int16_t>(final))
: Literal((int64_t)get<uint16_t>(final));
case 4:
return signed_ ? Literal((int64_t)get<int32_t>(final))
: Literal((int64_t)get<uint32_t>(final));
case 8:
return Literal((int64_t)get<int64_t>(final));
default:
WASM_UNREACHABLE("invalid size");
}
}
case Type::f32: {
switch (byteCount) {
case 2:
return Literal(bit_cast<int32_t>(
fp16_ieee_to_fp32_value(get<uint16_t>(final))))
.castToF32();
case 4:
return Literal(get<uint32_t>(final)).castToF32();
default:
WASM_UNREACHABLE("invalid size");
}
}
case Type::f64:
return Literal(get<uint64_t>(final)).castToF64();
case Type::v128:
return Literal(get<std::array<uint8_t, 16>>(final).data());
default:
WASM_UNREACHABLE("unexpected type");
}
}
void RealRuntimeMemory::store(Address addr,
Address offset,
uint8_t byteCount,
MemoryOrder order,
Literal value,
Type type) {
Address final = validateAddress(addr, offset, byteCount);
if (order != MemoryOrder::Unordered) {
checkAtomicAddress(*this, final, byteCount);
}
switch (type.getBasic()) {
case Type::i32: {
switch (byteCount) {
case 1:
set<int8_t>(final, value.geti32());
break;
case 2:
set<int16_t>(final, value.geti32());
break;
case 4:
set<int32_t>(final, value.geti32());
break;
default:
WASM_UNREACHABLE("invalid size");
}
break;
}
case Type::i64: {
switch (byteCount) {
case 1:
set<int8_t>(final, value.geti64());
break;
case 2:
set<int16_t>(final, value.geti64());
break;
case 4:
set<int32_t>(final, value.geti64());
break;
case 8:
set<int64_t>(final, value.geti64());
break;
default:
WASM_UNREACHABLE("invalid size");
}
break;
}
case Type::f32: {
switch (byteCount) {
case 2:
set<uint16_t>(final,
fp16_ieee_from_fp32_value(
bit_cast<float>(value.reinterpreti32())));
break;
case 4:
set<int32_t>(final, value.reinterpreti32());
break;
default:
WASM_UNREACHABLE("invalid size");
}
break;
}
case Type::f64:
set<int64_t>(final, value.reinterpreti64());
break;
case Type::v128:
set<std::array<uint8_t, 16>>(final, value.getv128());
break;
default:
WASM_UNREACHABLE("unexpected type");
}
}
bool RealRuntimeMemory::grow(Address delta) {
Address pageSize = memoryDefinition.pageSize();
Address oldPages = intendedSize / pageSize;
Address newPages = oldPages + delta;
if (newPages > memoryDefinition.max && memoryDefinition.hasMax()) {
return false;
}
// Apply a reasonable limit on memory size, 1GB, to avoid DOS on the
// interpreter.
if (newPages * pageSize > 1024 * 1024 * 1024) {
return false;
}
resize(newPages * pageSize);
return true;
}
Address RealRuntimeMemory::size() const { return intendedSize; }
void RealRuntimeMemory::init(Address dest,
Address src,
Address byteCount,
const DataSegment* data) {
if (src > data->data.size() || byteCount > data->data.size() - src) {
trap("out of bounds segment access in memory.init");
}
Address final = validateAddress(dest, 0, byteCount);
if (byteCount > 0) {
std::memcpy(&memory[final], &data->data[src], byteCount);
}
}
void RealRuntimeMemory::copy(Address dest,
Address src,
Address byteCount,
const RuntimeMemory* srcMemory) {
Address finalDest = validateAddress(dest, 0, byteCount);
if (byteCount > 0) {
srcMemory->copyTo(&memory[finalDest], src, byteCount);
} else {
// still need to validate src even for 0-byte copy
srcMemory->validateAddress(src, 0, 0);
}
}
void RealRuntimeMemory::fill(Address dest, uint8_t value, Address byteCount) {
Address final = validateAddress(dest, 0, byteCount);
if (byteCount > 0) {
std::memset(&memory[final], value, byteCount);
}
}
void RealRuntimeMemory::copyTo(uint8_t* dest, Address src, Address byteCount) const {
Address finalSrc = validateAddress(src, 0, byteCount);
if (byteCount > 0 && dest) {
std::memcpy(dest, &memory[finalSrc], byteCount);
}
}
Address RealRuntimeMemory::validateAddress(Address addr, Address offset, Address byteCount) const {
Address memorySizeBytes = size();
if (offset > memorySizeBytes || addr > memorySizeBytes - offset) {
trap("out of bounds memory access");
}
addr = size_t(addr) + offset;
if (byteCount > memorySizeBytes - addr) {
trap("out of bounds memory access");
}
return addr;
}
void RealRuntimeMemory::resize(size_t newSize) {
intendedSize = newSize;
const size_t minSize = 1 << 12;
size_t oldAllocatedSize = memory.size();
size_t newAllocatedSize = std::max(minSize, newSize);
if (newAllocatedSize > oldAllocatedSize) {
memory.resize(newAllocatedSize);
std::memset(&memory[oldAllocatedSize], 0, newAllocatedSize - oldAllocatedSize);
}
if (newSize < oldAllocatedSize && newSize < minSize) {
std::memset(&memory[newSize], 0, minSize - newSize);
}
}
template<typename T> T RealRuntimeMemory::get(size_t address) const {
if (aligned<T>(&memory[address])) {
return *reinterpret_cast<const T*>(&memory[address]);
} else {
T loaded;
std::memcpy(&loaded, &memory[address], sizeof(T));
return loaded;
}
}
template<typename T> void RealRuntimeMemory::set(size_t address, T value) {
if (aligned<T>(&memory[address])) {
*reinterpret_cast<T*>(&memory[address]) = value;
} else {
std::memcpy(&memory[address], &value, sizeof(T));
}
}
// Explicit instantiations for the templates
template int8_t RealRuntimeMemory::get<int8_t>(size_t) const;
template uint8_t RealRuntimeMemory::get<uint8_t>(size_t) const;
template int16_t RealRuntimeMemory::get<int16_t>(size_t) const;
template uint16_t RealRuntimeMemory::get<uint16_t>(size_t) const;
template int32_t RealRuntimeMemory::get<int32_t>(size_t) const;
template uint32_t RealRuntimeMemory::get<uint32_t>(size_t) const;
template int64_t RealRuntimeMemory::get<int64_t>(size_t) const;
template uint64_t RealRuntimeMemory::get<uint64_t>(size_t) const;
template std::array<uint8_t, 16>
RealRuntimeMemory::get<std::array<uint8_t, 16>>(size_t) const;
template void RealRuntimeMemory::set<int8_t>(size_t, int8_t);
template void RealRuntimeMemory::set<uint8_t>(size_t, uint8_t);
template void RealRuntimeMemory::set<int16_t>(size_t, int16_t);
template void RealRuntimeMemory::set<uint16_t>(size_t, uint16_t);
template void RealRuntimeMemory::set<int32_t>(size_t, int32_t);
template void RealRuntimeMemory::set<uint32_t>(size_t, uint32_t);
template void RealRuntimeMemory::set<int64_t>(size_t, int64_t);
template void RealRuntimeMemory::set<uint64_t>(size_t, uint64_t);
template void
RealRuntimeMemory::set<std::array<uint8_t, 16>>(size_t, std::array<uint8_t, 16>);
} // namespace wasm