-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.hpp
362 lines (317 loc) · 10.2 KB
/
runtime.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
#pragma once
#include "pager/shared.hpp"
#include "spec.hpp"
#include <csetjmp>
#include <cstdint>
#include <cstring>
#include <memory>
#include <unordered_map>
namespace mitey {
class Instance;
namespace runtime {
enum class TrapKind {
success = 0,
unreachable,
undefined_element,
uninitialized_element,
indirect_call_type_mismatch,
invalid_conversion_to_integer,
integer_overflow,
integer_divide_by_zero,
out_of_bounds_memory_access,
out_of_bounds_table_access,
call_stack_exhausted,
};
static inline const char *trap_kind_to_string(TrapKind kind) {
switch (kind) {
case TrapKind::success:
return "success";
case TrapKind::unreachable:
return "unreachable";
case TrapKind::undefined_element:
return "undefined element";
case TrapKind::uninitialized_element:
return "uninitialized element";
case TrapKind::indirect_call_type_mismatch:
return "indirect call type mismatch";
case TrapKind::invalid_conversion_to_integer:
return "invalid conversion to integer";
case TrapKind::integer_overflow:
return "integer overflow";
case TrapKind::integer_divide_by_zero:
return "integer divide by zero";
case TrapKind::out_of_bounds_memory_access:
return "out of bounds memory access";
case TrapKind::out_of_bounds_table_access:
return "out of bounds table access";
case TrapKind::call_stack_exhausted:
return "call stack exhausted";
}
}
extern uint32_t call_stack_depth;
extern std::jmp_buf *trap_buf;
[[noreturn]] static inline void __attribute__((preserve_most, noinline))
trap(TrapKind kind) {
std::longjmp(*trap_buf, static_cast<int>(kind));
}
union WasmValue;
struct WasmMemory;
struct __attribute__((packed)) FunctionType {
uint16_t params = 0;
uint16_t results = 0;
bool has_i32 : 1 = 0;
bool has_i64 : 1 = 0;
bool has_f32 : 1 = 0;
bool has_f64 : 1 = 0;
bool has_funcref : 1 = 0;
bool has_externref : 1 = 0;
uint64_t hash : 64 - 6 = 0;
bool operator==(const FunctionType &other) const {
return std::memcmp(this, &other, sizeof(FunctionType)) == 0;
}
FunctionType() = default;
FunctionType(const WasmSignature &sig)
: params(sig.params.size()), results(sig.results.size()), hash(0) {
for (valtype param : sig.params) {
switch (param) {
case valtype::i32:
has_i32 = true;
break;
case valtype::i64:
has_i64 = true;
break;
case valtype::f32:
has_f32 = true;
break;
case valtype::f64:
has_f64 = true;
break;
case valtype::funcref:
has_funcref = true;
break;
case valtype::externref:
has_externref = true;
break;
case valtype::null:
case valtype::any:
error<std::runtime_error>("invalid result type");
}
hash *= 16777619;
hash ^= static_cast<uint64_t>(param);
}
hash *= 31;
for (valtype result : sig.results) {
switch (result) {
case valtype::i32:
has_i32 = true;
break;
case valtype::i64:
has_i64 = true;
break;
case valtype::f32:
has_f32 = true;
break;
case valtype::f64:
has_f64 = true;
break;
case valtype::funcref:
has_funcref = true;
break;
case valtype::externref:
has_externref = true;
break;
case valtype::null:
case valtype::any:
error<std::runtime_error>("invalid result type");
}
hash *= 31;
hash ^= static_cast<uint64_t>(result);
}
}
template <typename Func> static FunctionType from_type() {
return FunctionType(WasmSignature::from_type<Func>());
}
};
static_assert(sizeof(FunctionType) == sizeof(uint32_t) + sizeof(uint64_t));
using Signature = void(uint8_t *memory, void **misc, WasmValue *stack,
uint64_t tmp1, uint64_t tmp2);
using TemplessSignature = void(uint8_t *, void **, WasmValue *);
struct FunctionInfo {
FunctionType type;
uint8_t *memory;
void **misc;
TemplessSignature *signature;
// this creates a circular dependency
// todo: fix this
std::shared_ptr<Instance> instance;
};
using Funcref = FunctionInfo *;
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 int() { return i32; }
operator unsigned() { return u32; }
operator long() { return i64; }
operator unsigned long() { return u64; }
operator long long() { return i64; }
operator unsigned long long() { return u64; }
operator float() { return f32; }
operator double() { return f64; }
operator Funcref() { return funcref; }
operator Externref() { return externref; }
};
struct ElementSegment {
valtype type;
uint32_t size;
std::unique_ptr<WasmValue[]> elements;
};
struct WasmTable {
uint32_t current;
uint32_t maximum;
WasmValue *elements;
valtype type;
WasmTable(valtype type, uint32_t initial, uint32_t maximum)
: current(initial), maximum(maximum),
elements(
static_cast<WasmValue *>(calloc(initial, sizeof(WasmValue)))),
type(type) {}
WasmTable() = delete;
WasmTable(const WasmTable &) = delete;
WasmTable(WasmTable &&) = delete;
WasmTable &operator=(const WasmTable &) = delete;
WasmTable &operator=(WasmTable &&) = delete;
uint32_t size() { return current; }
uint32_t max() { return maximum; }
uint32_t grow(uint32_t delta, WasmValue value);
WasmValue get(uint32_t idx);
void set(uint32_t idx, WasmValue value);
void copy_into(uint32_t dest, uint32_t src, const ElementSegment &segment,
uint32_t length);
void memcpy(WasmTable &dst_table, uint32_t dst, uint32_t src,
uint32_t length);
void memset(uint32_t dst, WasmValue value, uint32_t length);
};
struct Segment {
uint32_t memidx;
uint32_t size;
std::unique_ptr<uint8_t[]> data;
uint8_t *initializer;
static Segment empty;
};
struct WasmMemory {
static constexpr uint32_t MAX_PAGES = 65536;
static constexpr uint32_t PAGE_SIZE = 65536;
// :(
static Allocation (*default_make_memory)(size_t, AllocationKind);
static int (*default_grow_memory)(runtime::WasmMemory &, size_t);
uint32_t current;
uint32_t maximum;
// todo: make this be passed around in calling convetion
// can be updated after memory.grow or unknown calls
Allocation memory;
int (*grow_memory)(runtime::WasmMemory &, size_t);
WasmMemory(uint32_t initial, uint32_t maximum,
decltype(default_make_memory) make_memory,
decltype(default_grow_memory) grow_memory)
: current(initial), maximum(std::min(maximum, MAX_PAGES)),
memory(make_memory(PAGE_SIZE * initial, AllocationKind::Heap)),
grow_memory(grow_memory) {}
WasmMemory(uint32_t initial, uint32_t maximum)
: WasmMemory(initial, maximum, default_make_memory,
default_grow_memory) {}
WasmMemory()
: current(0), maximum(0), memory(nullptr, [](auto *) {}),
grow_memory(nullptr) {}
WasmMemory(const WasmMemory &) = delete;
WasmMemory(WasmTable &&) = delete;
WasmMemory &operator=(const WasmMemory &) = delete;
WasmMemory &operator=(WasmMemory &&) = delete;
uint32_t size() { return current; }
uint32_t max() { return maximum; }
uint32_t grow(uint32_t delta);
void copy_into(uint32_t dest, uint32_t src, const Segment &segment,
uint32_t length);
void memcpy(uint32_t dst, uint32_t src, uint32_t length);
void memset(uint32_t dst, uint8_t value, uint32_t length);
static WasmMemory empty;
};
struct WasmGlobal {
valtype type;
mut _mut;
WasmValue value;
};
using ExportValue =
std::variant<runtime::FunctionInfo, std::shared_ptr<runtime::WasmTable>,
std::shared_ptr<runtime::WasmMemory>,
std::shared_ptr<runtime::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 BrTableTarget {
int32_t lookup_offset;
int32_t stack_offset;
};
static_assert(sizeof(BrTableTarget) == sizeof(uint64_t));
struct BrInfo {
uint16_t n_targets; // for br_table
int16_t arity;
int32_t stack_offset; // offset from stack base to copy arity to
};
static_assert(sizeof(BrInfo) == sizeof(uint64_t));
struct CallIndirectInfo {
uint32_t table_idx;
FunctionType type;
};
static_assert(sizeof(CallIndirectInfo) == 2 * sizeof(uint64_t));
Signature ifXXconst;
Signature clear_locals;
Signature jump;
Signature call_extern;
Signature move_0_results;
Signature move_8_results;
Signature move_n_results;
Signature br_0_0;
Signature br_0_8;
Signature br_0_16;
Signature br_0_24;
Signature br_0_32;
Signature br_0_40;
Signature br_8_8;
Signature br_8_16;
Signature br_8_24;
Signature br_n_n;
Signature br_if_0_0;
Signature br_if_0_8;
Signature br_if_0_16;
Signature br_if_0_24;
Signature br_if_0_32;
Signature br_if_0_40;
Signature br_if_8_8;
Signature br_if_8_16;
Signature br_if_8_24;
Signature br_if_n_n;
Signature br_table_0;
Signature br_table_8;
Signature br_table_n;
Signature dummy;
#define HANDLER(name, str, byte) Signature name;
FOREACH_INSTRUCTION(HANDLER)
FOREACH_MULTIBYTE_INSTRUCTION(HANDLER)
#undef HANDLER
}; // namespace runtime
}; // namespace mitey