-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.cpp
370 lines (331 loc) · 13.7 KB
/
instance.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "instance.hpp"
#include "./module.hpp"
#include "interfacing.hpp"
#include "runtime.hpp"
#include "spec.hpp"
namespace mitey {
auto Instance::initial_stack = Allocation(nullptr, [](auto) {});
Instance::Instance(std::shared_ptr<Module> module)
: module(module), misc(std::make_unique<void *[]>(
1 + module->functions.size() + module->tables.size() +
module->globals.size() + module->elements.size() +
module->data_segments.size())),
memory(nullptr), functions(module->functions.size()),
globals(module->globals.size()), elements(module->elements.size()),
tables(module->tables.size()) {}
void Instance::initialize(const runtime::Imports &imports) {
auto prev = runtime::trap_buf;
std::jmp_buf buf;
runtime::trap_buf = &buf;
auto result = static_cast<runtime::TrapKind>(setjmp(*runtime::trap_buf));
if (result != runtime::TrapKind::success) {
error<uninstantiable_error>(runtime::trap_kind_to_string(result));
}
void **misc_ptr = misc.get();
auto misc_memory = reinterpret_cast<runtime::WasmMemory **>(misc_ptr);
// only used for imported functions
// good opportunity for tiering up though
auto misc_functions = reinterpret_cast<runtime::Funcref *>(misc_ptr += 1);
auto misc_tables = reinterpret_cast<runtime::WasmTable **>(
misc_ptr += module->functions.size());
auto misc_globals = reinterpret_cast<runtime::WasmValue **>(
misc_ptr += module->tables.size());
auto misc_elements = reinterpret_cast<runtime::ElementSegment **>(
misc_ptr += module->globals.size());
auto misc_segments = reinterpret_cast<runtime::Segment **>(
misc_ptr += module->elements.size());
auto get_import = [&](const ImportSpecifier &specifier) {
auto [module_name, field_name] = specifier;
if (!imports.contains(module_name)) {
error<link_error>("unknown import");
}
auto &import_module = imports.at(module_name);
if (!import_module.contains(field_name)) {
error<link_error>("unknown import");
}
auto &import = import_module.at(field_name);
if (static_cast<mitey::ImExDesc>(import.index()) !=
module->imports.at(module_name).at(field_name)) {
error<link_error>("incompatible import type: descriptor incorrect");
}
return import;
};
if (!module->memory.exists) {
memory = std::make_shared<runtime::WasmMemory>();
} else if (module->memory.import) {
auto imported_memory = std::get<std::shared_ptr<runtime::WasmMemory>>(
get_import(*module->memory.import));
if (imported_memory->size() < module->memory.min ||
imported_memory->max() > module->memory.max) {
error<link_error>(
"incompatible import type: memory size doesn't fit");
}
memory = imported_memory;
} else {
memory = std::make_shared<runtime::WasmMemory>(module->memory.min,
module->memory.max);
}
*misc_memory = memory.get();
for (uint32_t i = 0; i < functions.size(); i++) {
const auto &fn = module->functions[i];
if (fn.import) {
auto imported_function =
std::get<runtime::FunctionInfo>(get_import(*fn.import));
if (imported_function.type != runtime::FunctionType(fn.type)) {
error<link_error>(
"incompatible import type: function type doesn't match");
}
functions[i] = imported_function;
if (!functions[i].instance) {
functions[i].memory = memory.get()->memory.get();
functions[i].misc = misc.get();
functions[i].instance = self.lock();
}
} else {
auto memory = this->memory.get()->memory.get();
auto misc = this->misc.get();
functions[i] = runtime::FunctionInfo(
runtime::FunctionType(fn.type), memory, misc,
reinterpret_cast<runtime::TemplessSignature *>(fn.start),
self.lock());
}
misc_functions[i] = &functions[i];
}
for (uint32_t i = 0; i < globals.size(); i++) {
const auto &global = module->globals[i];
if (global.import) {
auto imported_global =
std::get<std::shared_ptr<runtime::WasmGlobal>>(
get_import(*global.import));
if (imported_global->type != global.type ||
imported_global->_mut != global.mutability) {
error<link_error>("incompatible import type: global mutability "
"doesn't match");
}
globals[i] = imported_global;
} else {
globals[i] = std::make_shared<runtime::WasmGlobal>(
global.type, global.mutability,
interpret_const_inplace(global.initializer));
}
misc_globals[i] = &(globals[i].get()->value);
}
for (uint32_t i = 0; i < tables.size(); i++) {
const auto &table = module->tables[i];
if (table.import) {
auto imported_table = std::get<std::shared_ptr<runtime::WasmTable>>(
get_import(*table.import));
if (imported_table->size() < table.min ||
imported_table->max() > table.max ||
imported_table->type != table.type) {
error<link_error>(
"incompatible import type: table size doesn't fit");
}
tables[i] = imported_table;
} else {
tables[i] = std::make_shared<runtime::WasmTable>(
table.type, table.min, table.max);
}
misc_tables[i] = tables[i].get();
}
auto iter = module->element_start;
for (uint32_t i = 0; i < elements.size(); i++) {
auto flags = safe_read_leb128<uint32_t>(iter);
if (flags & 1) {
if (flags & 0b10) {
if (flags & 0b100) {
// flags = 7
// characteristics: declarative, elem type + exprs
auto reftype = static_cast<valtype>(*iter++);
auto n_elements = safe_read_leb128<uint32_t>(iter);
for (uint32_t j = 0; j < n_elements; j++) {
interpret_const(iter);
}
elements[i] = runtime::ElementSegment(reftype, 0, nullptr);
} else {
// flags = 3
// characteristics: declarative, elem kind + indices
/* uint8_t elemkind = * */ iter++;
auto n_elements = safe_read_leb128<uint32_t>(iter);
for (uint32_t j = 0; j < n_elements; j++) {
safe_read_leb128<uint32_t>(iter);
}
elements[i] =
runtime::ElementSegment(valtype::funcref, 0, nullptr);
}
} else {
if (flags & 0b100) {
// flags = 5
// characteristics: passive, elem type + exprs
auto reftype = static_cast<valtype>(*iter++);
auto n_elements = safe_read_leb128<uint32_t>(iter);
auto elem =
std::make_unique<runtime::WasmValue[]>(n_elements);
for (uint32_t j = 0; j < n_elements; j++) {
elem[j] = interpret_const(iter);
}
elements[i] = runtime::ElementSegment(reftype, n_elements,
std::move(elem));
} else {
// flags = 1
// characteristics: passive, elem kind + indices
/* uint8_t elemkind = * */ iter++;
auto n_elements = safe_read_leb128<uint32_t>(iter);
auto elem =
std::make_unique<runtime::WasmValue[]>(n_elements);
for (uint32_t j = 0; j < n_elements; j++) {
elem[j] = &functions[safe_read_leb128<uint32_t>(iter)];
}
elements[i] = runtime::ElementSegment(
valtype::funcref, n_elements, std::move(elem));
}
}
} else {
auto reftype = valtype::null;
auto table =
tables[flags & 0b10 ? safe_read_leb128<uint32_t>(iter) : 0];
auto offset = interpret_const(iter).u32;
auto reftype_or_elemkind = flags & 0b10 ? *iter++ : 256;
auto n_elements = safe_read_leb128<uint32_t>(iter);
if (offset + n_elements > table->size()) {
error<uninstantiable_error>("out of bounds table access");
}
if (flags & 0b100) {
// flags = 4 or 6
// characteristics: active, elem type + exprs
if (reftype_or_elemkind == 256)
reftype_or_elemkind =
static_cast<uint16_t>(valtype::funcref);
reftype = static_cast<valtype>(reftype_or_elemkind);
for (uint32_t j = 0; j < n_elements; j++) {
auto ref = interpret_const(iter);
table->set(offset + j, ref);
}
} else {
if (reftype_or_elemkind == 256)
reftype_or_elemkind = 0;
reftype = valtype::funcref;
// flags = 0 or 2
// characteristics: active, elem kind + indices
for (uint32_t j = 0; j < n_elements; j++) {
auto elem_idx = safe_read_leb128<uint32_t>(iter);
auto funcref = &functions[elem_idx];
table->set(offset + j, funcref);
}
}
elements[i] = runtime::ElementSegment(reftype, 0, nullptr);
}
misc_elements[i] = &elements[i];
}
for (uint32_t i = 0; i < module->data_segments.size(); i++) {
auto &data = module->data_segments[i];
if (data.initializer) {
auto offset = interpret_const_inplace(data.initializer).u32;
memory->copy_into(offset, 0, data, data.size);
misc_segments[i] = &runtime::Segment::empty;
} else {
misc_segments[i] = &data;
}
}
for (const auto &[name, export_] : module->exports) {
switch (export_.desc) {
case ImExDesc::func:
exports.insert({name, functions[export_.idx]});
break;
case ImExDesc::table:
exports.insert({name, tables[export_.idx]});
break;
case ImExDesc::mem:
exports.insert({name, memory});
break;
case ImExDesc::global:
exports.insert({name, globals[export_.idx]});
break;
}
}
if (module->start != std::numeric_limits<uint32_t>::max()) {
const auto &fn = functions[module->start];
if (fn.type.params || fn.type.results) {
error<validation_error>("start function");
}
auto stack =
reinterpret_cast<runtime::WasmValue *>(initial_stack.get());
fn.signature(fn.memory, fn.misc, stack);
}
runtime::trap_buf = prev;
}
runtime::WasmValue Instance::interpret_const(uint8_t *&iter) {
auto stack = std::vector<runtime::WasmValue>();
#define OP(ty, op) \
{ \
auto arg1 = stack.back(); \
stack.pop_back(); \
auto arg2 = stack.back(); \
stack.pop_back(); \
stack.push_back(arg1.ty op arg2.ty); \
break; \
}
#define I32_OP(op) OP(i32, op)
#define I64_OP(op) OP(i64, op)
while (1) {
auto byte = *iter++;
using enum Instruction;
if (static_cast<Instruction>(byte) == end) {
break;
}
switch (static_cast<Instruction>(byte)) {
case i32const:
stack.push_back(safe_read_sleb128<int32_t>(iter));
break;
case i64const:
stack.push_back(safe_read_sleb128<int64_t>(iter));
break;
case f32const: {
float x;
std::memcpy(&x, iter, sizeof(float));
stack.push_back(x);
iter += sizeof(float);
break;
}
case f64const: {
double x;
std::memcpy(&x, iter, sizeof(double));
stack.push_back(x);
iter += sizeof(double);
break;
}
case globalget:
stack.push_back(globals[safe_read_leb128<int32_t>(iter)]->value);
break;
case i32add:
I32_OP(+);
case i32sub:
I32_OP(-);
case i32mul:
I32_OP(*);
case i64add:
I64_OP(+);
case i64sub:
I64_OP(-);
case i64mul:
I64_OP(*);
case ref_null: {
safe_read_leb128<uint32_t>(iter);
stack.push_back(Externref(nullptr));
break;
}
case ref_func: {
uint32_t func_idx = safe_read_leb128<uint32_t>(iter);
stack.push_back(&functions[func_idx]);
break;
}
default:
__builtin_unreachable();
}
}
return stack.back();
#undef OP
#undef I32_OP
#undef I64_OP
}
} // namespace mitey