forked from microsoft/hermes-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuntimeModule.cpp
503 lines (444 loc) · 17.2 KB
/
RuntimeModule.cpp
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
* 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/RuntimeModule.h"
#include "hermes/BCGen/HBC/BytecodeProviderFromSrc.h"
#include "hermes/Support/PerfSection.h"
#include "hermes/VM/CodeBlock.h"
#include "hermes/VM/Domain.h"
#include "hermes/VM/HiddenClass.h"
#include "hermes/VM/Predefined.h"
#include "hermes/VM/Runtime.h"
#include "hermes/VM/RuntimeModule-inline.h"
#include "hermes/VM/StringPrimitive.h"
namespace hermes {
namespace vm {
RuntimeModule::RuntimeModule(
Runtime *runtime,
Handle<Domain> domain,
RuntimeModuleFlags flags,
llvh::StringRef sourceURL,
facebook::hermes::debugger::ScriptID scriptID)
: runtime_(runtime),
domain_(&runtime->getHeap(), domain),
flags_(flags),
sourceURL_(sourceURL),
scriptID_(scriptID) {
runtime_->addRuntimeModule(this);
Domain::addRuntimeModule(domain, runtime, this);
#ifndef HERMESVM_LEAN
lazyRoot_ = this;
#endif
}
SymbolID RuntimeModule::createSymbolFromStringIDMayAllocate(
StringID stringID,
const StringTableEntry &entry,
OptValue<uint32_t> mhash) {
// Use manual pointer arithmetic to avoid out of bounds errors on empty
// string accesses.
auto strStorage = bcProvider_->getStringStorage();
if (entry.isUTF16()) {
const char16_t *s =
(const char16_t *)(strStorage.begin() + entry.getOffset());
UTF16Ref str{s, entry.getLength()};
uint32_t hash = mhash ? *mhash : hashString(str);
return mapStringMayAllocate(str, stringID, hash);
} else {
// ASCII.
const char *s = (const char *)strStorage.begin() + entry.getOffset();
ASCIIRef str{s, entry.getLength()};
uint32_t hash = mhash ? *mhash : hashString(str);
return mapStringMayAllocate(str, stringID, hash);
}
}
RuntimeModule::~RuntimeModule() {
if (bcProvider_ && !bcProvider_->getRawBuffer().empty())
runtime_->getCrashManager().unregisterMemory(bcProvider_.get());
runtime_->getCrashManager().unregisterMemory(this);
runtime_->removeRuntimeModule(this);
// We may reference other CodeBlocks through lazy compilation, but we only
// own the ones that reference us.
for (auto *block : functionMap_) {
if (block != nullptr && block->getRuntimeModule() == this) {
runtime_->getHeap().getIDTracker().untrackNative(block);
delete block;
}
}
runtime_->getHeap().getIDTracker().untrackNative(&functionMap_);
}
void RuntimeModule::prepareForRuntimeShutdown() {
for (int i = 0, e = functionMap_.size(); i < e; i++) {
if (functionMap_[i] != nullptr &&
functionMap_[i]->getRuntimeModule() != this) {
functionMap_[i] = nullptr;
}
}
}
CallResult<RuntimeModule *> RuntimeModule::create(
Runtime *runtime,
Handle<Domain> domain,
facebook::hermes::debugger::ScriptID scriptID,
std::shared_ptr<hbc::BCProvider> &&bytecode,
RuntimeModuleFlags flags,
llvh::StringRef sourceURL) {
RuntimeModule *result;
{
WeakRefLock lk{runtime->getHeap().weakRefMutex()};
result = new RuntimeModule(runtime, domain, flags, sourceURL, scriptID);
}
runtime->getCrashManager().registerMemory(result, sizeof(*result));
if (bytecode) {
if (result->initializeMayAllocate(std::move(bytecode)) ==
ExecutionStatus::EXCEPTION) {
return ExecutionStatus::EXCEPTION;
}
// If the BC provider is backed by a buffer, register the BC provider struct
// (but not the buffer contents, since that might be too large).
if (result->bcProvider_ && !result->bcProvider_->getRawBuffer().empty())
runtime->getCrashManager().registerMemory(
result->bcProvider_.get(), sizeof(hbc::BCProviderFromBuffer));
}
return result;
}
RuntimeModule *RuntimeModule::createUninitialized(
Runtime *runtime,
Handle<Domain> domain,
RuntimeModuleFlags flags,
facebook::hermes::debugger::ScriptID scriptID) {
WeakRefLock lk{runtime->getHeap().weakRefMutex()};
return new RuntimeModule(runtime, domain, flags, "", scriptID);
}
void RuntimeModule::initializeWithoutCJSModulesMayAllocate(
std::shared_ptr<hbc::BCProvider> &&bytecode) {
assert(!bcProvider_ && "RuntimeModule already initialized");
bcProvider_ = std::move(bytecode);
importStringIDMapMayAllocate();
initializeFunctionMap();
}
ExecutionStatus RuntimeModule::initializeMayAllocate(
std::shared_ptr<hbc::BCProvider> &&bytecode) {
initializeWithoutCJSModulesMayAllocate(std::move(bytecode));
if (LLVM_UNLIKELY(importCJSModuleTable() == ExecutionStatus::EXCEPTION)) {
return ExecutionStatus::EXCEPTION;
}
return ExecutionStatus::RETURNED;
}
CodeBlock *RuntimeModule::getCodeBlockSlowPath(unsigned index) {
#ifndef HERMESVM_LEAN
if (bcProvider_->isFunctionLazy(index)) {
auto *lazyModule = RuntimeModule::createLazyModule(
runtime_, getDomain(runtime_), this, index);
functionMap_[index] = lazyModule->getOnlyLazyCodeBlock();
return functionMap_[index];
}
#endif
functionMap_[index] = CodeBlock::createCodeBlock(
this,
bcProvider_->getFunctionHeader(index),
bcProvider_->getBytecode(index),
index);
return functionMap_[index];
}
#ifndef HERMESVM_LEAN
RuntimeModule *RuntimeModule::createLazyModule(
Runtime *runtime,
Handle<Domain> domain,
RuntimeModule *parent,
uint32_t functionID) {
auto RM = createUninitialized(runtime, domain);
RM->lazyRoot_ = parent->lazyRoot_;
// Copy the lazy root's script ID for lazy modules.
RM->scriptID_ = RM->lazyRoot_->scriptID_;
// Set the bcProvider's BytecodeModule to point to the parent's.
assert(parent->isInitialized() && "Parent module must have been initialized");
auto *bcFunction = &((hbc::BCProviderFromSrc *)parent->getBytecode())
->getBytecodeModule()
->getFunction(functionID);
RM->bcProvider_ = hbc::BCProviderLazy::createBCProviderLazy(bcFunction);
// We don't know which function index this block will eventually represent,
// so just add it as 0 to ensure ownership. We'll move it later in
// `initializeLazy`.
RM->functionMap_.emplace_back(CodeBlock::createCodeBlock(
RM, RM->bcProvider_->getFunctionHeader(functionID), {}, functionID));
// The module doesn't have a string table until we've compiled the block,
// so just add the string name as 0 in the mean time for f.name to work via
// getLazyName(). Since it's in the stringIDMap_, it'll be correctly GC'd.
RM->stringIDMap_.emplace_back(parent->getSymbolIDFromStringIDMayAllocate(
bcFunction->getHeader().functionName));
return RM;
}
SymbolID RuntimeModule::getLazyName() {
assert(functionMap_.size() == 1 && "Not a lazy module?");
assert(stringIDMap_.size() == 1 && "Missing lazy function name symbol");
assert(this->stringIDMap_[0].isValid() && "Invalid function name symbol");
return this->stringIDMap_[0];
}
void RuntimeModule::initializeLazyMayAllocate(
std::unique_ptr<hbc::BCProvider> bytecode) {
// Clear the old data provider first.
bcProvider_ = nullptr;
// Initialize without CJS module table because this compilation is done
// separately, and the bytecode will not contain a module table.
initializeWithoutCJSModulesMayAllocate(std::move(bytecode));
// createLazyCodeBlock added a single codeblock as functionMap_[0]
assert(functionMap_[0] && "Missing first entry");
// We should move it to the index where it's supposed to be. This ensures a
// 1-1 relationship between codeblocks and bytecodefunctions, which the
// debugger relies on for setting step-out breakpoints in all functions.
if (bcProvider_->getGlobalFunctionIndex() == 0) {
// No move needed
return;
}
assert(
!functionMap_[bcProvider_->getGlobalFunctionIndex()] &&
"Entry point is already occupied");
functionMap_[bcProvider_->getGlobalFunctionIndex()] = functionMap_[0];
functionMap_[0] = nullptr;
}
#endif
void RuntimeModule::importStringIDMapMayAllocate() {
assert(bcProvider_ && "Uninitialized RuntimeModule");
PerfSection perf("Import String ID Map");
GCScope scope(runtime_);
auto strTableSize = bcProvider_->getStringCount();
stringIDMap_.clear();
// Populate the string ID map with empty identifiers.
stringIDMap_.resize(strTableSize, RootSymbolID(SymbolID::empty()));
if (runtime_->getVMExperimentFlags() &
experiments::MAdviseStringsSequential) {
bcProvider_->adviseStringTableSequential();
}
if (runtime_->getVMExperimentFlags() & experiments::MAdviseStringsWillNeed) {
bcProvider_->willNeedStringTable();
}
// Get the array of pre-computed hashes from identifiers in the bytecode
// to their runtime representation as SymbolIDs.
auto kinds = bcProvider_->getStringKinds();
auto hashes = bcProvider_->getIdentifierHashes();
assert(
hashes.size() <= strTableSize &&
"Should not have more strings than identifiers");
// Preallocate enough space to store all identifiers to prevent
// unnecessary allocations. NOTE: If this module is not the first module,
// then this is an underestimate.
runtime_->getIdentifierTable().reserve(hashes.size());
{
StringID strID = 0;
uint32_t hashID = 0;
for (auto entry : kinds) {
switch (entry.kind()) {
case StringKind::String:
strID += entry.count();
break;
case StringKind::Identifier:
for (uint32_t i = 0; i < entry.count(); ++i, ++strID, ++hashID) {
createSymbolFromStringIDMayAllocate(
strID, bcProvider_->getStringTableEntry(strID), hashes[hashID]);
}
break;
}
}
assert(strID == strTableSize && "Should map every string in the bytecode.");
assert(hashID == hashes.size() && "Should hash all identifiers.");
}
if (runtime_->getVMExperimentFlags() & experiments::MAdviseStringsRandom) {
bcProvider_->adviseStringTableRandom();
}
if (strTableSize == 0) {
// If the string table turns out to be empty,
// we always add one empty string to it.
// Note that this can only happen when we are creating the RuntimeModule
// in a non-standard way, either in unit tests or the special
// emptyCodeBlockRuntimeModule_ in Runtime where the creation happens
// manually instead of going through bytecode module generation.
// In those cases, functions will be created with a default nameID=0
// without adding the name string into the string table. Hence here
// we need to add it manually and it will have index 0.
ASCIIRef s;
stringIDMap_.push_back({});
mapStringMayAllocate(s, 0, hashString(s));
}
}
void RuntimeModule::initializeFunctionMap() {
assert(bcProvider_ && "Uninitialized RuntimeModule");
assert(
bcProvider_->getFunctionCount() >= functionMap_.size() &&
"Unexpected size reduction. Lazy module missing functions?");
functionMap_.resize(bcProvider_->getFunctionCount());
}
ExecutionStatus RuntimeModule::importCJSModuleTable() {
PerfSection perf("Import CJS Module Table");
return Domain::importCJSModuleTable(getDomain(runtime_), runtime_, this);
}
StringPrimitive *RuntimeModule::getStringPrimFromStringIDMayAllocate(
StringID stringID) {
return runtime_->getStringPrimFromSymbolID(
getSymbolIDFromStringIDMayAllocate(stringID));
}
std::string RuntimeModule::getStringFromStringID(StringID stringID) {
auto entry = bcProvider_->getStringTableEntry(stringID);
auto strStorage = bcProvider_->getStringStorage();
if (entry.isUTF16()) {
const char16_t *s =
(const char16_t *)(strStorage.begin() + entry.getOffset());
std::string out;
convertUTF16ToUTF8WithReplacements(out, UTF16Ref{s, entry.getLength()});
return out;
} else {
// ASCII.
const char *s = (const char *)strStorage.begin() + entry.getOffset();
return std::string{s, entry.getLength()};
}
}
llvh::ArrayRef<uint8_t> RuntimeModule::getRegExpBytecodeFromRegExpID(
uint32_t regExpId) const {
assert(
regExpId < bcProvider_->getRegExpTable().size() && "Invalid regexp id");
RegExpTableEntry entry = bcProvider_->getRegExpTable()[regExpId];
return bcProvider_->getRegExpStorage().slice(entry.offset, entry.length);
}
template <typename T>
SymbolID RuntimeModule::mapStringMayAllocate(
llvh::ArrayRef<T> str,
StringID stringID,
uint32_t hash) {
// Create a SymbolID for a given string. In general a SymbolID holds onto an
// intern'd StringPrimitive. As an optimization, if this RuntimeModule is
// persistent, then it will not be deallocated before the Runtime, and we can
// have the SymbolID hold a raw pointer into the storage and produce the
// StringPrimitive when it is first required.
SymbolID id;
if (flags_.persistent) {
// Registering a lazy identifier does not allocate, so we do not need a
// GC scope.
id = runtime_->getIdentifierTable().registerLazyIdentifier(str, hash);
} else {
// Accessing a symbol non-lazily may allocate in the GC heap, so add a scope
// marker.
GCScopeMarkerRAII scopeMarker{runtime_};
id = *runtime_->ignoreAllocationFailure(
runtime_->getIdentifierTable().getSymbolHandle(runtime_, str, hash));
}
stringIDMap_[stringID] = RootSymbolID(id);
return id;
}
void RuntimeModule::markRoots(RootAcceptor &acceptor, bool markLongLived) {
for (auto &it : templateMap_) {
acceptor.acceptPtr(it.second);
}
if (markLongLived) {
for (auto symbol : stringIDMap_) {
if (symbol.isValid()) {
acceptor.accept(symbol);
}
}
}
}
void RuntimeModule::markWeakRoots(WeakRootAcceptor &acceptor) {
for (auto &cbPtr : functionMap_) {
// Only mark a CodeBlock is its non-null, and has not been scanned
// previously in this top-level markRoots invocation.
if (cbPtr != nullptr && cbPtr->getRuntimeModule() == this) {
cbPtr->markCachedHiddenClasses(runtime_, acceptor);
}
}
for (auto &entry : objectLiteralHiddenClasses_) {
if (entry.second) {
acceptor.acceptWeak(entry.second);
}
}
}
void RuntimeModule::markDomainRef(WeakRefAcceptor &acceptor) {
acceptor.accept(domain_);
}
llvh::Optional<Handle<HiddenClass>> RuntimeModule::findCachedLiteralHiddenClass(
Runtime *runtime,
unsigned keyBufferIndex,
unsigned numLiterals) const {
if (canGenerateLiteralHiddenClassCacheKey(keyBufferIndex, numLiterals)) {
const auto cachedHiddenClassIter = objectLiteralHiddenClasses_.find(
getLiteralHiddenClassCacheHashKey(keyBufferIndex, numLiterals));
if (cachedHiddenClassIter != objectLiteralHiddenClasses_.end()) {
if (HiddenClass *const cachedHiddenClass =
cachedHiddenClassIter->second.get(runtime, &runtime->getHeap())) {
return runtime_->makeHandle(cachedHiddenClass);
}
}
}
return llvh::None;
}
void RuntimeModule::tryCacheLiteralHiddenClass(
Runtime *runtime,
unsigned keyBufferIndex,
HiddenClass *clazz) {
auto numLiterals = clazz->getNumProperties();
if (canGenerateLiteralHiddenClassCacheKey(keyBufferIndex, numLiterals)) {
assert(
!findCachedLiteralHiddenClass(runtime, keyBufferIndex, numLiterals)
.hasValue() &&
"Why are we caching an item already cached?");
objectLiteralHiddenClasses_[getLiteralHiddenClassCacheHashKey(
keyBufferIndex, numLiterals)]
.set(runtime, clazz);
}
}
size_t RuntimeModule::additionalMemorySize() const {
return stringIDMap_.capacity() * sizeof(SymbolID) +
objectLiteralHiddenClasses_.getMemorySize() +
templateMap_.getMemorySize();
}
void RuntimeModule::snapshotAddNodes(GC *gc, HeapSnapshot &snap) const {
// Create a native node for each CodeBlock owned by this module.
for (const CodeBlock *cb : functionMap_) {
// Skip the null code blocks, they are lazily inserted the first time
// they are used.
if (cb && cb->getRuntimeModule() == this) {
// Only add a CodeBlock if this runtime module is the owner.
snap.beginNode();
snap.endNode(
HeapSnapshot::NodeType::Native,
"CodeBlock",
gc->getNativeID(cb),
sizeof(CodeBlock) + cb->additionalMemorySize(),
0);
}
}
// Create a node for functionMap_.
snap.beginNode();
// Create an edge to each CodeBlock owned by this module.
for (int i = 0, e = functionMap_.size(); i < e; i++) {
const CodeBlock *cb = functionMap_[i];
// Skip the null code blocks, they are lazily inserted the first time
// they are used.
if (cb && cb->getRuntimeModule() == this) {
// Only add a CodeBlock if this runtime module is the owner.
snap.addIndexedEdge(
HeapSnapshot::EdgeType::Element, i, gc->getNativeID(cb));
}
}
snap.endNode(
HeapSnapshot::NodeType::Native,
"std::vector<CodeBlock *>",
gc->getNativeID(&functionMap_),
functionMap_.capacity() * sizeof(CodeBlock *),
0);
}
void RuntimeModule::snapshotAddEdges(GC *gc, HeapSnapshot &snap) const {
snap.addNamedEdge(
HeapSnapshot::EdgeType::Internal,
"functionMap",
gc->getNativeID(&functionMap_));
}
namespace detail {
StringID mapStringMayAllocate(RuntimeModule &module, const char *str) {
module.stringIDMap_.push_back({});
module.mapStringMayAllocate(
createASCIIRef(str), module.stringIDMap_.size() - 1);
return module.stringIDMap_.size() - 1;
}
} // namespace detail
} // namespace vm
} // namespace hermes