forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomain.cpp
More file actions
434 lines (379 loc) · 15.4 KB
/
Domain.cpp
File metadata and controls
434 lines (379 loc) · 15.4 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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "hermes/VM/Domain.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/GCPointer-inline.h"
#include "hermes/VM/JSLib.h"
#include "hermes/VM/Profiler/SamplingProfiler.h"
#include "llvh/Support/Debug.h"
#define DEBUG_TYPE "serialize"
namespace hermes {
namespace vm {
VTable Domain::vt{CellKind::DomainKind,
cellSize<Domain>(),
_finalizeImpl,
_markWeakImpl,
_mallocSizeImpl};
void DomainBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
const auto *self = static_cast<const Domain *>(cell);
mb.addField("cjsModules", &self->cjsModules_);
mb.addField("throwingRequire", &self->throwingRequire_);
}
#ifdef HERMESVM_SERIALIZE
Domain::Domain(Deserializer &d) : GCCell(&d.getRuntime()->getHeap(), &vt) {
if (d.readInt<uint8_t>()) {
cjsModules_.set(
d.getRuntime(),
Domain::deserializeArrayStorage(d),
&d.getRuntime()->getHeap());
}
// Field llvh::DenseMap<SymbolID, uint32_t> cjsModuleTable_{};
size_t size = d.readInt<size_t>();
for (size_t i = 0; i < size; i++) {
auto res = cjsModuleTable_
.try_emplace(
SymbolID::unsafeCreate(d.readInt<uint32_t>()),
d.readInt<uint32_t>())
.second;
if (!res) {
hermes_fatal("Shouldn't fail to insert during deserialization");
}
}
// Field CopyableVector<RuntimeModule *> runtimeModules_{};
size = d.readInt<size_t>();
for (size_t i = 0; i < size; i++) {
runtimeModules_.push_back(
RuntimeModule::deserialize(d), &d.getRuntime()->getHeap());
}
d.readRelocation(&throwingRequire_, RelocationKind::GCPointer);
}
void DomainSerialize(Serializer &s, const GCCell *cell) {
auto *self = vmcast<const Domain>(cell);
// If we have an ArrayStorage serialize it here.
bool hasArray = (bool)self->cjsModules_;
s.writeInt<uint8_t>(hasArray);
if (hasArray) {
Domain::serializeArrayStorage(s, self->cjsModules_.get(s.getRuntime()));
}
// Field llvh::DenseMap<SymbolID, uint32_t> cjsModuleTable_{};
size_t size = self->cjsModuleTable_.size();
s.writeInt<size_t>(size);
for (auto it = self->cjsModuleTable_.begin();
it != self->cjsModuleTable_.end();
it++) {
s.writeInt<uint32_t>(it->first.unsafeGetRaw());
s.writeInt<uint32_t>(it->second);
}
// Field CopyableVector<RuntimeModule *> runtimeModules_{};
// Domain owns RuntimeModules. Call serialize funtion for them here.
size = self->runtimeModules_.size();
s.writeInt<size_t>(size);
for (size_t i = 0; i < size; i++) {
self->runtimeModules_[i]->serialize(s);
}
s.writeRelocation(self->throwingRequire_.get(s.getRuntime()));
s.endObject(cell);
}
void DomainDeserialize(Deserializer &d, CellKind kind) {
assert(kind == CellKind::DomainKind && "Expected Domain");
void *mem = d.getRuntime()->alloc</*fixedSize*/ true, HasFinalizer::Yes>(
cellSize<Domain>());
auto *cell = new (mem) Domain(d);
auto &samplingProfiler = SamplingProfiler::getInstance();
samplingProfiler->increaseDomainCount();
d.endObject(cell);
}
void Domain::serializeArrayStorage(Serializer &s, const ArrayStorage *cell) {
assert(
cell->size() % runtimeModuleOffset == 0 && "Invalid ArrayStorage size");
s.writeInt<ArrayStorage::size_type>(cell->capacity());
s.writeInt<ArrayStorage::size_type>(cell->size());
for (ArrayStorage::size_type i = 0; i < cell->size(); i += CJSModuleSize) {
s.writeHermesValue(cell->data()[i + CachedExportsOffset]);
s.writeHermesValue(cell->data()[i + ModuleOffset]);
s.writeHermesValue(cell->data()[i + FunctionIndexOffset]);
s.writeHermesValue(
cell->data()[i + runtimeModuleOffset], /* nativePointer */ true);
}
s.endObject(cell);
}
ArrayStorage *Domain::deserializeArrayStorage(Deserializer &d) {
ArrayStorage::size_type capacity = d.readInt<ArrayStorage::size_type>();
ArrayStorage::size_type size = d.readInt<ArrayStorage::size_type>();
assert(size % runtimeModuleOffset == 0 && "Invalid ArrayStorage size");
auto cjsModulesRes = ArrayStorage::create(d.getRuntime(), capacity, size);
if (LLVM_UNLIKELY(cjsModulesRes == ExecutionStatus::EXCEPTION)) {
hermes_fatal("fail to allocate memory for CJSModules");
}
auto *cell = vmcast<ArrayStorage>(*cjsModulesRes);
for (ArrayStorage::size_type i = 0; i < cell->size(); i += CJSModuleSize) {
d.readHermesValue(&cell->data()[i + CachedExportsOffset]);
d.readHermesValue(&cell->data()[i + ModuleOffset]);
d.readHermesValue(&cell->data()[i + FunctionIndexOffset]);
d.readHermesValue(
&cell->data()[i + runtimeModuleOffset], /* nativePointer */ true);
}
d.endObject(cell);
return cell;
}
#endif
PseudoHandle<Domain> Domain::create(Runtime *runtime) {
void *mem =
runtime->alloc</*fixedSize*/ true, HasFinalizer::Yes>(cellSize<Domain>());
auto self = createPseudoHandle(new (mem) Domain(runtime));
auto &samplingProfiler = SamplingProfiler::getInstance();
samplingProfiler->increaseDomainCount();
return self;
}
void Domain::_finalizeImpl(GCCell *cell, GC *gc) {
auto *self = vmcast<Domain>(cell);
self->~Domain();
auto &samplingProfiler = SamplingProfiler::getInstance();
samplingProfiler->decreaseDomainCount();
}
Domain::~Domain() {
for (RuntimeModule *rm : runtimeModules_) {
delete rm;
}
}
PseudoHandle<NativeFunction> Domain::getThrowingRequire(
Runtime *runtime) const {
return createPseudoHandle(throwingRequire_.get(runtime));
}
void Domain::_markWeakImpl(GCCell *cell, WeakRefAcceptor &acceptor) {
auto *self = reinterpret_cast<Domain *>(cell);
self->markWeakRefs(acceptor);
}
void Domain::markWeakRefs(WeakRefAcceptor &acceptor) {
for (RuntimeModule *rm : runtimeModules_) {
rm->markDomainRef(acceptor);
}
}
size_t Domain::_mallocSizeImpl(GCCell *cell) {
auto *self = vmcast<Domain>(cell);
return self->cjsModuleTable_.getMemorySize() +
self->runtimeModules_.capacity_in_bytes();
}
ExecutionStatus Domain::importCJSModuleTable(
Handle<Domain> self,
Runtime *runtime,
RuntimeModule *runtimeModule) {
if (runtimeModule->getBytecode()->getCJSModuleTable().empty() &&
runtimeModule->getBytecode()->getCJSModuleTableStatic().empty()) {
// Nothing to do, avoid allocating and simply return.
return ExecutionStatus::RETURNED;
}
static_assert(
CJSModuleSize < 10, "CJSModuleSize must be small to avoid overflow");
MutableHandle<ArrayStorage> cjsModules{runtime};
if (!self->cjsModules_) {
// Create the module table on first import.
// If module IDs are contiguous and start from 0, we won't need to resize
// for this RuntimeModule.
const uint64_t firstSegmentModules =
runtimeModule->getBytecode()->getCJSModuleTable().size() +
runtimeModule->getBytecode()->getCJSModuleTableStatic().size();
assert(
firstSegmentModules <= std::numeric_limits<uint32_t>::max() &&
"number of modules is 32 bits due to the bytecode format");
// Use uint64_t to allow us to check for overflow.
const uint64_t requiredSize = firstSegmentModules * CJSModuleSize;
if (requiredSize > std::numeric_limits<uint32_t>::max()) {
return runtime->raiseRangeError("Loaded module count exceeded limit");
}
auto cjsModulesRes = ArrayStorage::create(runtime, requiredSize);
if (LLVM_UNLIKELY(cjsModulesRes == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
cjsModules = vmcast<ArrayStorage>(*cjsModulesRes);
auto requireFn = NativeFunction::create(
runtime,
Handle<JSObject>::vmcast(&runtime->functionPrototype),
(void *)TypeErrorKind::InvalidDynamicRequire,
throwTypeError,
Predefined::getSymbolID(Predefined::emptyString),
0,
Runtime::makeNullHandle<JSObject>());
auto context = RequireContext::create(
runtime,
self,
runtime->getPredefinedStringHandle(Predefined::emptyString));
// Set the require.context property.
PropertyFlags pf = PropertyFlags::defaultNewNamedPropertyFlags();
pf.writable = 0;
pf.configurable = 0;
if (LLVM_UNLIKELY(
JSObject::defineNewOwnProperty(
requireFn,
runtime,
Predefined::getSymbolID(Predefined::context),
pf,
context) == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
self->throwingRequire_.set(runtime, *requireFn, &runtime->getHeap());
} else {
cjsModules = self->cjsModules_.get(runtime);
}
assert(cjsModules && "cjsModules not set");
// Find the maximum module ID so we can resize cjsModules at most once per
// RuntimeModule.
uint64_t maxModuleID = cjsModules->size() / CJSModuleSize;
// The non-static module table does not store module IDs. They are assigned
// during registration by counting insertions to cjsModuleTable_. Count the
// insertions up front.
for (const auto &pair : runtimeModule->getBytecode()->getCJSModuleTable()) {
SymbolID symbolId =
runtimeModule->getSymbolIDFromStringIDMayAllocate(pair.first);
if (self->cjsModuleTable_.find(symbolId) == self->cjsModuleTable_.end()) {
++maxModuleID;
}
}
// The static module table stores module IDs in an arbitrary order. Scan for
// the maximum ID.
for (const auto &pair :
runtimeModule->getBytecode()->getCJSModuleTableStatic()) {
const auto &moduleID = pair.first;
if (moduleID > maxModuleID) {
maxModuleID = moduleID;
}
}
assert(
maxModuleID <= std::numeric_limits<uint32_t>::max() &&
"number of modules is 32 bits due to the bytecode format");
// Use uint64_t to allow us to check for overflow.
const uint64_t requiredSize = (maxModuleID + 1) * CJSModuleSize;
if (requiredSize > std::numeric_limits<uint32_t>::max()) {
return runtime->raiseRangeError("Loaded module count exceeded limit");
}
// Resize the array to allow for the new modules, if necessary.
if (requiredSize > cjsModules->size()) {
if (LLVM_UNLIKELY(
ArrayStorage::resize(cjsModules, runtime, requiredSize) ==
ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
}
/// \return Whether the module with ID \param moduleID has been registered in
/// cjsModules. \pre Space has been allocated for this module's record in
/// cjsModules.
const auto isModuleRegistered = [&cjsModules,
maxModuleID](uint32_t moduleID) -> bool {
assert(
moduleID <= maxModuleID &&
"CJS module ID exceeds maximum known module ID");
(void)maxModuleID;
uint32_t index = moduleID * CJSModuleSize;
uint32_t requiredSize = index + CJSModuleSize;
assert(
cjsModules->size() >= requiredSize &&
"CJS module ID exceeds allocated storage");
(void)requiredSize;
return !cjsModules->at(index + FunctionIndexOffset).isEmpty();
};
/// Register CJS module \param moduleID in the runtime module table.
/// \return The index into cjsModules where this module's record begins.
/// \pre Space has been allocated for this module's record in cjsModules.
/// \pre There is no module already registered under moduleID.
const auto registerModule =
[runtime, &cjsModules, runtimeModule, &isModuleRegistered](
uint32_t moduleID, uint32_t functionID) -> uint32_t {
assert(!isModuleRegistered(moduleID) && "CJS module ID collision occurred");
(void)isModuleRegistered;
uint32_t index = moduleID * CJSModuleSize;
cjsModules->at(index + CachedExportsOffset)
.set(HermesValue::encodeEmptyValue(), &runtime->getHeap());
cjsModules->at(index + ModuleOffset)
.set(HermesValue::encodeObjectValue(nullptr), &runtime->getHeap());
cjsModules->at(index + FunctionIndexOffset)
.set(HermesValue::encodeNativeUInt32(functionID), &runtime->getHeap());
cjsModules->at(index + runtimeModuleOffset)
.set(
HermesValue::encodeNativePointer(runtimeModule),
&runtime->getHeap());
assert(isModuleRegistered(moduleID) && "CJS module was not registered");
return index;
};
// Import full table that allows dynamic requires.
for (const auto &pair : runtimeModule->getBytecode()->getCJSModuleTable()) {
SymbolID symbolId =
runtimeModule->getSymbolIDFromStringIDMayAllocate(pair.first);
auto emplaceRes = self->cjsModuleTable_.try_emplace(symbolId, 0xffffffff);
if (emplaceRes.second) {
// This module has not been registered before.
// Assign it an arbitrary unused module ID, because nothing will be
// referencing that ID from outside Domain.
// Counting insertions to cjsModuleTable_ is a valid source of unique IDs
// since a given Domain uses either dynamic requires or statically
// resolved requires.
uint32_t moduleID = self->cjsModuleTable_.size() - 1;
const auto functionID = pair.second;
auto index = registerModule(moduleID, functionID);
// Update the mapping from symbolId to an index into cjsModules.
emplaceRes.first->second = index;
}
}
// Import table to be used for requireFast.
for (const auto &pair :
runtimeModule->getBytecode()->getCJSModuleTableStatic()) {
const auto &moduleID = pair.first;
const auto &functionID = pair.second;
if (!isModuleRegistered(moduleID)) {
registerModule(moduleID, functionID);
}
}
self->cjsModules_.set(runtime, cjsModules.get(), &runtime->getHeap());
return ExecutionStatus::RETURNED;
}
ObjectVTable RequireContext::vt{
VTable(CellKind::RequireContextKind, cellSize<RequireContext>()),
RequireContext::_getOwnIndexedRangeImpl,
RequireContext::_haveOwnIndexedImpl,
RequireContext::_getOwnIndexedPropertyFlagsImpl,
RequireContext::_getOwnIndexedImpl,
RequireContext::_setOwnIndexedImpl,
RequireContext::_deleteOwnIndexedImpl,
RequireContext::_checkAllOwnIndexedImpl,
};
void RequireContextBuildMeta(const GCCell *cell, Metadata::Builder &mb) {
mb.addJSObjectOverlapSlots(JSObject::numOverlapSlots<RequireContext>());
ObjectBuildMeta(cell, mb);
}
#ifdef HERMESVM_SERIALIZE
RequireContext::RequireContext(Deserializer &d) : JSObject(d, &vt.base) {}
void RequireContextSerialize(Serializer &s, const GCCell *cell) {
JSObject::serializeObjectImpl(
s, cell, JSObject::numOverlapSlots<RequireContext>());
s.endObject(cell);
}
void RequireContextDeserialize(Deserializer &d, CellKind kind) {
assert(kind == CellKind::RequireContextKind && "Expected RequireContext");
void *mem = d.getRuntime()->alloc</*fixedSize*/ true, HasFinalizer::No>(
cellSize<RequireContext>());
auto *cell = new (mem) RequireContext(d);
d.endObject(cell);
}
#endif
Handle<RequireContext> RequireContext::create(
Runtime *runtime,
Handle<Domain> domain,
Handle<StringPrimitive> dirname) {
JSObjectAlloc<RequireContext> mem{runtime};
auto self = mem.initToHandle(new (mem) RequireContext(
runtime,
vmcast<JSObject>(runtime->objectPrototype),
runtime->getHiddenClassForPrototypeRaw(
vmcast<JSObject>(runtime->objectPrototype),
ANONYMOUS_PROPERTY_SLOTS)));
JSObject::setInternalProperty(
*self, runtime, domainPropIndex(), domain.getHermesValue());
JSObject::setInternalProperty(
*self, runtime, dirnamePropIndex(), dirname.getHermesValue());
return self;
}
} // namespace vm
} // namespace hermes