-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathInstantiator.v3
More file actions
419 lines (406 loc) · 15.1 KB
/
Instantiator.v3
File metadata and controls
419 lines (406 loc) · 15.1 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
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
// Copyright 2020 Ben L. Titzer. All rights reserved.
// See LICENSE for details of Apache 2.0 license.
// Extension point for import processing. An {ImportProcessor} lookups or materializes
// an {Exportable} for a given name and import kind. It can use the {binder} as a
// callback to access parts of the module that need polymorphic substitution.
class ImportProcessor(name: string) {
def preprocess(error: ErrorGen, module: Module, imports: Array<Exportable>) { }
def processFunction(error: ErrorGen, name: string, decl: FuncDecl) -> Exportable { return null; }
def processTable(error: ErrorGen, name: string, decl: TableDecl) -> Exportable { return null; }
def processMemory(error: ErrorGen, name: string, decl: MemoryDecl) -> Exportable { return null; }
def processGlobal(error: ErrorGen, name: string, decl: GlobalDecl) -> Exportable { return null; }
def processTag(error: ErrorGen, name: string, decl: TagDecl) -> Exportable { return null; }
def postprocess(error: ErrorGen, instance: Instance) { }
}
// Creates {Instance} objects, given a module and a list of imports.
class Instantiator(extensions: Extension.set, module: Module, var imports: Array<Exportable>, error: ErrorGen) { // TODO: s/error/err
def instance = Instance.new(module, imports);
def processors = Vector<ImportProcessor>.new();
var tiering: ExecutionStrategy;
var binder: (Decl, Exportable) -> Exportable; // last binding step
var trap_reason: TrapReason;
var import_pos: u31;
var import_info: ImportInfo;
private var partially_instantiated = false;
private var fully_instantiated = false;
def run() -> Instance {
return runPartial(null);
}
// Initialize the instance with the given processors to populate imports.
// `deferred_imports` contains the import indices that should be skipped during first pass
// initialization. It represents the imports from preloaded wasm modules that are dynamically linked.
//
// For single module instantiation, `deferred_imports` is null or empty.
// For dynamic linking with multiple wasm modules, this method is called twice with the same set of `deferred_imports`.
// The instantiator keeps track of partial/completed instantiation state.
def runPartial(deferred_imports: Array<int>) -> Instance {
if (fully_instantiated) return instance;
//if (!partially_instantiated) {
if (tiering != null) tiering.onInstantiateStart(this);
if (module.imports.length > 0) {
if (imports == null) imports = Array.new(module.imports.length);
else if (imports.length < module.imports.length) {
return fail(Strings.format2("expected %d imports, got %d", module.imports.length, imports.length));
}
}
for (i < processors.length) {
processors[i].preprocess(error, module, imports);
}
var needSigIds = false;
for (j < module.decls.length) {
if (error.error()) return null;
match (module.decls[j]) {
Sig(index) => {
instance.heaptypes[index] = module.heaptypes[index];
}
Struct(index) => {
instance.heaptypes[index] = module.heaptypes[index];
}
Array(index) => {
instance.heaptypes[index] = module.heaptypes[index];
}
Func(index, sig_index) => {
var decl = module.functions[index];
if ((import_info = decl.imp) != null) {
if (partially_instantiated != checkDeferredImport(decl.imp.import_index, deferred_imports)) continue;
var r = imports[decl.imp.import_index];
instance.functions[index] = importFunction(decl, r);
} else if (!partially_instantiated) {
var f = WasmFunction.new(instance, mapFuncDecl(index));
instance.functions[index] = f;
}
}
Table(index) => {
var decl = module.tables[index];
if ((import_info = decl.imp) != null) {
if (partially_instantiated != checkDeferredImport(decl.imp.import_index, deferred_imports)) continue;
var r = imports[decl.imp.import_index];
var t = importTable(decl, r);
instance.tables[index] = t;
if (t != null && t.funcs != null) needSigIds = true;
} else if (!partially_instantiated) {
var t = Table.new(decl.elemtype, decl);
if (t.oom) return fail("out of memory allocating table");
instance.tables[index] = t;
if (t.funcs != null) needSigIds = true;
}
}
Memory(index) => {
var decl = module.memories[index];
if ((import_info = decl.imp) != null) {
if (partially_instantiated != checkDeferredImport(decl.imp.import_index, deferred_imports)) continue;
var r = imports[decl.imp.import_index];
instance.memories[index] = importMemory(decl, r);
} else if (!partially_instantiated) {
var m = Target.newMemory(decl);
if (m.oom) return fail("out of memory allocating memory");
instance.memories[index] = m;
}
}
Global(index) => {
var decl = module.globals[index];
if ((import_info = decl.imp) != null) {
if (partially_instantiated != checkDeferredImport(decl.imp.import_index, deferred_imports)) continue;
var r = imports[decl.imp.import_index];
instance.globals[index] = importGlobal(decl, r);
} else if (!partially_instantiated) {
var g = Global.new(decl.valtype, decl);
instance.globals[index] = g;
g.set(instance.evalInitExpr(decl.init));
}
}
Tag(index) => {
var decl = module.tags[index];
if ((import_info = decl.imp) != null) {
if (partially_instantiated != checkDeferredImport(decl.imp.import_index, deferred_imports)) continue;
var r = imports[decl.imp.import_index];
instance.tags[index] = importTag(decl, r);
} else if (!partially_instantiated) {
instance.tags[index] = Tag.new(getSigDecl(decl.sig_index), decl);
}
}
Continuation(index) => {
// TODO-SS: implement
}
}
}
if (error.error()) return null;
import_info = null;
if (needSigIds) {
for (i < instance.heaptypes.length) {
match (instance.heaptypes[i]) {
x: SigDecl => instance.sig_ids[i] = Canon.sigId(x);
_ => instance.sig_ids[i] = -1;
}
}
}
// Initialize tables that have default element values.
for (i < instance.tables.length) {
var t = instance.tables[i];
var decl = instance.module.tables[i];
var is_deferred = if(t == null, true,
decl.imp != null && checkDeferredImport(decl.imp.import_index, deferred_imports));
// deferred imports are only loaded during finalization, regular imports are only
// loaded on first pass.
if (partially_instantiated != is_deferred || !t.decl.has_default_elem) continue;
var val = instance.evalInitExpr(t.decl.default_elem);
t.fill(0, val, u32.view(t.elems.length));
}
// Load element segments.
for (i < module.elems.length) {
var e = module.elems[i];
match (e.mode) {
Passive => ;
Active(index, offset) => {
var t = instance.tables[index];
var decl = instance.module.tables[index];
var is_deferred = if(t == null, true,
decl.imp != null && checkDeferredImport(decl.imp.import_index, deferred_imports));
// deferred imports are only loaded during finalization, regular imports are only
// loaded on first pass.
if (partially_instantiated != is_deferred) continue;
var evals = instance.getElems(i);
loadElems(index, offset, evals);
instance.dropped_elems[i] = true;
if (error.error()) return null;
}
Declarative => {
instance.dropped_elems[i] = true;
}
}
}
// Load data segments.
for (i < module.data.length) {
var d = module.data[i];
match (d.mode) {
Passive => ;
Active(index, offset) => {
var mem = instance.memories[index];
var decl = instance.module.memories[index];
var is_deferred = if (mem == null, true,
decl.imp != null && checkDeferredImport(decl.imp.import_index, deferred_imports));
// deferred imports are only loaded during finalization, regular imports are only
// loaded on first pass.
if (partially_instantiated != is_deferred) continue;
loadData(index, offset, d);
instance.dropped_data[i] = true;
if (error.error()) return null;
}
Declarative => {
instance.dropped_data[i] = true;
}
}
}
// Organize exports
var exports = instance.exports;
for (i < module.exports.length) {
exports[i] = getDecl(module.exports[i].1);
}
var has_deferred_imports = deferred_imports != null && deferred_imports.length > 0;
if (!partially_instantiated) {
partially_instantiated = true;
fully_instantiated = !has_deferred_imports;
} else {
fully_instantiated = true;
}
if (fully_instantiated) {
for (i < processors.length) {
processors[i].postprocess(error, instance);
}
if (tiering != null) tiering.onInstantiateFinish(this, error);
}
return instance;
}
def importFunction(decl: FuncDecl, r: Exportable) -> Function {
if (r == null) r = processImport(decl, ImportProcessor.processFunction(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
func: Function => {
var expected_sig = getSigDecl(decl.sig_index);
if (func.sig != expected_sig) {
var buf = StringBuilder.new().puts("expected function with signature ");
if (expected_sig == null) buf.puts("(null)");
else expected_sig.render(buf);
buf.puts(", got ");
func.sig.render(buf);
if (func.sig.isAssignableSig(expected_sig)) {
// structurally equivalent, but not allowed in Wasm's canonicalization--add context
buf.put2(", structurally equivalent (canon #%d, #%d)", expected_sig.canon_id, func.sig.canon_id);
}
fail(buf.toString());
}
return func;
}
null => fail("function import not found");
_ => fail("expected function import");
}
return null;
}
def importTable(decl: TableDecl, r: Exportable) -> Table {
if (r == null) r = processImport(decl, ImportProcessor.processTable(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
table: Table => {
var f = table.decl.size;
if (!checkSizeConstraint(f.is64, u64.!(table.elems.length), f.maximum, decl.size)) {
fail("table limits mismatch");
}
var expected_type = decl.elemtype;
var got_type = table.decl.elemtype;
if (got_type != expected_type) { // TODO: proper equality check
fail("table element type mismatch");
}
return table;
}
}
fail("expected table import");
return null;
}
def importMemory(decl: MemoryDecl, r: Exportable) -> Memory {
if (r == null) r = processImport(decl, ImportProcessor.processMemory(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
memory: Memory => {
var f = memory.decl.size;
if (!checkSizeConstraint(f.is64, memory.num_pages, f.maximum, decl.size)) {
fail("memory limits mismatch");
}
if (memory.decl.shared != decl.shared) {
fail("memory sharing mismatch");
}
return memory;
}
}
fail("expected memory import");
return null;
}
def importTag(decl: TagDecl, r: Exportable) -> Tag {
if (r == null) r = processImport(decl, ImportProcessor.processTag(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
tag: Tag => {
var expected_sig = getSigDecl(decl.sig_index);
if (tag.sig != expected_sig) {
var buf = StringBuilder.new().puts("expected tag with signature ");
if (expected_sig == null) buf.puts("(null)");
else expected_sig.render(buf);
buf.puts(", got ");
tag.sig.render(buf);
if (tag.sig.isAssignableSig(expected_sig)) {
// structurally equivalent, but not allowed in Wasm's canonicalization--add context
buf.put2(", structurally equivalent (canon #%d, #%d)", expected_sig.canon_id, tag.sig.canon_id);
}
fail(buf.toString());
}
return tag;
}
null => fail("tag import not found");
_ => fail("expected tag import");
}
return null;
}
def importGlobal(decl: GlobalDecl, r: Exportable) -> Global {
if (r == null) r = processImport(decl, ImportProcessor.processGlobal(_, error, _, decl));
if (binder != null) r = binder(decl, r);
match (r) {
global: Global => {
if (decl.mutable != global.decl.mutable) {
fail("global immutability mismatch");
}
var expected_type = decl.valtype;
var got_type = global.valtype;
if (decl.mutable) {
if (got_type != expected_type) fail("global type mismatch");
} else {
if (!ValueTypes.isAssignable(got_type, expected_type)) fail("global type mismatch");
}
return global;
}
}
fail("expected global import");
return null;
}
def processImport(decl: Decl, f: (ImportProcessor, string) -> Exportable) -> Exportable {
var i = decl.imp;
import_pos = i.import_index;
var modname = i.module_name, fieldname = i.field_name;
for (i < processors.length) {
var p = processors[i];
if (Strings.equal(modname, p.name)) {
var r = f(p, fieldname);
if (r != null) {
imports[decl.imp.import_index] = r;
return r;
}
}
}
return null;
}
def getDecl(d: Decl) -> Exportable {
match (d) {
x: FuncDecl => return instance.functions[x.func_index];
x: TableDecl => return instance.tables[x.table_index];
x: MemoryDecl => return instance.memories[x.memory_index];
x: GlobalDecl => return instance.globals[x.global_index];
x: TagDecl => return instance.tags[x.tag_index];
}
return null;
}
def getSigDecl(sig_index: int) -> SigDecl {
if (u32.view(sig_index) >= instance.heaptypes.length) return null;
return SigDecl.!(instance.heaptypes[sig_index]);
}
// Substitution utilities.
def mapFuncDecl(func_index: int) -> FuncDecl {
var orig = module.functions[func_index];
return orig;
}
private def checkDeferredImport(imp_index: int, deferred_imports: Array<int>) -> bool {
if (deferred_imports == null || deferred_imports.length == 0) return false;
for (i < deferred_imports.length) {
if (deferred_imports[i] == imp_index) return true;
}
return false;
}
private def fail(msg: string) -> Instance {
if (import_info != null) {
var buf = StringBuilder.new()
.put3("import #%d(\"%s\".\"%s\"): ", import_pos, import_info.module_name, import_info.field_name)
.puts(msg);
msg = buf.toString();
}
error.abs(import_pos).set(msg);
return null;
}
private def checkSizeConstraint(is64: bool, size: u64, fmax: Max, t: SizeConstraint) -> bool {
if (size < t.initial) return false;
if (is64 != t.is64) return false;
match (t.maximum) {
None => return true;
Set(tmax_val) => match (fmax) {
None => return false;
Set(fmax_val) => return fmax_val <= tmax_val;
}
}
}
private def loadElems(index: int, offset: InitExpr, evals: Array<Value>) {
var dst_val = instance.evalInitExpr(offset);
var dst_offset = if(Value.I64.?(dst_val), Value.I64.!(dst_val).val, Value.I32.!(dst_val).val);
var table = instance.tables[index];
var r = table.copyE(instance, dst_offset, evals, 0, u32.view(evals.length));
if (r != TrapReason.NONE) {
fail("out of bounds in initialization");
trap_reason = r;
}
}
private def loadData(index: int, offset: InitExpr, ddecl: DataDecl) {
var dst_val = instance.evalInitExpr(offset);
var dst_offset = if(Value.I64.?(dst_val), Value.I64.!(dst_val).val, Value.I32.!(dst_val).val);
var memory = instance.memories[index];
var r = memory.copyIn(dst_offset, if(ddecl != null, ddecl.data), 0, u32.view(ddecl.data.length));
if (r != TrapReason.NONE) {
fail("out of bounds in initialization");
trap_reason = r;
}
}
}