-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule.cpp
More file actions
484 lines (431 loc) · 17.9 KB
/
Module.cpp
File metadata and controls
484 lines (431 loc) · 17.9 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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
#include <string.h>
#include <atomic>
#include <memory>
#include <utility>
#include "RuntimePrivate.h"
#include "WAVM/IR/IR.h"
#include "WAVM/IR/Module.h"
#include "WAVM/IR/Types.h"
#include "WAVM/IR/Value.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Hash.h"
#include "WAVM/Inline/HashMap.h"
#include "WAVM/Inline/Lock.h"
#include "WAVM/Inline/Serialization.h"
#include "WAVM/LLVMJIT/LLVMJIT.h"
#include "WAVM/Platform/Intrinsic.h"
#include "WAVM/Platform/Mutex.h"
#include "WAVM/Runtime/Runtime.h"
using namespace WAVM;
using namespace WAVM::IR;
using namespace WAVM::Runtime;
static Value evaluateInitializer(const std::vector<Global*>& moduleGlobals,
InitializerExpression expression)
{
switch(expression.type)
{
case InitializerExpression::Type::i32_const: return expression.i32;
case InitializerExpression::Type::i64_const: return expression.i64;
case InitializerExpression::Type::f32_const: return expression.f32;
case InitializerExpression::Type::f64_const: return expression.f64;
case InitializerExpression::Type::v128_const: return expression.v128;
case InitializerExpression::Type::get_global:
{
// Find the import this refers to.
errorUnless(expression.globalIndex < moduleGlobals.size());
Global* global = moduleGlobals[expression.globalIndex];
errorUnless(global);
errorUnless(!global->type.isMutable);
return IR::Value(global->type.valueType, global->initialValue);
}
case InitializerExpression::Type::ref_null: return nullptr;
default: Errors::unreachable();
};
}
ModuleRef Runtime::compileModule(const IR::Module& irModule)
{
std::vector<U8> objectCode = LLVMJIT::compileModule(irModule);
return std::make_shared<Module>(IR::Module(irModule), std::move(objectCode));
}
std::vector<U8> Runtime::getObjectCode(ModuleConstRefParam module) { return module->objectCode; }
ModuleRef Runtime::loadPrecompiledModule(const IR::Module& irModule,
const std::vector<U8>& objectCode)
{
return std::make_shared<Module>(IR::Module(irModule), std::vector<U8>(objectCode));
}
ModuleInstance::~ModuleInstance()
{
if(id != UINTPTR_MAX)
{
wavmAssertMutexIsLockedByCurrentThread(compartment->mutex);
compartment->moduleInstances.removeOrFail(id);
}
}
ModuleInstance* Runtime::instantiateModule(Compartment* compartment,
ModuleConstRefParam module,
ImportBindings&& imports,
std::string&& moduleDebugName)
{
dummyReferenceAtomics();
dummyReferenceWAVMIntrinsics();
Uptr id = UINTPTR_MAX;
{
Lock<Platform::Mutex> compartmentLock(compartment->mutex);
id = compartment->moduleInstances.add(UINTPTR_MAX, nullptr);
}
if(id == UINTPTR_MAX) { return nullptr; }
// Check the type of the ModuleInstance's imports.
std::vector<Function*> functions = std::move(imports.functions);
errorUnless(functions.size() == module->ir.functions.imports.size());
for(Uptr importIndex = 0; importIndex < module->ir.functions.imports.size(); ++importIndex)
{
Object* importObject = asObject(functions[importIndex]);
errorUnless(
isA(importObject, module->ir.types[module->ir.functions.getType(importIndex).index]));
errorUnless(isInCompartment(importObject, compartment));
}
std::vector<Table*> tables = std::move(imports.tables);
errorUnless(tables.size() == module->ir.tables.imports.size());
for(Uptr importIndex = 0; importIndex < module->ir.tables.imports.size(); ++importIndex)
{
Object* importObject = asObject(tables[importIndex]);
errorUnless(isA(importObject, module->ir.tables.getType(importIndex)));
errorUnless(isInCompartment(importObject, compartment));
}
std::vector<Memory*> memories = std::move(imports.memories);
errorUnless(memories.size() == module->ir.memories.imports.size());
for(Uptr importIndex = 0; importIndex < module->ir.memories.imports.size(); ++importIndex)
{
Object* importObject = asObject(memories[importIndex]);
errorUnless(isA(importObject, module->ir.memories.getType(importIndex)));
errorUnless(isInCompartment(importObject, compartment));
}
std::vector<Global*> globals = std::move(imports.globals);
errorUnless(globals.size() == module->ir.globals.imports.size());
for(Uptr importIndex = 0; importIndex < module->ir.globals.imports.size(); ++importIndex)
{
Object* importObject = asObject(globals[importIndex]);
errorUnless(isA(importObject, module->ir.globals.getType(importIndex)));
errorUnless(isInCompartment(importObject, compartment));
}
std::vector<ExceptionType*> exceptionTypes = std::move(imports.exceptionTypes);
errorUnless(exceptionTypes.size() == module->ir.exceptionTypes.imports.size());
for(Uptr importIndex = 0; importIndex < module->ir.exceptionTypes.imports.size(); ++importIndex)
{
Object* importObject = asObject(exceptionTypes[importIndex]);
errorUnless(isA(importObject, module->ir.exceptionTypes.getType(importIndex)));
errorUnless(isInCompartment(importObject, compartment));
}
// Deserialize the disassembly names.
DisassemblyNames disassemblyNames;
getDisassemblyNames(module->ir, disassemblyNames);
// Instantiate the module's memory and table definitions.
for(Uptr tableDefIndex = 0; tableDefIndex < module->ir.tables.defs.size(); ++tableDefIndex)
{
std::string debugName
= disassemblyNames.tables[module->ir.tables.imports.size() + tableDefIndex];
auto table = createTable(
compartment, module->ir.tables.defs[tableDefIndex].type, std::move(debugName));
if(!table) { throwException(Exception::outOfMemoryType); }
tables.push_back(table);
}
for(Uptr memoryDefIndex = 0; memoryDefIndex < module->ir.memories.defs.size(); ++memoryDefIndex)
{
std::string debugName
= disassemblyNames.memories[module->ir.memories.imports.size() + memoryDefIndex];
auto memory = createMemory(
compartment, module->ir.memories.defs[memoryDefIndex].type, std::move(debugName));
if(!memory) { throwException(Exception::outOfMemoryType); }
memories.push_back(memory);
}
// Instantiate the module's global definitions.
for(const GlobalDef& globalDef : module->ir.globals.defs)
{
const Value initialValue = evaluateInitializer(globals, globalDef.initializer);
errorUnless(isSubtype(initialValue.type, globalDef.type.valueType));
globals.push_back(createGlobal(compartment, globalDef.type, initialValue));
}
// Instantiate the module's exception types.
for(Uptr exceptionTypeDefIndex = 0;
exceptionTypeDefIndex < module->ir.exceptionTypes.defs.size();
++exceptionTypeDefIndex)
{
const ExceptionTypeDef& exceptionTypeDef
= module->ir.exceptionTypes.defs[exceptionTypeDefIndex];
std::string debugName
= disassemblyNames
.exceptionTypes[module->ir.exceptionTypes.imports.size() + exceptionTypeDefIndex];
exceptionTypes.push_back(
createExceptionType(compartment, exceptionTypeDef.type, std::move(debugName)));
}
// Set up the values to bind to the symbols in the LLVMJIT object code.
HashMap<std::string, LLVMJIT::FunctionBinding> wavmIntrinsicsExportMap;
for(const HashMapPair<std::string, Intrinsics::Function*>& intrinsicFunctionPair :
Intrinsics::getUninstantiatedFunctions(INTRINSIC_MODULE_REF(wavmIntrinsics)))
{
LLVMJIT::FunctionBinding functionBinding{
intrinsicFunctionPair.value->getCallingConvention(),
intrinsicFunctionPair.value->getNativeFunction()};
wavmIntrinsicsExportMap.add(intrinsicFunctionPair.key, functionBinding);
}
std::vector<LLVMJIT::FunctionBinding> jitFunctionImports;
for(Uptr importIndex = 0; importIndex < module->ir.functions.imports.size(); ++importIndex)
{
jitFunctionImports.push_back(
{CallingConvention::wasm, const_cast<U8*>(functions[importIndex]->code)});
}
std::vector<LLVMJIT::TableBinding> jitTables;
for(Table* table : tables) { jitTables.push_back({table->id}); }
std::vector<LLVMJIT::MemoryBinding> jitMemories;
for(Memory* memory : memories) { jitMemories.push_back({memory->id}); }
std::vector<LLVMJIT::GlobalBinding> jitGlobals;
for(Global* global : globals)
{
LLVMJIT::GlobalBinding globalSpec;
globalSpec.type = global->type;
if(global->type.isMutable) { globalSpec.mutableGlobalIndex = global->mutableGlobalIndex; }
else
{
globalSpec.immutableValuePointer = &global->initialValue;
}
jitGlobals.push_back(globalSpec);
}
std::vector<LLVMJIT::ExceptionTypeBinding> jitExceptionTypes;
for(ExceptionType* exceptionType : exceptionTypes)
{ jitExceptionTypes.push_back({exceptionType->id}); }
// Create a FunctionMutableData for each function definition.
std::vector<FunctionMutableData*> functionDefMutableDatas;
for(Uptr functionDefIndex = 0; functionDefIndex < module->ir.functions.defs.size();
++functionDefIndex)
{
std::string debugName
= disassemblyNames.functions[module->ir.functions.imports.size() + functionDefIndex]
.name;
if(!debugName.size())
{ debugName = "<function #" + std::to_string(functionDefIndex) + ">"; }
debugName = "wasm!" + moduleDebugName + '!' + debugName;
functionDefMutableDatas.push_back(new FunctionMutableData(std::move(debugName)));
}
// Load the compiled module's object code with this module instance's imports.
std::vector<FunctionType> jitTypes = module->ir.types;
std::vector<Runtime::Function*> jitFunctionDefs;
jitFunctionDefs.resize(module->ir.functions.defs.size(), nullptr);
std::shared_ptr<LLVMJIT::Module> jitModule
= LLVMJIT::loadModule(module->objectCode,
std::move(wavmIntrinsicsExportMap),
std::move(jitTypes),
std::move(jitFunctionImports),
std::move(jitTables),
std::move(jitMemories),
std::move(jitGlobals),
std::move(jitExceptionTypes),
{id},
reinterpret_cast<Uptr>(getOutOfBoundsElement()),
functionDefMutableDatas);
// LLVMJIT::loadModule filled in the functionDefMutableDatas' function pointers with the
// compiled functions. Add those functions to the module.
for(FunctionMutableData* functionMutableData : functionDefMutableDatas)
{ functions.push_back(functionMutableData->function); }
// Set up the instance's exports.
HashMap<std::string, Object*> exportMap;
for(const Export& exportIt : module->ir.exports)
{
Object* exportedObject = nullptr;
switch(exportIt.kind)
{
case IR::ExternKind::function: exportedObject = asObject(functions[exportIt.index]); break;
case IR::ExternKind::table: exportedObject = tables[exportIt.index]; break;
case IR::ExternKind::memory: exportedObject = memories[exportIt.index]; break;
case IR::ExternKind::global: exportedObject = globals[exportIt.index]; break;
case IR::ExternKind::exceptionType: exportedObject = exceptionTypes[exportIt.index]; break;
default: Errors::unreachable();
}
exportMap.addOrFail(exportIt.name, exportedObject);
}
// Copy the module's passive data and table segments into the ModuleInstance for later use.
PassiveDataSegmentMap passiveDataSegments;
PassiveElemSegmentMap passiveElemSegments;
for(Uptr segmentIndex = 0; segmentIndex < module->ir.dataSegments.size(); ++segmentIndex)
{
const DataSegment& dataSegment = module->ir.dataSegments[segmentIndex];
if(!dataSegment.isActive)
{
passiveDataSegments.add(segmentIndex,
std::make_shared<std::vector<U8>>(dataSegment.data));
}
}
for(Uptr segmentIndex = 0; segmentIndex < module->ir.elemSegments.size(); ++segmentIndex)
{
const ElemSegment& elemSegment = module->ir.elemSegments[segmentIndex];
if(!elemSegment.isActive)
{
auto passiveElemSegmentObjects = std::make_shared<std::vector<Object*>>();
for(Uptr functionIndex : elemSegment.indices)
{ passiveElemSegmentObjects->push_back(asObject(functions[functionIndex])); }
passiveElemSegments.add(segmentIndex, passiveElemSegmentObjects);
}
}
// Look up the module's start function.
Function* startFunction = nullptr;
if(module->ir.startFunctionIndex != UINTPTR_MAX)
{
startFunction = functions[module->ir.startFunctionIndex];
wavmAssert(FunctionType(startFunction->encodedType) == FunctionType());
}
// Create the ModuleInstance and add it to the compartment's modules list.
ModuleInstance* moduleInstance = new ModuleInstance(compartment,
id,
std::move(exportMap),
std::move(functions),
std::move(tables),
std::move(memories),
std::move(globals),
std::move(exceptionTypes),
startFunction,
std::move(passiveDataSegments),
std::move(passiveElemSegments),
std::move(jitModule),
std::move(moduleDebugName));
{
Lock<Platform::Mutex> compartmentLock(compartment->mutex);
compartment->moduleInstances[id] = moduleInstance;
}
// Copy the module's data segments into their designated memory instances.
for(const DataSegment& dataSegment : module->ir.dataSegments)
{
if(dataSegment.isActive)
{
Memory* memory = moduleInstance->memories[dataSegment.memoryIndex];
const Value baseOffsetValue
= evaluateInitializer(moduleInstance->globals, dataSegment.baseOffset);
errorUnless(baseOffsetValue.type == ValueType::i32);
const U32 baseOffset = baseOffsetValue.i32;
if(dataSegment.data.size())
{
Platform::bytewiseMemCopy(memory->baseAddress + baseOffset,
dataSegment.data.data(),
dataSegment.data.size());
}
else
{
// WebAssembly still expects out-of-bounds errors if the segment base offset is
// out-of-bounds, even if the segment is empty.
if(baseOffset > memory->numPages * IR::numBytesPerPage)
{
throwException(Runtime::Exception::outOfBoundsMemoryAccessType,
{memory, U64(baseOffset)});
}
}
}
}
// Copy the module's elem segments into their designated table instances.
for(const ElemSegment& elemSegment : module->ir.elemSegments)
{
if(elemSegment.isActive)
{
Table* table = moduleInstance->tables[elemSegment.tableIndex];
const Value baseOffsetValue
= evaluateInitializer(moduleInstance->globals, elemSegment.baseOffset);
errorUnless(baseOffsetValue.type == ValueType::i32);
const U32 baseOffset = baseOffsetValue.i32;
if(elemSegment.indices.size())
{
for(Uptr index = 0; index < elemSegment.indices.size(); ++index)
{
const Uptr functionIndex = elemSegment.indices[index];
wavmAssert(functionIndex < moduleInstance->functions.size());
Function* function = moduleInstance->functions[functionIndex];
setTableElement(table, baseOffset + index, asObject(function));
}
}
else
{
// WebAssembly still expects out-of-bounds errors if the segment base offset is
// out-of-bounds, even if the segment is empty.
if(baseOffset > getTableNumElements(table))
{
throwException(Runtime::Exception::outOfBoundsTableAccessType,
{table, U64(baseOffset)});
}
}
}
}
return moduleInstance;
}
ModuleInstance* Runtime::cloneModuleInstance(ModuleInstance* moduleInstance,
Compartment* newCompartment)
{
// Remap the module's references to the cloned compartment.
HashMap<std::string, Object*> newExportMap;
for(const auto& pair : moduleInstance->exportMap)
{ newExportMap.add(pair.key, remapToClonedCompartment(pair.value, newCompartment)); }
std::vector<Function*> newFunctions = moduleInstance->functions;
std::vector<Table*> newTables;
for(Table* table : moduleInstance->tables)
{ newTables.push_back(remapToClonedCompartment(table, newCompartment)); }
std::vector<Memory*> newMemories;
for(Memory* memory : moduleInstance->memories)
{ newMemories.push_back(remapToClonedCompartment(memory, newCompartment)); }
std::vector<Global*> newGlobals;
for(Global* global : moduleInstance->globals)
{ newGlobals.push_back(remapToClonedCompartment(global, newCompartment)); }
std::vector<ExceptionType*> newExceptionTypes;
for(ExceptionType* exceptionType : moduleInstance->exceptionTypes)
{ newExceptionTypes.push_back(remapToClonedCompartment(exceptionType, newCompartment)); }
Function* newStartFunction
= remapToClonedCompartment(moduleInstance->startFunction, newCompartment);
PassiveDataSegmentMap newPassiveDataSegments;
{
Lock<Platform::Mutex> passiveDataSegmentsLock(moduleInstance->passiveDataSegmentsMutex);
newPassiveDataSegments = moduleInstance->passiveDataSegments;
}
PassiveElemSegmentMap newPassiveElemSegments;
{
Lock<Platform::Mutex> passiveDataSegmentsLock(moduleInstance->passiveDataSegmentsMutex);
newPassiveDataSegments = moduleInstance->passiveDataSegments;
}
for(const auto& pair : newPassiveElemSegments)
{
for(Uptr index = 0; index < pair.value->size(); ++index)
{ (*pair.value)[index] = remapToClonedCompartment((*pair.value)[index], newCompartment); }
}
// Create the new ModuleInstance in the cloned compartment, but with the same ID as the old one.
std::shared_ptr<LLVMJIT::Module> jitModuleCopy = moduleInstance->jitModule;
ModuleInstance* newModuleInstance = new ModuleInstance(newCompartment,
moduleInstance->id,
std::move(newExportMap),
std::move(newFunctions),
std::move(newTables),
std::move(newMemories),
std::move(newGlobals),
std::move(newExceptionTypes),
std::move(newStartFunction),
std::move(newPassiveDataSegments),
std::move(newPassiveElemSegments),
std::move(jitModuleCopy),
std::string(moduleInstance->debugName));
{
Lock<Platform::Mutex> compartmentLock(newCompartment->mutex);
newCompartment->moduleInstances.insertOrFail(moduleInstance->id, newModuleInstance);
}
return newModuleInstance;
}
Function* Runtime::getStartFunction(ModuleInstance* moduleInstance)
{
return moduleInstance->startFunction;
}
Memory* Runtime::getDefaultMemory(ModuleInstance* moduleInstance)
{
return moduleInstance->memories.size() ? moduleInstance->memories[0] : nullptr;
}
Table* Runtime::getDefaultTable(ModuleInstance* moduleInstance)
{
return moduleInstance->tables.size() ? moduleInstance->tables[0] : nullptr;
}
Object* Runtime::getInstanceExport(ModuleInstance* moduleInstance, const std::string& name)
{
wavmAssert(moduleInstance);
Object* const* exportedObjectPtr = moduleInstance->exportMap.get(name);
return exportedObjectPtr ? *exportedObjectPtr : nullptr;
}