summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Lib/Runtime/RuntimePrivate.h
blob: 362d28984be15e43ea0bdec3bc5bbcf0de86a33b (plain) (blame)
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
#pragma once

#include <atomic>
#include <cstddef>
#include <functional>
#include <memory>
#include "WAVM/IR/Module.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/DenseStaticIntSet.h"
#include "WAVM/Inline/HashMap.h"
#include "WAVM/Inline/HashSet.h"
#include "WAVM/Inline/IndexMap.h"
#include "WAVM/LLVMJIT/LLVMJIT.h"
#include "WAVM/Platform/Defines.h"
#include "WAVM/Platform/RWMutex.h"
#include "WAVM/Platform/VectorOverMMap.h"
#include "WAVM/Runtime/Intrinsics.h"
#include "WAVM/Runtime/Runtime.h"
#include "WAVM/RuntimeABI/RuntimeABI.h"

namespace WAVM { namespace Intrinsics {
	struct Module;
}}

namespace WAVM { namespace Runtime {

	// A private base class for all runtime objects that are garbage collected.
	struct GCObject : Object
	{
		Compartment* const compartment;
		mutable std::atomic<Uptr> numRootReferences{0};
		void* userData{nullptr};
		void (*finalizeUserData)(void*);
		std::string debugName;

		GCObject(ObjectKind inKind, Compartment* inCompartment, std::string&& inDebugName);
		virtual ~GCObject();
	};

	// An instance of a WebAssembly Table.
	struct Table : GCObject
	{
		struct Element
		{
			std::atomic<Uptr> biasedValue;
		};

		Uptr id = UINTPTR_MAX;

		const IR::ReferenceType elementType;
		const bool isShared;
		const IR::IndexType indexType;
		const U64 maxElements;

		Element* elements = nullptr;
		Uptr numReservedBytes = 0;
		Uptr numReservedElements = 0;

		mutable Platform::RWMutex resizingMutex;
		std::atomic<Uptr> numElements{0};

		ResourceQuotaRef resourceQuota;

		Table(Compartment* inCompartment,
			  const IR::TableType& inType,
			  std::string&& inDebugName,
			  ResourceQuotaRefParam inResourceQuota)
		: GCObject(ObjectKind::table, inCompartment, std::move(inDebugName))
		, elementType(inType.elementType)
		, isShared(inType.isShared)
		, indexType(inType.indexType)
		, maxElements(inType.size.max)
		, resourceQuota(inResourceQuota)
		{
		}
		~Table() override;

		static void setCurrentTable(Table* table);
		static Table* getCurrentTable();
	};

	// This is used as a sentinel value for table elements that are out-of-bounds. The address of
	// this Object is subtracted from every address stored in the table, so zero-initialized pages
	// at the end of the array will, when re-adding this Function's address, point to this Object.
	extern Object* getOutOfBoundsElement();

	// An instance of a WebAssembly Memory.
	struct Memory : GCObject
	{
		Uptr id = UINTPTR_MAX;

		const bool isShared;
		const IR::IndexType indexType;
		const U64 maxPages;

		VectorOverMMap data;

		U8* getBaseAddress();
		size_t getNumReservedBytes();

		static constexpr size_t getNumGuardBytes()
		{
			return VectorOverMMap::getNumGuardBytes();
		}

		mutable Platform::RWMutex resizingMutex;
		std::atomic<Uptr> numPages{0};

		ResourceQuotaRef resourceQuota;

		Memory(Compartment* inCompartment,
			   const IR::MemoryType& inType,
			   std::string&& inDebugName,
			   ResourceQuotaRefParam inResourceQuota)
		: GCObject(ObjectKind::memory, inCompartment, std::move(inDebugName))
		, isShared(inType.isShared)
		, indexType(inType.indexType)
		, maxPages(inType.size.max)
		, resourceQuota(inResourceQuota)
		{
		}
		~Memory() override;

		static void setCurrentMemory(Memory* memory);
		static Memory* getCurrentMemory();
	};

	// An instance of a WebAssembly global.
	struct Global : GCObject
	{
		Uptr id = UINTPTR_MAX;

		const IR::GlobalType type;
		const U32 mutableGlobalIndex;
		IR::UntaggedValue initialValue;
		bool hasBeenInitialized;

		Global(Compartment* inCompartment,
			   IR::GlobalType inType,
			   U32 inMutableGlobalId,
			   std::string&& inDebugName,
			   IR::UntaggedValue inInitialValue = IR::UntaggedValue())
		: GCObject(ObjectKind::global, inCompartment, std::move(inDebugName))
		, type(inType)
		, mutableGlobalIndex(inMutableGlobalId)
		, initialValue(inInitialValue)
		, hasBeenInitialized(false)
		{
		}
		~Global() override;
	};

	// An instance of a WebAssembly exception type.
	struct ExceptionType : GCObject
	{
		Uptr id = UINTPTR_MAX;

		IR::ExceptionType sig;

