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
|
#include <inttypes.h>
#include <atomic>
#include <utility>
#include <vector>
#include "RuntimePrivate.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Errors.h"
#include "WAVM/Inline/Hash.h"
#include "WAVM/Inline/HashSet.h"
#include "WAVM/Inline/Timing.h"
#include "WAVM/Logging/Logging.h"
#include "WAVM/Platform/RWMutex.h"
#include "WAVM/Runtime/Runtime.h"
using namespace WAVM;
using namespace WAVM::Runtime;
Runtime::GCObject::GCObject(ObjectKind inKind,
Compartment* inCompartment,
std::string&& inDebugName)
: Object{inKind}
, compartment(inCompartment)
, userData(nullptr)
, finalizeUserData(nullptr)
, debugName(inDebugName)
{
}
#define IMPLEMENT_GCOBJECT_REFCOUNTING(Type) \
void Runtime::addGCRoot(const Type* object) { ++object->numRootReferences; } \
void Runtime::removeGCRoot(const Type* object) noexcept { --object->numRootReferences; }
IMPLEMENT_GCOBJECT_REFCOUNTING(Table)
IMPLEMENT_GCOBJECT_REFCOUNTING(Memory)
IMPLEMENT_GCOBJECT_REFCOUNTING(Global)
IMPLEMENT_GCOBJECT_REFCOUNTING(ExceptionType)
IMPLEMENT_GCOBJECT_REFCOUNTING(Instance)
IMPLEMENT_GCOBJECT_REFCOUNTING(Context)
IMPLEMENT_GCOBJECT_REFCOUNTING(Compartment)
IMPLEMENT_GCOBJECT_REFCOUNTING(Foreign)
void Runtime::addGCRoot(const Function* function)
{
WAVM_ASSERT(function->mutableData);
++function->mutableData->numRootReferences;
}
void Runtime::removeGCRoot(const Function* function) noexcept
{
WAVM_ASSERT(function->mutableData);
--function->mutableData->numRootReferences;
}
void Runtime::addGCRoot(const Object* object)
{
if(object->kind == ObjectKind::function) { addGCRoot((const Function*)object); }
else
{
const GCObject* gcObject = static_cast<const GCObject*>(object);
++gcObject->numRootReferences;
}
}
void Runtime::removeGCRoot(const Object* object) noexcept
{
if(object->kind == ObjectKind::function) { removeGCRoot((const Function*)object); }
else
{
GCObject* gcObject = (GCObject*)object;
--gcObject->numRootReferences;
}
}
struct GCState
{
Compartment* compartment;
HashSet<GCObject*> unreferencedObjects;
std::vector<GCObject*> pendingScanObjects;
GCState(Compartment* inCompartment) : compartment(inCompartment) {}
void visitReference(Object* object)
{
if(object)
{
if(object->kind == ObjectKind::function)
{
Function* function = asFunction(object);
if(function->instanceId != UINTPTR_MAX)
{
WAVM_ASSERT(compartment->instances.contains(function->instanceId));
Instance* instance = compartment->instances[function->instanceId];
visitReference(instance);
}
}
else if(unreferencedObjects.remove((GCObject*)object))
{
pendingScanObjects.push_back((GCObject*)object);
}
}
}
template<typename Array> void visitReferenceArray(const Array& array)
{
for(auto reference : array) { visitReference(asObject(reference)); }
}
void initGCObject(GCObject* object, bool forceRoot = false)
{
if(forceRoot || object->numRootReferences > 0) { pendingScanObjects.push_back(object); }
else
{
unreferencedObjects.add(object);
}
}
void scanObject(GCObject* object)
{
WAVM_ASSERT(!object->compartment || object->compartment == compartment);
visitReference(object->compartment);
// Gather the child references for this object based on its kind.
switch(object->kind)
{
case ObjectKind::table: {
Table* table = asTable(object);
Platform::RWMutex::ShareableLock resizingLock(table->resizingMutex);
const Uptr numElements = getTableNumElements(table);
for(Uptr elementIndex = 0; elementIndex < numElements; ++elementIndex)
{ visitReference(getTableElement(table, elementIndex)); }
break;
}
case ObjectKind::global: {
Global* global = asGlobal(object);
if(isReferenceType(global->type.valueType))
{
if(global->type.isMutable)
{
visitReference(
compartment->initialContextMutableGlobals[global->mutableGlobalIndex]
.object);
for(Context* context : compartment->contexts)
{
visitReference(
context->runtimeData->mutableGlobals[global->mutableGlobalIndex]
.object);
}
}
visitReference(global->initialValue.object);
}
break;
}
case ObjectKind::instance: {
Instance* instance = asInstance(object);
visitReferenceArray(instance->functions);
visitReferenceArray(instance->tables);
visitReferenceArray(instance->memories);
visitReferenceArray(instance->globals);
visitReferenceArray(instance->exceptionTypes);
break;
}
case ObjectKind::compartment: {
WAVM_ASSERT(object == compartment);
break;
}
case ObjectKind::memory:
case ObjectKind::exceptionType:
case ObjectKind::context: break;
case ObjectKind::function:
case ObjectKind::foreign:
case ObjectKind::invalid:
default: WAVM_UNREACHABLE();
};
}
};
static bool collectGarbageImpl(Compartment* compartment)
{
Platform::RWMutex::ExclusiveLock compartmentLock(compartment->mutex);
Timing::Timer timer;
GCState state(compartment);
// Initialize the GC state from the compartment's various sets of objects.
state.initGCObject(compartment);
for(Instance* instance : compartment->instances)
{
if(instance)
{
// Transfer root markings from functions to their instance.
bool hasRootFunction = false;
for(Function* function : instance->functions)
{
if(function && function->mutableData->numRootReferences
&& function->instanceId == instance->id)
{
hasRootFunction = true;
break;
}
}
state.initGCObject(instance, hasRootFunction);
}
}
for(Memory* memory : compartment->memories) { state.initGCObject(memory); }
for(Table* table : compartment->tables) { state.initGCObject(table); }
for(ExceptionType* exceptionType : compartment->exceptionTypes)
{ state.initGCObject(exceptionType); }
for(Global* global : compartment->globals) { state.initGCObject(global); }
for(Context* context : compartment->contexts) { state.initGCObject(context); }
// Scan the objects added to the referenced set so far: gather their child references and
// recurse.
const Uptr numInitialObjects
= state.pendingScanObjects.size() + state.unreferencedObjects.size();
const Uptr numRoots = state.pendingScanObjects.size();
while(state.pendingScanObjects.size())
{
GCObject* object = state.pendingScanObjects.back();
state.pendingScanObjects.pop_back();
state.scanObject(object);
};
// Delete each unreferenced object that isn't the compartment.
bool wasCompartmentUnreferenced = false;
for(GCObject* object : state.unreferencedObjects)
{
if(object == compartment) { wasCompartmentUnreferenced = true; }
else
{
delete object;
}
}
// Delete the compartment last, if it wasn't referenced.
compartmentLock.unlock();
if(wasCompartmentUnreferenced) { delete compartment; }
Log::printf(Log::metrics,
"Collected garbage in %.2fms: %" WAVM_PRIuPTR " roots, %" WAVM_PRIuPTR
" objects, %" WAVM_PRIuPTR " garbage\n",
timer.getMilliseconds(),
numRoots,
numInitialObjects,
Uptr(state.unreferencedObjects.size()));
return wasCompartmentUnreferenced;
}
void Runtime::collectCompartmentGarbage(Compartment* compartment)
{
collectGarbageImpl(compartment);
}
bool Runtime::tryCollectCompartment(GCPointer<Compartment>&& compartmentRootRef)
{
Compartment* compartment = &*compartmentRootRef;
compartmentRootRef = nullptr;
return collectGarbageImpl(compartment);
}
|