forked from WAVM/WAVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntime.cpp
More file actions
320 lines (271 loc) · 11.1 KB
/
Copy pathRuntime.cpp
File metadata and controls
320 lines (271 loc) · 11.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
#include "Inline/BasicTypes.h"
#include "Logging/Logging.h"
#include "Runtime.h"
#include "RuntimePrivate.h"
namespace Runtime
{
bool isA(Object* object,const ObjectType& type)
{
if(Runtime::ObjectKind(type.kind) != object->kind) { return false; }
switch(type.kind)
{
case IR::ObjectKind::function: return asFunctionType(type) == asFunction(object)->type;
case IR::ObjectKind::global: return asGlobalType(type) == asGlobal(object)->type;
case IR::ObjectKind::table: return isSubset(asTableType(type),asTable(object)->type);
case IR::ObjectKind::memory: return isSubset(asMemoryType(type),asMemory(object)->type);
case IR::ObjectKind::exceptionType: return asExceptionTypeType(type) == asExceptionType(object)->parameters;
default: Errors::unreachable();
}
}
IR::ObjectType getObjectType(Object* object)
{
switch(object->kind)
{
case Runtime::ObjectKind::function: return asFunction(object)->type;
case Runtime::ObjectKind::global: return asGlobal(object)->type;
case Runtime::ObjectKind::table: return asTable(object)->type;
case Runtime::ObjectKind::memory: return asMemory(object)->type;
case Runtime::ObjectKind::exceptionType: return asExceptionType(object)->parameters;
default: Errors::unreachable();
};
}
UntaggedValue* invokeFunctionUnchecked(Context* context,FunctionInstance* function,const UntaggedValue* arguments)
{
const FunctionType* functionType = function->type;
// Get the invoke thunk for this function type.
auto invokeFunctionPointer = LLVMJIT::getInvokeThunk(functionType,function->callingConvention);
// Copy the arguments into the thunk arguments buffer in ContextRuntimeData.
ContextRuntimeData* contextRuntimeData = &context->compartment->runtimeData->contexts[context->id];
U8* argData = contextRuntimeData->thunkArgAndReturnData;
Uptr argDataOffset = 0;
for(Uptr argumentIndex = 0;argumentIndex < functionType->parameters.size();++argumentIndex)
{
const ValueType type = functionType->parameters[argumentIndex];
const UntaggedValue& argument = arguments[argumentIndex];
if(type == ValueType::v128)
{
// Use 16-byte alignment for V128 arguments.
argDataOffset = (argDataOffset + 15) & ~15;
}
if(argDataOffset >= maxThunkArgAndReturnBytes)
{
// Throw an exception if the invoke uses too much memory for arguments.
throwException(Exception::outOfMemoryType);
}
memcpy(argData + argDataOffset,argument.bytes,getTypeByteWidth(type));
argDataOffset += type == ValueType::v128 ? 16 : 8;
}
// Call the invoke thunk.
contextRuntimeData = (ContextRuntimeData*)(*invokeFunctionPointer)(
function->nativeFunction,
contextRuntimeData);
// Return a pointer to the return value that was written to the ContextRuntimeData.
return (UntaggedValue*)contextRuntimeData->thunkArgAndReturnData;
}
Result invokeFunctionChecked(Context* context,FunctionInstance* function,const std::vector<Value>& arguments)
{
const FunctionType* functionType = function->type;
// Check that the parameter types match the function, and copy them into a memory block that stores each as a 64-bit value.
if(arguments.size() != functionType->parameters.size())
{ throwException(Exception::invokeSignatureMismatchType); }
// Convert the arguments from a vector of TaggedValues to a stack-allocated block of UntaggedValues.
UntaggedValue* untaggedArguments = (UntaggedValue*)alloca(arguments.size() * sizeof(UntaggedValue));
for(Uptr argumentIndex = 0;argumentIndex < arguments.size();++argumentIndex)
{
if(functionType->parameters[argumentIndex] != arguments[argumentIndex].type)
{
throwException(Exception::invokeSignatureMismatchType);
}
untaggedArguments[argumentIndex] = arguments[argumentIndex];
}
return Result(functionType->ret,*invokeFunctionUnchecked(context,function,untaggedArguments));
}
const FunctionType* getFunctionType(FunctionInstance* function)
{
return function->type;
}
GlobalInstance* createGlobal(Compartment* compartment,GlobalType type,Value initialValue)
{
assert(initialValue.type == type.valueType);
// Allow immutable globals to be created without a compartment.
errorUnless(!type.isMutable || compartment);
Platform::Lock compartmentLock(compartment->mutex);
GlobalInstance* globalInstance;
if(!type.isMutable) { globalInstance = new GlobalInstance(compartment,type,UINT32_MAX,initialValue); }
else
{
// Allocate a naturally aligned address to store the global at in the per-context data.
const U32 numBytes = getTypeByteWidth(type.valueType);
U32 dataOffset = (compartment->numGlobalBytes + numBytes - 1) & ~(numBytes - 1);
if(dataOffset + numBytes >= maxGlobalBytes) { return nullptr; }
compartment->numGlobalBytes = dataOffset + numBytes;
// Initialize the global value for each context, and the data used to initialize new contexts.
memcpy(compartment->initialContextGlobalData + dataOffset,&initialValue,numBytes);
for(Context* context : compartment->contexts)
{
memcpy(context->runtimeData->globalData + dataOffset,&initialValue,numBytes);
}
globalInstance = new GlobalInstance(compartment,type,dataOffset,initialValue);
}
globalInstance->id = compartment->globals.size();
compartment->globals.push_back(globalInstance);
return globalInstance;
}
GlobalInstance* cloneGlobal(GlobalInstance* global,Compartment* newCompartment)
{
return createGlobal(newCompartment,global->type,Value(global->type.valueType,global->initialValue));
}
void GlobalInstance::finalize()
{
Platform::Lock compartmentLock(compartment->mutex);
compartment->globals[id] = nullptr;
}
Value getGlobalValue(Context* context,GlobalInstance* global)
{
assert(context || !global->type.isMutable);
return Value(
global->type.valueType,
global->type.isMutable
? *(UntaggedValue*)(context->runtimeData->globalData + global->mutableDataOffset)
: global->initialValue);
}
Value setGlobalValue(Context* context,GlobalInstance* global,Value newValue)
{
assert(context);
assert(newValue.type == global->type.valueType);
assert(global->type.isMutable);
UntaggedValue& value = *(UntaggedValue*)(context->runtimeData->globalData + global->mutableDataOffset);
const Value previousValue = Value(global->type.valueType,value);
value = newValue;
return previousValue;
}
Compartment::Compartment()
: ObjectImpl(ObjectKind::compartment)
, mutex(Platform::createMutex())
, unalignedRuntimeData(nullptr)
, numGlobalBytes(0)
{
runtimeData = (CompartmentRuntimeData*)Platform::allocateAlignedVirtualPages(
compartmentReservedBytes >> Platform::getPageSizeLog2(),
compartmentRuntimeDataAlignmentLog2,
unalignedRuntimeData);
errorUnless(Platform::commitVirtualPages(
(U8*)runtimeData,
offsetof(CompartmentRuntimeData,contexts) >> Platform::getPageSizeLog2()));
runtimeData->compartment = this;
wavmIntrinsics = instantiateWAVMIntrinsics(this);
}
Compartment::~Compartment()
{
Platform::destroyMutex(mutex);
Platform::decommitVirtualPages((U8*)runtimeData,compartmentReservedBytes >> Platform::getPageSizeLog2());
Platform::freeAlignedVirtualPages(unalignedRuntimeData,
compartmentReservedBytes >> Platform::getPageSizeLog2(),
compartmentRuntimeDataAlignmentLog2);
runtimeData = nullptr;
unalignedRuntimeData = nullptr;
}
Compartment* createCompartment()
{
return new Compartment();
}
Compartment* cloneCompartment(Compartment* compartment)
{
Compartment* newCompartment = new Compartment;
Platform::Lock lock(compartment->mutex);
// Clone globals.
for(Uptr globalIndex = 0;globalIndex < compartment->globals.size();++globalIndex)
{
GlobalInstance* global = compartment->globals[globalIndex];
GlobalInstance* newGlobal = cloneGlobal(global,newCompartment);
SUPPRESS_UNUSED(newGlobal);
assert(newGlobal->id == global->id);
assert(newGlobal->mutableDataOffset == global->mutableDataOffset);
}
assert(newCompartment->numGlobalBytes == compartment->numGlobalBytes);
// Clone memories.
for(Uptr memoryIndex = 0;memoryIndex < compartment->memories.size();++memoryIndex)
{
MemoryInstance* memory = compartment->memories[memoryIndex];
MemoryInstance* newMemory = cloneMemory(memory,newCompartment);
SUPPRESS_UNUSED(newMemory);
assert(newMemory->id == memory->id);
}
// Clone tables.
for(Uptr tableIndex = 0;tableIndex < compartment->tables.size();++tableIndex)
{
TableInstance* table = compartment->tables[tableIndex];
TableInstance* newTable = cloneTable(table,newCompartment);
SUPPRESS_UNUSED(newTable);
assert(newTable->id == table->id);
}
return newCompartment;
}
Context* createContext(Compartment* compartment)
{
assert(compartment);
Context* context = new Context(compartment);
{
Platform::Lock lock(compartment->mutex);
// Allocate an ID for the context in the compartment.
context->id = compartment->contexts.size();
context->runtimeData = &compartment->runtimeData->contexts[context->id];
compartment->contexts.push_back(context);
// Commit the page(s) for the context's runtime data.
errorUnless(Platform::commitVirtualPages(
(U8*)context->runtimeData,
sizeof(ContextRuntimeData) >> Platform::getPageSizeLog2()));
// Initialize the context's global data.
memcpy(
context->runtimeData->globalData,
compartment->initialContextGlobalData,
compartment->numGlobalBytes);
}
return context;
}
void Context::finalize()
{
Platform::Lock compartmentLock(compartment->mutex);
compartment->contexts[id] = nullptr;
}
Compartment* getCompartmentFromContext(Context* context)
{
return context->compartment;
}
Context* cloneContext(Context* context,Compartment* newCompartment)
{
// Create a new context and initialize its runtime data with the values from the source context.
Context* clonedContext = createContext(newCompartment);
const Uptr numGlobalBytes = context->compartment->numGlobalBytes;
assert(numGlobalBytes <= newCompartment->numGlobalBytes);
memcpy(
clonedContext->runtimeData->globalData,
context->runtimeData->globalData,
numGlobalBytes);
return clonedContext;
}
Context* getContextFromRuntimeData(ContextRuntimeData* contextRuntimeData)
{
const CompartmentRuntimeData* compartmentRuntimeData = getCompartmentRuntimeData(contextRuntimeData);
const Uptr contextId = contextRuntimeData - compartmentRuntimeData->contexts;
Platform::Lock compartmentLock(compartmentRuntimeData->compartment->mutex);
return compartmentRuntimeData->compartment->contexts[contextId];
}
ContextRuntimeData* getContextRuntimeData(Context* context)
{
return context->runtimeData;
}
TableInstance* getTableFromRuntimeData(ContextRuntimeData* contextRuntimeData,Uptr tableId)
{
Compartment* compartment = getCompartmentRuntimeData(contextRuntimeData)->compartment;
Platform::Lock compartmentLock(compartment->mutex);
assert(tableId < compartment->tables.size());
return compartment->tables[tableId];
}
MemoryInstance* getMemoryFromRuntimeData(ContextRuntimeData* contextRuntimeData,Uptr memoryId)
{
Compartment* compartment = getCompartmentRuntimeData(contextRuntimeData)->compartment;
Platform::Lock compartmentLock(compartment->mutex);
return compartment->memories[memoryId];
}
}