		ExceptionType(Compartment* inCompartment,
					  IR::ExceptionType inSig,
					  std::string&& inDebugName)
		: GCObject(ObjectKind::exceptionType, inCompartment, std::move(inDebugName)), sig(inSig)
		{
		}

		~ExceptionType() override;
	};

	typedef std::vector<std::shared_ptr<std::vector<U8>>> DataSegmentVector;
	typedef std::vector<std::shared_ptr<IR::ElemSegment::Contents>> ElemSegmentVector;

	// A compiled WebAssembly module.
	struct Module
	{
		IR::Module ir;
		std::vector<U8> objectCode;

		Module(IR::Module&& inIR, std::vector<U8>&& inObjectCode)
		: ir(inIR), objectCode(std::move(inObjectCode))
		{
		}
	};

	// An instance of a WebAssembly module.
	struct Instance : GCObject
	{
		const Uptr id;

		HashMap<std::string, Object*> exportMap;
		const std::vector<Object*> exports;

		const std::vector<Function*> functions;
		const std::vector<Table*> tables;
		const std::vector<Memory*> memories;
		const std::vector<Global*> globals;
		const std::vector<ExceptionType*> exceptionTypes;

		Function* const startFunction;

		mutable Platform::RWMutex dataSegmentsMutex;
		DataSegmentVector dataSegments;

		mutable Platform::RWMutex elemSegmentsMutex;
		ElemSegmentVector elemSegments;

		const std::shared_ptr<LLVMJIT::Module> jitModule;

		ResourceQuotaRef resourceQuota;

		Instance(Compartment* inCompartment,
				 Uptr inID,
				 HashMap<std::string, Object*>&& inExportMap,
				 std::vector<Object*>&& inExports,
				 std::vector<Function*>&& inFunctions,
				 std::vector<Table*>&& inTables,
				 std::vector<Memory*>&& inMemories,
				 std::vector<Global*>&& inGlobals,
				 std::vector<ExceptionType*>&& inExceptionTypes,
				 Function* inStartFunction,
				 DataSegmentVector&& inPassiveDataSegments,
				 ElemSegmentVector&& inPassiveElemSegments,
				 std::shared_ptr<LLVMJIT::Module>&& inJITModule,
				 std::string&& inDebugName,
				 ResourceQuotaRefParam inResourceQuota)
		: GCObject(ObjectKind::instance, inCompartment, std::move(inDebugName))
		, id(inID)
		, exportMap(std::move(inExportMap))
		, exports(std::move(inExports))
		, functions(std::move(inFunctions))
		, tables(std::move(inTables))
		, memories(std::move(inMemories))
		, globals(std::move(inGlobals))
		, exceptionTypes(std::move(inExceptionTypes))
		, startFunction(inStartFunction)
		, dataSegments(std::move(inPassiveDataSegments))
		, elemSegments(std::move(inPassiveElemSegments))
		, jitModule(std::move(inJITModule))
		, resourceQuota(inResourceQuota)
		{
		}

		virtual ~Instance() override;
	};

	struct Context : GCObject
	{
		Uptr id = UINTPTR_MAX;
		struct ContextRuntimeData* runtimeData = nullptr;
		void (*checkStackDepthCallback)() = nullptr;

		Context(Compartment* inCompartment, std::string&& inDebugName)
		: GCObject(ObjectKind::context, inCompartment, std::move(inDebugName))
		{
		}
		~Context();

		void setCheckStackDepthCallback(void (*callback)()) { checkStackDepthCallback = callback; }
	};

	struct Compartment : GCObject
	{
		mutable Platform::RWMutex mutex;

		struct CompartmentRuntimeData* const runtimeData;
		U8* const unalignedRuntimeData;

		IndexMap<Uptr, Table*> tables;
		IndexMap<Uptr, Memory*> memories;
		IndexMap<Uptr, Global*> globals;
		IndexMap<Uptr, ExceptionType*> exceptionTypes;
		IndexMap<Uptr, Instance*> instances;
		IndexMap<Uptr, Context*> contexts;
		IndexMap<Uptr, Foreign*> foreigns;

		DenseStaticIntSet<U32, maxMutableGlobals> globalDataAllocationMask;
		IR::UntaggedValue initialContextMutableGlobals[maxMutableGlobals];

		Compartment(std::string&& inDebugName,
					struct CompartmentRuntimeData* inRuntimeData,
					U8* inUnalignedRuntimeData);
		~Compartment();
	};

	struct Foreign : GCObject
	{
		Uptr id = UINTPTR_MAX;

		Foreign(Compartment* inCompartment, std::string&& inDebugName)
		: GCObject(ObjectKind::foreign, inCompartment, std::move(inDebugName))
		{
		}
	};

