summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Lib/Runtime/Compartment.cpp
blob: b202203a25c551503f0eff484f6eb4c7047e9803 (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
#include <stddef.h>
#include <atomic>
#include <memory>
#include <vector>
#include "RuntimePrivate.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Timing.h"
#include "WAVM/Platform/Memory.h"
#include "WAVM/Platform/RWMutex.h"
#include "WAVM/Runtime/Runtime.h"
#include "WAVM/RuntimeABI/RuntimeABI.h"

using namespace WAVM;
using namespace WAVM::Runtime;

Runtime::Compartment::Compartment(std::string&& inDebugName,
								  struct CompartmentRuntimeData* inRuntimeData,
								  U8* inUnalignedRuntimeData)
: GCObject(ObjectKind::compartment, this, std::move(inDebugName))
, runtimeData(inRuntimeData)
, unalignedRuntimeData(inUnalignedRuntimeData)
, tables(0, maxTables - 1)
, memories(0, maxMemories - 1)
// Use UINTPTR_MAX as an invalid ID for globals, exception types, and instances.
, globals(0, UINTPTR_MAX - 1)
, exceptionTypes(0, UINTPTR_MAX - 1)
, instances(0, UINTPTR_MAX - 1)
, contexts(0, maxContexts - 1)
, foreigns(0, UINTPTR_MAX - 1)
{
	runtimeData->compartment = this;
}

Runtime::Compartment::~Compartment()
{
	Platform::RWMutex::ExclusiveLock compartmentLock(mutex);

	WAVM_ASSERT(!memories.size());
	WAVM_ASSERT(!tables.size());
	WAVM_ASSERT(!exceptionTypes.size());
	WAVM_ASSERT(!globals.size());
	WAVM_ASSERT(!instances.size());
	WAVM_ASSERT(!contexts.size());
	WAVM_ASSERT(!foreigns.size());

	Platform::freeAlignedVirtualPages(unalignedRuntimeData,
									  compartmentReservedBytes >> Platform::getBytesPerPageLog2(),
									  compartmentRuntimeDataAlignmentLog2);
	Platform::deregisterVirtualAllocation(offsetof(CompartmentRuntimeData, contexts));
}

static CompartmentRuntimeData* initCompartmentRuntimeData(U8*& outUnalignedRuntimeData)
{
	CompartmentRuntimeData* runtimeData
		= (CompartmentRuntimeData*)Platform::allocateAlignedVirtualPages(
			compartmentReservedBytes >> Platform::getBytesPerPageLog2(),
			compartmentRuntimeDataAlignmentLog2,
			outUnalignedRuntimeData);

	WAVM_ERROR_UNLESS(Platform::commitVirtualPages(
		(U8*)runtimeData,
		offsetof(CompartmentRuntimeData, contexts) >> Platform::getBytesPerPageLog2()));
	Platform::registerVirtualAllocation(offsetof(CompartmentRuntimeData, contexts));

	return runtimeData;
}

Compartment* Runtime::createCompartment(std::string&& debugName)
{
	U8* unalignedRuntimeData = nullptr;
	CompartmentRuntimeData* runtimeData = initCompartmentRuntimeData(unalignedRuntimeData);
	if(!runtimeData) { return nullptr; }

	return new Compartment(std::move(debugName), runtimeData, unalignedRuntimeData);
}

Compartment* Runtime::cloneCompartment(const Compartment* compartment, std::string&& debugName)
{
	Timing::Timer timer;

	U8* unalignedRuntimeData = nullptr;
	CompartmentRuntimeData* runtimeData = initCompartmentRuntimeData(unalignedRuntimeData);
	if(!runtimeData) { return nullptr; }

	Compartment* newCompartment
		= new Compartment(std::move(debugName), runtimeData, unalignedRuntimeData);
	if(!newCompartment) { goto error; }
	else
	{
		Platform::RWMutex::ShareableLock compartmentLock(compartment->mutex);

		// Clone tables.
		for(Table* table : compartment->tables)
		{
			Table* newTable = cloneTable(table, newCompartment);
			if(!newTable) { goto error; }
			WAVM_ASSERT(newTable->id == table->id);
		}

		// Clone memories.
		for(Memory* memory : compartment->memories)
		{
			Memory* newMemory = cloneMemory(memory, newCompartment);
			if(!newMemory) { goto error; }
			WAVM_ASSERT(newMemory->id == memory->id);
		}

		// Clone globals.
		newCompartment->globalDataAllocationMask = compartment->globalDataAllocationMask;
		memcpy(newCompartment->initialContextMutableGlobals,
			   compartment->initialContextMutableGlobals,
			   sizeof(newCompartment->initialContextMutableGlobals));
		for(Global* global : compartment->globals)
		{
			Global* newGlobal = cloneGlobal(global, newCompartment);
			if(!newGlobal) { goto error; }
			WAVM_ASSERT(newGlobal->id == global->id);
			WAVM_ASSERT(newGlobal->mutableGlobalIndex == global->mutableGlobalIndex);
		}

		// Clone exception types.
		for(ExceptionType* exceptionType : compartment->exceptionTypes)
		{
			ExceptionType* newExceptionType = cloneExceptionType(exceptionType, newCompartment);
			if(!newExceptionType) { goto error; }
			WAVM_ASSERT(newExceptionType->id == exceptionType->id);
		}

		// Clone instances.
		for(Instance* instance : compartment->instances)
		{
			Instance* newInstance = cloneInstance(instance, newCompartment);
			if(!newInstance) { goto error; }
			WAVM_ASSERT(newInstance->id == instance->id);
		}

		Timing::logTimer("Cloned compartment", timer);
		return newCompartment;
	}

error:
	// If there was an error, clean up the partially created compartment.
	if(newCompartment)
	{ WAVM_ERROR_UNLESS(tryCollectCompartment(GCPointer<Compartment>(newCompartment))); }
	return nullptr;
}

Object* Runtime::remapToClonedCompartment(const Object* object, const Compartment* newCompartment)
{
	if(!object) { return nullptr; }
	if(object->kind == ObjectKind::function) { return const_cast<Object*>(object); }

	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	switch(object->kind)
	{
	case ObjectKind::table: return newCompartment->tables[asTable(object)->id];
	case ObjectKind::memory: return newCompartment->memories[asMemory(object)->id];
	case ObjectKind::global: return newCompartment->globals[asGlobal(object)->id];
	case ObjectKind::exceptionType:
		return newCompartment->exceptionTypes[asExceptionType(object)->id];
	case ObjectKind::instance: return newCompartment->instances[asInstance(object)->id];

	case ObjectKind::function:
	case ObjectKind::context:
	case ObjectKind::compartment:
	case ObjectKind::foreign:
	case ObjectKind::invalid:
	default: WAVM_UNREACHABLE();
	};
}

Function* Runtime::remapToClonedCompartment(const Function* function,
											const Compartment* newCompartment)
{
	return const_cast<Function*>(function);
}
Table* Runtime::remapToClonedCompartment(const Table* table, const Compartment* newCompartment)
{
	if(!table) { return nullptr; }
	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	return newCompartment->tables[table->id];
}
Memory* Runtime::remapToClonedCompartment(const Memory* memory, const Compartment* newCompartment)
{
	if(!memory) { return nullptr; }
	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	return newCompartment->memories[memory->id];
}
Global* Runtime::remapToClonedCompartment(const Global* global, const Compartment* newCompartment)
{
	if(!global) { return nullptr; }
	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	return newCompartment->globals[global->id];
}
ExceptionType* Runtime::remapToClonedCompartment(const ExceptionType* exceptionType,
												 const Compartment* newCompartment)
{
	if(!exceptionType) { return nullptr; }
	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	return newCompartment->exceptionTypes[exceptionType->id];
}
Instance* Runtime::remapToClonedCompartment(const Instance* instance,
											const Compartment* newCompartment)
{
	if(!instance) { return nullptr; }
	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	return newCompartment->instances[instance->id];
}
Foreign* Runtime::remapToClonedCompartment(const Foreign* foreign,
										   const Compartment* newCompartment)
{
	if(!foreign) { return nullptr; }
	Platform::RWMutex::ShareableLock compartmentLock(newCompartment->mutex);
	return newCompartment->foreigns[foreign->id];
}

bool Runtime::isInCompartment(const Object* object, const Compartment* compartment)
{
	if(object->kind == ObjectKind::function)
	{
		// The function may be in multiple compartments, but if this compartment maps the function's
		// instanceId to a Instance with the LLVMJIT LoadedModule that contains this
		// function, then the function is in this compartment.
		Function* function = (Function*)object;

		// Treat functions with instanceId=UINTPTR_MAX as if they are in all compartments.
		if(function->instanceId == UINTPTR_MAX) { return true; }

		Platform::RWMutex::ShareableLock compartmentLock(compartment->mutex);
		if(!compartment->instances.contains(function->instanceId)) { return false; }
		Instance* instance = compartment->instances[function->instanceId];
		return instance->jitModule.get() == function->mutableData->jitModule;
	}
	else
	{
		GCObject* gcObject = (GCObject*)object;
		return gcObject->compartment == compartment;
	}
}