forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceInterpreter.cpp
More file actions
1643 lines (1571 loc) · 61.6 KB
/
TraceInterpreter.cpp
File metadata and controls
1643 lines (1571 loc) · 61.6 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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*/
#ifdef HERMESVM_API_TRACE
#include <hermes/TraceInterpreter.h>
#include <hermes/SynthTraceParser.h>
#include <hermes/TracingRuntime.h>
#include <hermes/VM/instrumentation/PerfEvents.h>
#include <jsi/instrumentation.h>
#include <llvh/Support/SHA1.h>
#include <llvh/Support/SaveAndRestore.h>
#include <algorithm>
#include <unordered_set>
using namespace hermes::parser;
using namespace facebook::jsi;
namespace facebook {
namespace hermes {
namespace tracing {
using RecordType = SynthTrace::RecordType;
using ObjectID = SynthTrace::ObjectID;
namespace {
constexpr ObjectID setupFuncID = std::numeric_limits<ObjectID>::max();
/// Get the map from host functions to calls, and host objects to calls, for
/// the given list of records.
/// \param setupFuncID The function id to use for the top-level function. This
/// should not collide with any possible existing function id.
/// \param records A list of records inside a trace.
std::pair<
TraceInterpreter::HostFunctionToCalls,
TraceInterpreter::HostObjectToCalls>
getCalls(
ObjectID setupFuncID,
const std::vector<std::unique_ptr<SynthTrace::Record>> &records) {
// This function iterates through the list of records, maintaining a stack of
// function calls that mimics the same sequence that was taken during trace
// collection. It attributes each contiguous sequence of records that are part
// of the same function invocation into a Piece, which it then puts into the
// call.
// NOTE: StackValue should be a std::variant when it's available. In the
// meantime, use inheritance to keep it typesafe, in particular to destruct
// the std::string correctly.
struct StackValue {
virtual ~StackValue() {}
};
struct HostFunction final : public StackValue {
ObjectID hostFunctionID;
HostFunction(ObjectID hostFunctionID)
: StackValue(), hostFunctionID(hostFunctionID) {}
};
struct HostObject final : public StackValue {
ObjectID objID;
std::string propName;
HostObject(ObjectID hostObjID, const std::string &propName)
: StackValue(), objID(hostObjID), propName(propName) {}
};
struct HostObjectPropNames final : public StackValue {
ObjectID objID;
HostObjectPropNames(ObjectID hostObjID) : StackValue(), objID(hostObjID) {}
};
TraceInterpreter::HostFunctionToCalls funcIDToRecords;
// A mapping from a host object id to a map from property name to
// list of records
TraceInterpreter::HostObjectToCalls hostObjIDToNameToRecords;
// As CallRecords are encountered, the id of the object or function being
// called is placed at the end of this stack, so that when the matching return
// is executed, records can be attributed to the previous call.
std::vector<std::unique_ptr<StackValue>> stack;
// Make the setup function. It is the bottom of the stack, and also a host
// function.
stack.emplace_back(new HostFunction(setupFuncID));
funcIDToRecords[setupFuncID].emplace_back(
TraceInterpreter::Call(TraceInterpreter::Call::Piece()));
// Get a list of calls of the currently executing function from the stack.
const auto getCallsFromStack =
[&funcIDToRecords, &hostObjIDToNameToRecords](
const StackValue &stackObj) -> std::vector<TraceInterpreter::Call> & {
if (const auto *hostFunc = dynamic_cast<const HostFunction *>(&stackObj)) {
// A function is just the function id.
return funcIDToRecords[hostFunc->hostFunctionID];
} else if (
const auto *hostObj = dynamic_cast<const HostObject *>(&stackObj)) {
// Host object is a combination of the object id and property
// name.
return hostObjIDToNameToRecords[hostObj->objID]
.propNameToCalls[hostObj->propName];
} else if (
const auto *hostObj =
dynamic_cast<const HostObjectPropNames *>(&stackObj)) {
return hostObjIDToNameToRecords[hostObj->objID].callsToGetPropertyNames;
} else {
llvm_unreachable("Shouldn't be any other subclasses of StackValue");
}
};
for (uint64_t recordNum = 0; recordNum < records.size(); ++recordNum) {
const auto &rec = records[recordNum];
if (rec->getType() == RecordType::CreateHostFunction) {
const auto &createHFRec =
static_cast<const SynthTrace::CreateHostFunctionRecord &>(*rec);
assert(
funcIDToRecords.find(createHFRec.objID_) == funcIDToRecords.end() &&
"The host function was already initialized, it was called before it was created");
// Insert a call so that this function is in the map, even if nothing
// calls it.
funcIDToRecords.emplace(
createHFRec.objID_, std::vector<TraceInterpreter::Call>());
} else if (rec->getType() == RecordType::CreateHostObject) {
const auto &createHORec =
static_cast<const SynthTrace::CreateHostObjectRecord &>(*rec);
assert(
hostObjIDToNameToRecords.find(createHORec.objID_) ==
hostObjIDToNameToRecords.end() &&
"The host object was already initialized, it was used before it was created");
// Insert an entry so that this host object is in the map, even if nothing
// uses it.
hostObjIDToNameToRecords.emplace(
createHORec.objID_, TraceInterpreter::HostObjectInfo{});
} else if (rec->getType() == RecordType::CallToNative) {
// JS will cause a call to native code. Add a new call on the stack, and
// add a call with an empty piece to the function it is calling.
const auto &callRec =
static_cast<const SynthTrace::CallToNativeRecord &>(*rec);
assert(
callRec.functionID_ != setupFuncID &&
"Should never encounter a call into the setup function");
stack.emplace_back(new HostFunction(callRec.functionID_));
funcIDToRecords[callRec.functionID_].emplace_back(
TraceInterpreter::Call(TraceInterpreter::Call::Piece(recordNum)));
} else if (
rec->getType() == RecordType::GetPropertyNative ||
rec->getType() == RecordType::SetPropertyNative) {
const auto &nativeAccessRec =
static_cast<const SynthTrace::GetOrSetPropertyNativeRecord &>(*rec);
// JS will access a property on a host object, which delegates to an
// accessor. Add a new call on the stack, and add a call with an empty
// piece to the object and property it is calling.
stack.emplace_back(new HostObject(
nativeAccessRec.hostObjectID_, nativeAccessRec.propName_));
hostObjIDToNameToRecords[nativeAccessRec.hostObjectID_]
.propNameToCalls[nativeAccessRec.propName_]
.emplace_back(
TraceInterpreter::Call(TraceInterpreter::Call::Piece(recordNum)));
} else if (rec->getType() == RecordType::GetNativePropertyNames) {
const auto &nativePropNamesRec =
static_cast<const SynthTrace::GetNativePropertyNamesRecord &>(*rec);
// JS asked for all properties on a host object. Add a new call on the
// stack, and add a call with an empty piece to the object it is calling.
stack.emplace_back(
new HostObjectPropNames(nativePropNamesRec.hostObjectID_));
hostObjIDToNameToRecords[nativePropNamesRec.hostObjectID_]
.callsToGetPropertyNames.emplace_back(
TraceInterpreter::Call(TraceInterpreter::Call::Piece(recordNum)));
} else if (rec->getType() == RecordType::GetNativePropertyNamesReturn) {
// Set the vector of strings that were returned from the original call.
const auto &nativePropNamesRec =
static_cast<const SynthTrace::GetNativePropertyNamesReturnRecord &>(
*rec);
// The stack object must be a HostObjectPropNames in order for this to be
// a return from there.
hostObjIDToNameToRecords
[dynamic_cast<const HostObjectPropNames &>(*stack.back()).objID]
.resultsOfGetPropertyNames.emplace_back(
nativePropNamesRec.propNames_);
}
auto &calls = getCallsFromStack(*stack.back());
assert(!calls.empty() && "There should always be at least one call");
auto &call = calls.back();
if (rec->getType() == RecordType::ReturnToNative) {
// Add a new piece to the current call because we're about to
// re-enter native.
call.pieces.emplace_back(TraceInterpreter::Call::Piece(recordNum));
}
auto &piece = call.pieces.back();
piece.records.emplace_back(&*rec);
if (rec->getType() == RecordType::GetPropertyNativeReturn ||
rec->getType() == RecordType::SetPropertyNativeReturn ||
rec->getType() == RecordType::GetNativePropertyNamesReturn ||
rec->getType() == RecordType::ReturnFromNative) {
stack.pop_back();
}
}
assert(stack.size() == 1 && "Stack was not fully exhausted");
return std::make_pair(funcIDToRecords, hostObjIDToNameToRecords);
}
/// Given a call, computes each local object's last def before first use, and
/// last use.
/// Stores this information into \code call->locals.
void createCallMetadata(TraceInterpreter::Call *call) {
// start is unused, since there's no need to convert to global indices
for (auto &piece : call->pieces) {
auto recordNum = piece.start;
for (auto *rec : piece.records) {
// For records that define and use the same object, this order is
// important, it might def an object that it uses, or use an object
// that it defs.
// For now, there's only one case where this happens, in
// GetPropertyRecord, which is the case of def'ing an object that it
// uses. So the uses need to come before defs.
// A general solution would require each record saying which order the
// two should be run in, or have a conflict resolver inside itself.
for (ObjectID use : rec->uses()) {
auto &loc = call->locals[use];
// Locally defined object access, move the last use forward.
loc.lastUse = recordNum;
}
for (ObjectID def : rec->defs()) {
auto &loc = call->locals[def];
if (loc.lastUse == TraceInterpreter::DefAndUse::kUnused) {
// Nothing local has used this object yet, we can eliminate any
// previous defs.
loc.lastDefBeforeFirstUse = recordNum;
}
}
recordNum++;
}
}
// TODO (T31512967): Prune unused defs.
}
std::unordered_map<ObjectID, TraceInterpreter::DefAndUse> createGlobalMap(
const TraceInterpreter::HostFunctionToCalls &funcCallStacks,
const TraceInterpreter::HostObjectToCalls &objCallStacks,
ObjectID globalObjID) {
struct Glob {
uint64_t firstUse{TraceInterpreter::DefAndUse::kUnused};
uint64_t lastUse{TraceInterpreter::DefAndUse::kUnused};
};
// For Objects, Strings, and PropNameIDs.
std::unordered_map<ObjectID, Glob> firstAndLastGlobalUses;
// For Objects, Strings, and PropNameIDs.
std::unordered_map<ObjectID, std::unordered_set<uint64_t>> defsPerObj;
defsPerObj[globalObjID].insert(0);
firstAndLastGlobalUses[globalObjID].firstUse = 0;
firstAndLastGlobalUses[globalObjID].lastUse = std::numeric_limits<int>::max();
std::vector<const TraceInterpreter::Call *> calls;
for (const auto &p : funcCallStacks) {
const std::vector<TraceInterpreter::Call> &funcCalls = p.second;
for (const auto &call : funcCalls) {
calls.push_back(&call);
}
}
for (const auto &p : objCallStacks) {
for (const auto &x : p.second.propNameToCalls) {
for (const auto &call : x.second) {
calls.push_back(&call);
}
}
for (const auto &call : p.second.callsToGetPropertyNames) {
calls.push_back(&call);
}
}
for (const auto *call : calls) {
for (const auto &piece : call->pieces) {
auto globalRecordNum = piece.start;
for (const auto *rec : piece.records) {
for (ObjectID def : rec->defs()) {
// Add to the set of defs.
defsPerObj[def].emplace(globalRecordNum);
}
for (ObjectID use : rec->uses()) {
// Update the last use only if it can't be satisfied by a local def.
const auto &loc = call->locals.at(use);
if (loc.lastDefBeforeFirstUse ==
TraceInterpreter::DefAndUse::kUnused) {
// It was a global access
auto &glob = firstAndLastGlobalUses[use];
if (globalRecordNum < glob.firstUse ||
glob.firstUse == TraceInterpreter::DefAndUse::kUnused) {
// Earlier global use or first global use, update.
glob.firstUse = globalRecordNum;
}
if (globalRecordNum > glob.lastUse ||
glob.lastUse == TraceInterpreter::DefAndUse::kUnused) {
// Later global use, update.
glob.lastUse = globalRecordNum;
}
}
}
globalRecordNum++;
};
};
}
// For each object, find the max def that is before the first use.
std::unordered_map<ObjectID, TraceInterpreter::DefAndUse> globalDefsAndUses;
for (auto &p : firstAndLastGlobalUses) {
const auto objID = p.first;
const auto firstUse = p.second.firstUse;
const auto lastUse = p.second.lastUse;
assert(
firstUse <= lastUse &&
"Should never have the first use be greater than the last use");
std::unordered_set<uint64_t> &defs = defsPerObj[objID];
assert(
defs.size() &&
"There must be at least one def for any globally used object");
// The defs here are global record numbers.
uint64_t lastDefBeforeFirstUse = *defs.begin();
for (uint64_t def : defs) {
if (def < firstUse) {
lastDefBeforeFirstUse = std::max(lastDefBeforeFirstUse, def);
}
}
assert(
lastDefBeforeFirstUse <= lastUse &&
"Should never have the last def before first use be greater than "
"the last use");
globalDefsAndUses[objID] =
TraceInterpreter::DefAndUse{lastDefBeforeFirstUse, lastUse};
}
return globalDefsAndUses;
}
/// Merge host function calls and host object calls into one set of calls, and
/// compute which values are local to each call.
void createCallMetadataForHostFunctions(
TraceInterpreter::HostFunctionToCalls *funcCallStacks) {
for (auto &p : *funcCallStacks) {
for (TraceInterpreter::Call &call : p.second) {
createCallMetadata(&call);
}
}
}
void createCallMetadataForHostObjects(
TraceInterpreter::HostObjectToCalls *objCallStacks) {
for (auto &p : *objCallStacks) {
for (auto &x : p.second.propNameToCalls) {
for (TraceInterpreter::Call &call : x.second) {
createCallMetadata(&call);
}
}
for (auto &call : p.second.callsToGetPropertyNames) {
createCallMetadata(&call);
}
}
}
Value traceValueToJSIValue(
Runtime &rt,
const SynthTrace &trace,
std::function<Value(ObjectID)> getJSIValueForUse,
SynthTrace::TraceValue value) {
if (value.isUndefined()) {
return Value::undefined();
}
if (value.isNull()) {
return Value::null();
}
if (value.isNumber()) {
return Value(value.getNumber());
}
if (value.isBool()) {
return Value(value.getBool());
}
if (value.isObject() || value.isString()) {
return getJSIValueForUse(value.getUID());
}
llvm_unreachable("Unrecognized value type encountered");
}
std::unique_ptr<const jsi::Buffer> bufConvert(
std::unique_ptr<llvh::MemoryBuffer> buf) {
// A jsi::Buffer adapter that owns a llvh::MemoryBuffer.
class OwnedMemoryBuffer : public jsi::Buffer {
public:
OwnedMemoryBuffer(std::unique_ptr<llvh::MemoryBuffer> buffer)
: data_(std::move(buffer)) {}
size_t size() const override {
return data_->getBufferSize();
}
const uint8_t *data() const override {
return reinterpret_cast<const uint8_t *>(data_->getBufferStart());
}
private:
std::unique_ptr<llvh::MemoryBuffer> data_;
};
return llvh::make_unique<const OwnedMemoryBuffer>(std::move(buf));
}
static bool isAllZeroSourceHash(const ::hermes::SHA1 sourceHash) {
for (auto byte : sourceHash) {
if (byte) {
return false;
}
}
return true;
}
static void verifyBundlesExist(
const std::map<::hermes::SHA1, std::shared_ptr<const jsi::Buffer>> &bundles,
const SynthTrace &trace) {
std::vector<::hermes::SHA1> missingSourceHashes;
for (const auto &rec : trace.records()) {
if (rec->getType() == SynthTrace::RecordType::BeginExecJS) {
const auto &bejsr =
dynamic_cast<const SynthTrace::BeginExecJSRecord &>(*rec);
if (bundles.count(bejsr.sourceHash()) == 0 &&
!isAllZeroSourceHash(bejsr.sourceHash())) {
missingSourceHashes.emplace_back(bejsr.sourceHash());
}
}
}
if (!missingSourceHashes.empty()) {
std::string msg = "Missing bundles with the following source hashes: ";
bool first = true;
for (const auto &hash : missingSourceHashes) {
if (!first) {
msg += ", ";
}
msg += ::hermes::hashAsString(hash);
first = false;
}
msg += "\nProvided source hashes: ";
if (bundles.empty()) {
msg += "(no sources provided)";
} else {
first = true;
for (const auto &p : bundles) {
if (!first) {
msg += ", ";
}
msg += ::hermes::hashAsString(p.first);
first = false;
}
}
throw std::invalid_argument(msg);
}
}
/// Returns the element of \p repGCStats with the median "totalTime" stat.
static std::string mergeGCStats(const std::vector<std::string> &repGCStats) {
if (repGCStats.empty())
throw std::invalid_argument("Empty GC stats.");
std::vector<std::pair<double, const std::string *>> valueAndStats;
for (auto &stats : repGCStats) {
// Find "totalTime" by string search, to avoid JSONParser boilerplate
// and issues with the "GC Stats" header.
std::string target = "\"totalTime\":";
auto pos = stats.find(target);
if (pos == std::string::npos)
throw std::invalid_argument("Malformed GC stats.");
double value = std::atof(&stats[pos + target.size()]);
valueAndStats.emplace_back(value, &stats);
}
unsigned median = (valueAndStats.size() - 1) / 2;
std::nth_element(
valueAndStats.begin(),
valueAndStats.begin() + median,
valueAndStats.end());
return *valueAndStats[median].second;
}
} // namespace
TraceInterpreter::TraceInterpreter(
jsi::Runtime &rt,
const ExecuteOptions &options,
const SynthTrace &trace,
std::map<::hermes::SHA1, std::shared_ptr<const Buffer>> bundles,
const std::unordered_map<ObjectID, TraceInterpreter::DefAndUse>
&globalDefsAndUses,
const HostFunctionToCalls &hostFunctionCalls,
const HostObjectToCalls &hostObjectCalls)
: rt_(rt),
options_(options),
bundles_(std::move(bundles)),
trace_(trace),
globalDefsAndUses_(globalDefsAndUses),
hostFunctionCalls_(hostFunctionCalls),
hostObjectCalls_(hostObjectCalls),
hostFunctions_(),
hostFunctionsCallCount_(),
hostObjects_(),
hostObjectsCallCount_(),
hostObjectsPropertyNamesCallCount_(),
gom_() {
// Add the global object to the global object map
gom_.emplace(trace.globalObjID(), rt.global());
}
/* static */
void TraceInterpreter::exec(
const std::string &traceFile,
const std::vector<std::string> &bytecodeFiles,
const ExecuteOptions &options) {
// If there is a trace, don't write it out, not used here.
execFromFileNames(traceFile, bytecodeFiles, options, nullptr);
}
/* static */
std::string TraceInterpreter::execAndGetStats(
const std::string &traceFile,
const std::vector<std::string> &bytecodeFiles,
const ExecuteOptions &options) {
// If there is a trace, don't write it out, not used here.
return execFromFileNames(traceFile, bytecodeFiles, options, nullptr);
}
/* static */
void TraceInterpreter::execAndTrace(
const std::string &traceFile,
const std::vector<std::string> &bytecodeFiles,
const ExecuteOptions &options,
std::unique_ptr<llvh::raw_ostream> traceStream) {
assert(traceStream && "traceStream must be provided (precondition)");
execFromFileNames(traceFile, bytecodeFiles, options, std::move(traceStream));
}
/* static */
std::string TraceInterpreter::execFromFileNames(
const std::string &traceFile,
const std::vector<std::string> &bytecodeFiles,
const ExecuteOptions &options,
std::unique_ptr<llvh::raw_ostream> traceStream) {
auto errorOrFile = llvh::MemoryBuffer::getFile(traceFile);
if (!errorOrFile) {
throw std::system_error(errorOrFile.getError());
}
std::unique_ptr<llvh::MemoryBuffer> traceBuf = std::move(errorOrFile.get());
std::vector<std::unique_ptr<llvh::MemoryBuffer>> bytecodeBuffers;
for (const std::string &bytecode : bytecodeFiles) {
errorOrFile = llvh::MemoryBuffer::getFile(bytecode);
if (!errorOrFile) {
throw std::system_error(errorOrFile.getError());
}
bytecodeBuffers.emplace_back(std::move(errorOrFile.get()));
}
return execFromMemoryBuffer(
std::move(traceBuf),
std::move(bytecodeBuffers),
options,
std::move(traceStream));
}
/* static */
std::map<::hermes::SHA1, std::shared_ptr<const jsi::Buffer>>
TraceInterpreter::getSourceHashToBundleMap(
std::vector<std::unique_ptr<llvh::MemoryBuffer>> &&codeBufs,
const SynthTrace &trace,
bool *codeIsMmapped,
bool *isBytecode) {
if (codeIsMmapped) {
*codeIsMmapped = true;
for (const auto &buf : codeBufs) {
if (buf->getBufferKind() != llvh::MemoryBuffer::MemoryBuffer_MMap) {
// If any of the buffers aren't mmapped, don't turn on I/O tracking.
*codeIsMmapped = false;
break;
}
}
}
std::vector<std::unique_ptr<const jsi::Buffer>> bundles;
for (auto &buf : codeBufs) {
bundles.emplace_back(bufConvert(std::move(buf)));
}
if (isBytecode) {
*isBytecode = true;
for (const auto &bundle : bundles) {
if (!HermesRuntime::isHermesBytecode(bundle->data(), bundle->size())) {
// If any of the buffers are source code, don't turn on I/O tracking.
*isBytecode = false;
break;
}
}
}
// Map source hashes of files to their memory buffer.
std::map<::hermes::SHA1, std::shared_ptr<const jsi::Buffer>>
sourceHashToBundle;
for (auto &bundle : bundles) {
::hermes::SHA1 sourceHash{};
if (HermesRuntime::isHermesBytecode(bundle->data(), bundle->size())) {
sourceHash =
::hermes::hbc::BCProviderFromBuffer::getSourceHashFromBytecode(
llvh::makeArrayRef(bundle->data(), bundle->size()));
} else {
sourceHash =
llvh::SHA1::hash(llvh::makeArrayRef(bundle->data(), bundle->size()));
}
auto inserted = sourceHashToBundle.insert({sourceHash, std::move(bundle)});
assert(
inserted.second &&
"Duplicate source hash detected, files only need to be supplied once");
(void)inserted;
}
verifyBundlesExist(sourceHashToBundle, trace);
return sourceHashToBundle;
}
/* static */
::hermes::vm::RuntimeConfig TraceInterpreter::merge(
::hermes::vm::RuntimeConfig::Builder &rtConfigBuilderIn,
const ::hermes::vm::GCConfig::Builder &gcConfigBuilderIn,
const ExecuteOptions &options,
bool codeIsMmapped,
bool isBytecode) {
::hermes::vm::RuntimeConfig::Builder rtConfigBuilder;
::hermes::vm::GCConfig::Builder gcConfigBuilder;
if (options.useTraceConfig) {
rtConfigBuilder.update(rtConfigBuilderIn);
} else {
// Some portions of even the default config must agree with the trace
// config, because the contents of the trace assume a given shape for
// runtime data structures during replay. So far, we know of only one such
// parameter: EnableSampledStats.
rtConfigBuilder.withEnableSampledStats(
rtConfigBuilderIn.build().getEnableSampledStats());
}
if (options.bytecodeWarmupPercent) {
rtConfigBuilder.withBytecodeWarmupPercent(*options.bytecodeWarmupPercent);
}
if (options.shouldTrackIO) {
rtConfigBuilder.withTrackIO(
*options.shouldTrackIO && isBytecode && codeIsMmapped);
}
// If (and only if) an out trace is requested, turn on tracing in the VM
// as well.
rtConfigBuilder.withTraceEnabled(options.traceEnabled);
// If aggregating multiple reps, randomize the placement of some data
// structures in each rep, for a more robust time metric.
if (options.reps > 1) {
rtConfigBuilder.withRandomizeMemoryLayout(true);
}
gcConfigBuilder.update(gcConfigBuilderIn);
gcConfigBuilder.update(options.gcConfigBuilder);
return rtConfigBuilder.withGCConfig(gcConfigBuilder.build()).build();
}
/* static */
std::string TraceInterpreter::execFromMemoryBuffer(
std::unique_ptr<llvh::MemoryBuffer> &&traceBuf,
std::vector<std::unique_ptr<llvh::MemoryBuffer>> &&codeBufs,
const ExecuteOptions &options,
std::unique_ptr<llvh::raw_ostream> traceStream) {
auto traceAndConfigAndEnv = parseSynthTrace(std::move(traceBuf));
const auto &trace = std::get<0>(traceAndConfigAndEnv);
bool codeIsMmapped;
bool isBytecode;
std::map<::hermes::SHA1, std::shared_ptr<const jsi::Buffer>>
sourceHashToBundle = getSourceHashToBundleMap(
std::move(codeBufs), trace, &codeIsMmapped, &isBytecode);
options.traceEnabled = (traceStream != nullptr);
const auto &rtConfig = merge(
std::get<1>(traceAndConfigAndEnv),
std::get<2>(traceAndConfigAndEnv),
options,
codeIsMmapped,
isBytecode);
std::vector<std::string> repGCStats(options.reps);
for (int rep = -options.warmupReps; rep < options.reps; ++rep) {
::hermes::vm::instrumentation::PerfEvents::begin();
std::unique_ptr<jsi::Runtime> rt;
std::unique_ptr<HermesRuntime> hermesRuntime = makeHermesRuntime(rtConfig);
// Set up the mocks for environment-dependent JS behavior
{
auto env = std::get<3>(traceAndConfigAndEnv);
env.stabilizeInstructionCount = options.stabilizeInstructionCount;
hermesRuntime->setMockedEnvironment(env);
}
bool tracing = false;
if (traceStream) {
tracing = true;
rt = makeTracingHermesRuntime(
std::move(hermesRuntime),
rtConfig,
std::move(traceStream),
/* forReplay */ true);
} else {
rt = std::move(hermesRuntime);
}
auto stats = exec(*rt, options, trace, sourceHashToBundle);
// If we're not warming up, save the stats.
if (rep >= 0) {
repGCStats[rep] = stats;
}
// If we're tracing, flush the trace.
if (tracing) {
(void)rt->instrumentation().flushAndDisableBridgeTrafficTrace();
}
}
return rtConfig.getGCConfig().getShouldRecordStats()
? mergeGCStats(repGCStats)
: "";
}
/* static */
std::string TraceInterpreter::execFromMemoryBuffer(
std::unique_ptr<llvh::MemoryBuffer> &&traceBuf,
std::vector<std::unique_ptr<llvh::MemoryBuffer>> &&codeBufs,
jsi::Runtime &runtime,
const ExecuteOptions &options) {
auto traceAndConfigAndEnv = parseSynthTrace(std::move(traceBuf));
const auto &trace = std::get<0>(traceAndConfigAndEnv);
return exec(
runtime,
options,
trace,
getSourceHashToBundleMap(std::move(codeBufs), trace));
}
/* static */
std::string TraceInterpreter::exec(
jsi::Runtime &rt,
const ExecuteOptions &options,
const SynthTrace &trace,
std::map<::hermes::SHA1, std::shared_ptr<const jsi::Buffer>> bundles) {
// Partition the records into each call.
auto funcCallsAndObjectCalls = getCalls(setupFuncID, trace.records());
auto &hostFuncs = funcCallsAndObjectCalls.first;
auto &hostObjs = funcCallsAndObjectCalls.second;
// Write in the metadata.
createCallMetadataForHostFunctions(&hostFuncs);
createCallMetadataForHostObjects(&hostObjs);
assert(
hostFuncs.at(setupFuncID).size() == 1 &&
"The setup function should only have one call");
// Calculate the globals and locals information for the records to know
// which objects need to be put into a global map data structure.
const auto globalDefsAndUses =
createGlobalMap(hostFuncs, hostObjs, trace.globalObjID());
TraceInterpreter interpreter(
rt,
options,
trace,
std::move(bundles),
globalDefsAndUses,
hostFuncs,
hostObjs);
return interpreter.execEntryFunction(hostFuncs.at(setupFuncID).at(0));
}
Function TraceInterpreter::createHostFunction(
const SynthTrace::CreateHostFunctionRecord &rec,
const PropNameID &propNameID) {
const auto funcID = rec.objID_;
const std::vector<TraceInterpreter::Call> &calls =
hostFunctionCalls_.at(funcID);
#ifdef HERMESVM_API_TRACE_DEBUG
assert(propNameID.utf8(rt_) == rec.functionName_);
#endif
return Function::createFromHostFunction(
rt_,
propNameID,
rec.paramCount_,
[this, funcID, &calls](
Runtime &, const Value &thisVal, const Value *args, size_t count)
-> Value {
try {
return execFunction(
calls.at(hostFunctionsCallCount_[funcID]++),
thisVal,
args,
count);
} catch (const std::exception &e) {
crashOnException(e, llvh::None);
}
});
}
Object TraceInterpreter::createHostObject(ObjectID objID) {
struct FakeHostObject : public HostObject {
TraceInterpreter &interpreter;
const HostObjectInfo &hostObjectInfo;
std::unordered_map<std::string, uint64_t> &callCounts;
uint64_t &propertyNamesCallCounts;
FakeHostObject(
TraceInterpreter &interpreter,
const HostObjectInfo &hostObjectInfo,
std::unordered_map<std::string, uint64_t> &callCounts,
uint64_t &propertyNamesCallCounts)
: interpreter(interpreter),
hostObjectInfo(hostObjectInfo),
callCounts(callCounts),
propertyNamesCallCounts(propertyNamesCallCounts) {}
Value get(Runtime &rt, const PropNameID &name) override {
try {
const std::string propName = name.utf8(rt);
// There are no arguments to pass to a get call.
return interpreter.execFunction(
hostObjectInfo.propNameToCalls.at(propName).at(
callCounts[propName]++),
// This is undefined since there's no way to access the host object
// as a JS value normally from this position.
Value::undefined(),
nullptr,
0,
&name);
} catch (const std::exception &e) {
interpreter.crashOnException(e, llvh::None);
}
}
void set(Runtime &rt, const PropNameID &name, const Value &value) override {
try {
const std::string propName = name.utf8(rt);
const Value args[] = {Value(rt, value)};
// There is exactly one argument to pass to a set call.
interpreter.execFunction(
hostObjectInfo.propNameToCalls.at(propName).at(
callCounts[propName]++),
// This is undefined since there's no way to access the host object
// as a JS value normally from this position.
Value::undefined(),
args,
1);
} catch (const std::exception &e) {
interpreter.crashOnException(e, llvh::None);
}
}
std::vector<PropNameID> getPropertyNames(Runtime &rt) override {
try {
const auto callCount = propertyNamesCallCounts++;
interpreter.execFunction(
hostObjectInfo.callsToGetPropertyNames.at(callCount),
// This is undefined since there's no way to access the host object
// as a JS value normally from this position.
Value::undefined(),
nullptr,
0);
const std::vector<std::string> &names =
hostObjectInfo.resultsOfGetPropertyNames.at(callCount);
std::vector<PropNameID> props;
for (const std::string &name : names) {
props.emplace_back(PropNameID::forUtf8(rt, name));
}
return props;
} catch (const std::exception &e) {
interpreter.crashOnException(e, llvh::None);
}
}
};
return Object::createFromHostObject(
rt_,
std::make_shared<FakeHostObject>(
*this,
hostObjectCalls_.at(objID),
hostObjectsCallCount_[objID],
hostObjectsPropertyNamesCallCount_[objID]));
}
std::string TraceInterpreter::execEntryFunction(
const TraceInterpreter::Call &entryFunc) {
execFunction(entryFunc, Value::undefined(), nullptr, 0);
#ifdef HERMESVM_PROFILER_BB
if (auto *hermesRuntime = dynamic_cast<HermesRuntime *>(&rt_)) {
hermesRuntime->dumpBasicBlockProfileTrace(llvh::errs());
}
#endif
if (options_.snapshotMarker == "end") {
// Take a snapshot at the end if requested.
if (HermesRuntime *hermesRT = dynamic_cast<HermesRuntime *>(&rt_)) {
hermesRT->instrumentation().createSnapshotToFile(
options_.snapshotMarkerFileName);
} else {
llvh::errs() << "Heap snapshot requested from non-Hermes runtime\n";
}
snapshotMarkerFound_ = true;
}
if (!options_.snapshotMarker.empty() && !snapshotMarkerFound_) {
// Snapshot was requested at a marker but that marker wasn't found.
throw std::runtime_error(
std::string("Requested a heap snapshot at \"") +
options_.snapshotMarker + "\", but that marker wasn't reached\n");
}
// If this was a trace then stats were already collected.
if (options_.marker.empty()) {
return printStats();
} else {
if (!markerFound_) {
throw std::runtime_error(
std::string("Marker \"") + options_.marker +
"\" specified but not found in trace");
}
return stats_;
}
}
Value TraceInterpreter::execFunction(
const TraceInterpreter::Call &call,
const Value &thisVal,
const Value *args,
uint64_t count,
const PropNameID *nativePropNameToConsumeAsDef) {
llvh::SaveAndRestore<uint64_t> depthGuard(depth_, depth_ + 1);
// A mapping from an ObjectID to the Object for local variables.
// Invariant: value is Object or String;
std::unordered_map<ObjectID, Value> locals;
std::unordered_map<ObjectID, PropNameID> pniLocals;
// Save a value so that Call can set it, and Return can access it.
Value retval;
// Carry the return value from BeginJSExec to EndJSExec.
Value overallRetval;
#ifndef NDEBUG
if (depth_ != 1) {
RecordType firstRecType = call.pieces.front().records.front()->getType();
assert(
(firstRecType == RecordType::CallToNative ||
firstRecType == RecordType::GetNativePropertyNames ||
firstRecType == RecordType::GetPropertyNative ||
firstRecType == RecordType::SetPropertyNative) &&
"Illegal starting record");
}
#endif
#ifndef NDEBUG
// We'll want the first record, to verify that it's the one that consumes
// nativePropNameToConsumeAsDef if that is non-null.
const SynthTrace::Record *firstRec = call.pieces.at(0).records.at(0);
#endif
for (const TraceInterpreter::Call::Piece &piece : call.pieces) {
uint64_t globalRecordNum = piece.start;
const auto getJSIValueForUseOpt =
[this, &call, &locals, &globalRecordNum](
ObjectID obj) -> llvh::Optional<Value> {
// Check locals, then globals.
auto it = locals.find(obj);
if (it != locals.end()) {
// Satisfiable locally
Value val{rt_, it->second};
assert(val.isObject() || val.isString());
// If it was the last local use, delete that object id from locals.
auto defAndUse = call.locals.find(obj);
if (defAndUse != call.locals.end() &&
defAndUse->second.lastUse == globalRecordNum) {
assert(
defAndUse->second.lastDefBeforeFirstUse != DefAndUse::kUnused &&
"All uses must be preceded by a def");
locals.erase(it);
}
return val;
}
auto defAndUse = globalDefsAndUses_.find(obj);
// Since the use might not be a jsi::Value, it might be found in the
// locals table for non-Values, and therefore might not be in the global
// use/def table.
if (defAndUse == globalDefsAndUses_.end()) {
return llvh::None;
}
it = gom_.find(obj);
if (it != gom_.end()) {
Value val{rt_, it->second};
assert(val.isObject() || val.isString());
// If it was the last global use, delete that object id from globals.
if (defAndUse->second.lastUse == globalRecordNum) {
gom_.erase(it);
}
return val;
}
return llvh::None;
};
const auto getJSIValueForUse =
[&getJSIValueForUseOpt](ObjectID obj) -> Value {
auto valOpt = getJSIValueForUseOpt(obj);
assert(valOpt && "There must be a definition for all uses.");
return std::move(*valOpt);
};
const auto getObjForUse = [this,
getJSIValueForUse](ObjectID obj) -> Object {
return getJSIValueForUse(obj).getObject(rt_);
};
const auto getPropNameIDForUse =
[this, &call, &pniLocals, &globalRecordNum](