	struct ResourceQuota
	{
		template<typename Value> struct CurrentAndMax
		{
			CurrentAndMax(Value inMax) : current{0}, max(inMax) {}

			bool allocate(Value delta)
			{
				Platform::RWMutex::ExclusiveLock lock(mutex);

				// Make sure the delta doesn't make current overflow.
				if(current + delta < current) { return false; }

				if(current + delta > max) { return false; }

				current += delta;
				return true;
			}

			void free(Value delta)
			{
				Platform::RWMutex::ExclusiveLock lock(mutex);
				WAVM_ASSERT(current - delta <= current);
				current -= delta;
			}

			Value getCurrent() const
			{
				Platform::RWMutex::ShareableLock lock(mutex);
				return current;
			}
			Value getMax() const
			{
				Platform::RWMutex::ShareableLock lock(mutex);
				return max;
			}
			void setMax(Value newMax)
			{
				Platform::RWMutex::ExclusiveLock lock(mutex);
				max = newMax;
			}

		private:
			mutable Platform::RWMutex mutex;
			Value current;
			Value max;
		};

		CurrentAndMax<Uptr> memoryPages{UINTPTR_MAX};
		CurrentAndMax<Uptr> tableElems{UINTPTR_MAX};
	};

	WAVM_DECLARE_INTRINSIC_MODULE(wavmIntrinsics);
	WAVM_DECLARE_INTRINSIC_MODULE(wavmIntrinsicsAtomics);
	WAVM_DECLARE_INTRINSIC_MODULE(wavmIntrinsicsException);
	WAVM_DECLARE_INTRINSIC_MODULE(wavmIntrinsicsMemory);
	WAVM_DECLARE_INTRINSIC_MODULE(wavmIntrinsicsTable);

	// Checks whether an address is owned by a table or memory.
	bool isAddressOwnedByTable(U8* address, Table*& outTable, Uptr& outTableIndex);
	bool isAddressOwnedByMemory(U8* address, Memory*& outMemory, Uptr& outMemoryAddress);

	// Clones objects into a new compartment with the same ID.
	Table* cloneTable(Table* memory, Compartment* newCompartment);
	Memory* cloneMemory(Memory* memory, Compartment* newCompartment);
	ExceptionType* cloneExceptionType(ExceptionType* exceptionType, Compartment* newCompartment);
	Instance* cloneInstance(Instance* instance, Compartment* newCompartment);

	// Clone a global with same ID and mutable data offset (if mutable) in a new compartment.
	Global* cloneGlobal(Global* global, Compartment* newCompartment);

	Instance* getInstanceFromRuntimeData(ContextRuntimeData* contextRuntimeData, Uptr instanceId);
	Table* getTableFromRuntimeData(ContextRuntimeData* contextRuntimeData, Uptr tableId);
	Memory* getMemoryFromRuntimeData(ContextRuntimeData* contextRuntimeData, Uptr memoryId);

	// Initialize a data segment (equivalent to executing a memory.init instruction).
	void initDataSegment(Instance* instance,
						 Uptr dataSegmentIndex,
						 const std::vector<U8>* dataVector,
						 Memory* memory,
						 Uptr destAddress,
						 Uptr sourceOffset,
						 Uptr numBytes);

	// Initialize a table segment (equivalent to executing a table.init instruction).
	void initElemSegment(Instance* instance,
						 Uptr elemSegmentIndex,
						 const IR::ElemSegment::Contents* contents,
						 Table* table,
						 Uptr destOffset,
						 Uptr sourceOffset,
						 Uptr numElems);

	// This function is like Runtime::instantiateModule, but allows binding function imports
	// directly to native code. The interpretation of each FunctionImportBinding is determined by
	// the import's calling convention:
	// An import with CallingConvention::wasm reads FunctionImportBinding::wasmFunction,
	// but all other imports read FunctionImportBinding::nativeFunction.
	struct FunctionImportBinding
	{
		union
		{
			Function* wasmFunction;
			const void* nativeFunction;
		};

		FunctionImportBinding(Function* inWASMFunction) : wasmFunction(inWASMFunction) {}
		FunctionImportBinding(void* inNativeFunction) : nativeFunction(inNativeFunction) {}
	};
	Instance* instantiateModuleInternal(Compartment* compartment,
										ModuleConstRefParam module,
										std::vector<FunctionImportBinding>&& functionImports,
										std::unordered_map<Uptr, Uptr>&& importIndexToSelfDefinedFunctionIndex,
										std::vector<Table*>&& tableImports,
										std::vector<Memory*>&& memoryImports,
										std::vector<Global*>&& globalImports,
										std::vector<ExceptionType*>&& exceptionTypeImports,
										std::string&& debugName,
										ResourceQuotaRefParam resourceQuota = ResourceQuotaRef());
}}

namespace WAVM { namespace Intrinsics {
	HashMap<std::string, Function*> getUninstantiatedFunctions(
		const std::initializer_list<const Intrinsics::Module*>& moduleRefs);
}}

namespace WAVM { namespace Runtime {
	void setCurrentDeadline(std::optional<struct timespec> deadline);
	bool isCurrentDeadlineReached();
	struct timespec getInstant();
}}