-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.hpp
471 lines (394 loc) · 13.4 KB
/
module.hpp
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#pragma once
#include "spec.hpp"
#include <functional>
#include <memory>
#include <optional>
#include <span>
#include <tuple>
#include <variant>
#include <vector>
#ifdef __SIZEOF_INT128__
using cmp128_t = __int128_t;
#else
#include <array>
using cmp128_t = std::array<uint64_t, 2>;
#endif
namespace mitey {
template <typename T> struct function_traits;
template <typename R, typename... Args> struct function_traits<R (*)(Args...)> {
using args = std::tuple<Args...>;
using return_type = R;
};
template <template <typename...> class T, typename U>
constexpr bool is_specialization_of = false;
template <template <typename...> class T, typename... Us>
constexpr bool is_specialization_of<T, T<Us...>> = true;
class Instance;
union RuntimeType {
struct {
uint32_t n_params;
uint32_t n_results;
bool has_i32 : 1;
bool has_i64 : 1;
bool has_f32 : 1;
bool has_f64 : 1;
bool has_funcref : 1;
bool has_externref : 1;
uint64_t hash : 64 - 6;
};
cmp128_t cmp;
bool operator==(const RuntimeType &other) const { return cmp == other.cmp; }
template <typename Types, typename Iter>
static RuntimeType read_blocktype(Types &types, Iter &iter) {
constexpr uint8_t empty_type = 0x40;
uint8_t byte = *iter;
if (byte == empty_type) {
++iter;
return RuntimeType{{0, 0, 0, 0, 0, 0, 0, 0, 0}};
} else if (is_valtype(byte)) {
++iter;
return RuntimeType{{0, 1, 0, 0, 0, 0, 0, 0, byte}};
} else {
int64_t n = read_leb128(iter);
return types[n];
}
}
static RuntimeType from_signature(const Signature &sig) {
RuntimeType type;
type.n_params = sig.params.size();
type.n_results = sig.results.size();
type.has_i32 = type.has_i64 = type.has_f32 = type.has_f64 =
type.has_funcref = type.has_externref = false;
type.hash = 0;
for (valtype param : sig.params) {
switch (param) {
case valtype::i32:
type.has_i32 = true;
break;
case valtype::i64:
type.has_i64 = true;
break;
case valtype::f32:
type.has_f32 = true;
break;
case valtype::f64:
type.has_f64 = true;
break;
case valtype::funcref:
type.has_funcref = true;
break;
case valtype::externref:
type.has_externref = true;
break;
case valtype::null:
case valtype::any:
error<std::runtime_error>("invalid result type");
}
type.hash *= 16777619;
type.hash ^= static_cast<uint64_t>(param);
}
type.hash *= 31;
for (valtype result : sig.results) {
switch (result) {
case valtype::i32:
type.has_i32 = true;
break;
case valtype::i64:
type.has_i64 = true;
break;
case valtype::f32:
type.has_f32 = true;
break;
case valtype::f64:
type.has_f64 = true;
break;
case valtype::funcref:
type.has_funcref = true;
break;
case valtype::externref:
type.has_externref = true;
break;
case valtype::null:
case valtype::any:
error<std::runtime_error>("invalid result type");
}
type.hash *= 31;
type.hash ^= static_cast<uint64_t>(result);
}
return type;
}
};
struct FunctionInfo;
using Funcref = FunctionInfo *;
using Externref = void *;
// technically unsigned versions don't exist but easier to use if they're here
union WasmValue {
int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
float f32;
double f64;
Funcref funcref;
Externref externref;
WasmValue() : u64(0) {}
WasmValue(int32_t i32) : i32(i32) {}
WasmValue(uint32_t u32) : u32(u32) {}
WasmValue(int64_t i64) : i64(i64) {}
WasmValue(uint64_t u64) : u64(u64) {}
WasmValue(float f32) : f32(f32) {}
WasmValue(double f64) : f64(f64) {}
WasmValue(Funcref funcref) : funcref(funcref) {}
WasmValue(Externref externref) : externref(externref) {}
operator int32_t() { return i32; }
operator uint32_t() { return u32; }
operator int64_t() { return i64; }
operator uint64_t() { return u64; }
operator float() { return f32; }
operator double() { return f64; }
operator Funcref() { return funcref; }
operator Externref() { return externref; }
};
struct wasm_function {
std::shared_ptr<Instance> instance;
uint8_t *start = nullptr;
uint32_t n_locals = 0;
operator bool() const { return start != nullptr; }
};
// obtained via wasm_functionify<func>
using static_host_function = void(WasmValue *);
// obtained via wasm_functionify(func);
using dynamic_host_function = std::function<static_host_function>;
struct FunctionInfo {
RuntimeType type;
wasm_function wasm_fn;
static_host_function *static_fn = nullptr;
dynamic_host_function dyn_fn = nullptr;
FunctionInfo() = default;
FunctionInfo(Signature type, std::shared_ptr<Instance> instance,
uint8_t *start, std::vector<valtype> locals)
: FunctionInfo(RuntimeType::from_signature(type), instance, start,
locals) {}
FunctionInfo(RuntimeType type, std::shared_ptr<Instance> instance,
uint8_t *start, std::vector<valtype> locals)
: type(type), wasm_fn{instance, start, (uint32_t)locals.size()} {}
FunctionInfo(Signature type, dynamic_host_function fn)
: FunctionInfo(RuntimeType::from_signature(type), fn) {}
FunctionInfo(RuntimeType type, dynamic_host_function fn)
: type(type), dyn_fn(fn) {}
FunctionInfo(Signature type, static_host_function fn)
: FunctionInfo(RuntimeType::from_signature(type), fn) {}
FunctionInfo(RuntimeType type, static_host_function fn)
: type(type), static_fn(fn) {}
template <typename FunctionType> std::function<FunctionType> to() const {
using Traits = function_traits<FunctionType *>;
using ReturnType = typename Traits::return_type;
bool call_static = static_fn != nullptr;
return [this, call_static](auto... args) {
constexpr bool is_multivalue =
is_specialization_of<std::tuple, ReturnType>;
constexpr size_t num_args =
std::tuple_size_v<typename Traits::args>;
constexpr size_t num_results = [=] {
if constexpr (std::is_void_v<ReturnType>)
return 0;
if constexpr (!is_multivalue)
return 1;
else
return std::tuple_size_v<ReturnType>;
}();
void *buffer =
alloca(sizeof(WasmValue) * std::max(num_args, num_results));
WasmValue *stack = reinterpret_cast<WasmValue *>(buffer);
push_tuple_to_wasm(std::make_tuple(args...), stack,
std::make_index_sequence<sizeof...(args)>{});
if (call_static) {
static_fn(stack);
} else {
dyn_fn(stack);
}
if constexpr (is_multivalue) {
return [&]<size_t... I>(std::index_sequence<I...>) {
return ReturnType{(stack[I])...};
}(std::make_index_sequence<num_results>{});
} else if constexpr (!std::is_void_v<ReturnType>) {
return stack[0];
}
};
}
std::function<std::vector<WasmValue>(const std::vector<WasmValue> &)>
to() const {
if (wasm_fn) {
trap("non-exported wasm functions cannot be called from the host");
}
if (!static_fn && !dyn_fn) {
trap("function has no implementation");
}
if (static_fn && dyn_fn) {
trap("function has both static and dynamic implementations");
}
bool call_static = static_fn != nullptr;
return [this, call_static](const std::vector<WasmValue> &args) {
if (args.size() != type.n_params) {
trap("invalid number of arguments");
}
// todo: this should absolutely not be a dynamic allocation but
// alloca was throwing some shit
auto stack = static_cast<WasmValue *>(
alloca(sizeof(WasmValue) *
std::max(args.size(), (size_t)type.n_results)));
std::copy(args.begin(), args.end(), stack);
if (call_static) {
static_fn(stack);
} else {
dyn_fn(stack);
}
return std::vector<WasmValue>(stack, stack + type.n_results);
};
}
};
class safe_byte_iterator {
uint8_t *iter;
uint8_t *end;
public:
safe_byte_iterator(uint8_t *ptr, size_t length);
safe_byte_iterator(uint8_t *ptr, uint8_t *end);
uint8_t operator*() const;
uint8_t operator[](ssize_t n) const;
safe_byte_iterator &operator++();
safe_byte_iterator operator++(int);
safe_byte_iterator operator+(size_t n) const;
safe_byte_iterator &operator+=(size_t n);
ptrdiff_t operator-(safe_byte_iterator other) const;
ptrdiff_t operator-(const uint8_t *other) const;
bool operator<(safe_byte_iterator other) const;
uint8_t *get_with_at_least(size_t n) const;
bool empty() const;
bool has_n_left(size_t n) const;
uint8_t *unsafe_ptr() const { return iter; }
};
enum class ImExDesc {
func,
table,
mem,
global,
};
static inline bool is_imexdesc(uint8_t byte) {
return byte == static_cast<uint8_t>(ImExDesc::func) ||
byte == static_cast<uint8_t>(ImExDesc::table) ||
byte == static_cast<uint8_t>(ImExDesc::mem) ||
byte == static_cast<uint8_t>(ImExDesc::global);
}
using ImportSpecifier = std::pair<std::string, std::string>;
struct FunctionShell {
uint8_t *start;
Signature type;
std::vector<valtype> locals;
std::optional<ImportSpecifier> import;
bool is_declared = false;
};
struct TableShell {
uint32_t min;
uint32_t max;
valtype type;
std::optional<ImportSpecifier> import;
};
struct MemoryShell {
uint32_t min;
uint32_t max;
bool exists;
std::optional<ImportSpecifier> import;
};
struct GlobalShell {
valtype type;
mut mutability;
uint8_t *initializer;
std::optional<ImportSpecifier> import;
};
struct ExportShell {
ImExDesc desc;
uint32_t idx;
};
struct ElementShell {
valtype type;
};
struct Segment {
uint32_t memidx;
std::span<uint8_t> data;
uint8_t *initializer;
};
struct IfJump {
uint8_t *else_;
uint8_t *end;
};
class WasmTable;
class WasmMemory;
struct WasmGlobal;
using ExportValue =
std::variant<FunctionInfo, std::shared_ptr<WasmTable>,
std::shared_ptr<WasmMemory>, std::shared_ptr<WasmGlobal>>;
using Exports = std::unordered_map<std::string, ExportValue>;
using ModuleImports = std::unordered_map<std::string, ExportValue>;
using Imports = std::unordered_map<std::string, ModuleImports>;
struct Function {};
struct Block {
uint8_t *block_start;
};
struct Loop {};
struct If {
uint8_t *if_start;
};
struct IfElse {
uint8_t *if_start;
uint8_t *else_start;
};
struct ControlFlow {
std::vector<valtype> &expected;
Signature &sig;
bool polymorphized;
std::variant<Function, Block, Loop, If, IfElse> construct;
};
class Module;
class WasmStack;
using ValidationHandler = void(Module &, safe_byte_iterator &, FunctionShell &,
WasmStack &, std::vector<ControlFlow> &);
// should return a shared_ptr to itself for easier lifetimes
class Module {
friend class Instance;
friend ValidationHandler validate_missing;
#define V(name, _, byte) friend ValidationHandler validate_##name;
FOREACH_INSTRUCTION(V)
FOREACH_MULTIBYTE_INSTRUCTION(V)
#undef V
std::weak_ptr<Module> self;
std::unique_ptr<uint8_t[]> bytes;
std::vector<Signature> types;
std::unordered_map<std::string, std::unordered_map<std::string, ImExDesc>>
imports;
std::vector<TableShell> tables;
MemoryShell memory;
std::vector<GlobalShell> globals;
std::unordered_map<std::string, ExportShell> exports;
uint32_t start;
uint8_t *element_start;
std::vector<ElementShell> elements;
std::vector<FunctionShell> functions;
uint32_t n_data;
std::vector<Segment> data_segments;
// locations of if else/end instructions
std::unordered_map<uint8_t *, IfJump> if_jumps;
// locations of block end instructions
std::unordered_map<uint8_t *, uint8_t *> block_ends;
void validate(safe_byte_iterator &iter, FunctionShell &fn);
void validate_const(safe_byte_iterator &iter, valtype expected);
Module(std::unique_ptr<uint8_t[]> _bytes);
void initialize(uint32_t length);
public:
static constexpr uint32_t MAX_PAGES = 65536;
static constexpr uint32_t MAX_LOCALS = 50000;
static std::shared_ptr<Module> compile(std::unique_ptr<uint8_t[]> bytes,
uint32_t length);
std::shared_ptr<Instance> instantiate(const Imports &imports = {});
void validate(uint8_t *end);
};
} // namespace mitey