-
Notifications
You must be signed in to change notification settings - Fork 54
/
JSScript.cpp
5550 lines (4681 loc) · 172 KB
/
JSScript.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
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* JS script operations.
*/
#include "vm/JSScript-inl.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/PodOperations.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Span.h" // mozilla::{Span,MakeSpan}
#include "mozilla/Sprintf.h"
#include "mozilla/Unused.h"
#include "mozilla/Utf8.h"
#include "mozilla/Vector.h"
#include <algorithm>
#include <new>
#include <string.h>
#include <type_traits>
#include <utility>
#include "jsapi.h"
#include "jstypes.h"
#include "frontend/BinASTRuntimeSupport.h" // BinASTSourceMetadata{,Multipart,Context}
#include "frontend/BinASTTokenReaderContext.h" // HuffmanDictionaryForMetadata,AutoClearHuffmanDictionaryForMetadata
#include "frontend/BytecodeCompiler.h"
#include "frontend/BytecodeEmitter.h"
#include "frontend/SharedContext.h"
#include "frontend/SourceNotes.h" // SrcNote, SrcNoteType, SrcNoteIterator
#include "gc/FreeOp.h"
#include "jit/BaselineJIT.h"
#include "jit/Ion.h"
#include "jit/IonCode.h"
#include "jit/JitOptions.h"
#include "jit/JitRealm.h"
#include "js/CompileOptions.h"
#include "js/MemoryMetrics.h"
#include "js/Printf.h"
#include "js/SourceText.h"
#include "js/UniquePtr.h"
#include "js/Utility.h"
#include "js/Wrapper.h"
#include "util/Memory.h"
#include "util/Poison.h"
#include "util/StringBuffer.h"
#include "util/Text.h"
#include "vm/ArgumentsObject.h"
#include "vm/BytecodeIterator.h"
#include "vm/BytecodeLocation.h"
#include "vm/BytecodeUtil.h"
#include "vm/Compression.h"
#include "vm/HelperThreads.h" // js::RunPendingSourceCompressions
#include "vm/JSAtom.h"
#include "vm/JSContext.h"
#include "vm/JSFunction.h"
#include "vm/JSObject.h"
#include "vm/Opcodes.h"
#include "vm/SelfHosting.h"
#include "vm/Shape.h"
#include "vm/SharedImmutableStringsCache.h"
#include "vm/Warnings.h" // js::WarnNumberLatin1
#include "vm/Xdr.h"
#ifdef MOZ_VTUNE
# include "vtune/VTuneWrapper.h"
#endif
#include "debugger/DebugAPI-inl.h"
#include "gc/Marking-inl.h"
#include "vm/BytecodeIterator-inl.h"
#include "vm/BytecodeLocation-inl.h"
#include "vm/Compartment-inl.h"
#include "vm/EnvironmentObject-inl.h"
#include "vm/JSFunction-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/SharedImmutableStringsCache-inl.h"
#include "vm/Stack-inl.h"
using namespace js;
using mozilla::Maybe;
using mozilla::PodCopy;
using mozilla::PointerRangeSize;
using mozilla::Utf8AsUnsignedChars;
using mozilla::Utf8Unit;
using JS::CompileOptions;
using JS::ReadOnlyCompileOptions;
using JS::SourceText;
template <XDRMode mode>
XDRResult js::XDRScriptConst(XDRState<mode>* xdr, MutableHandleValue vp) {
JSContext* cx = xdr->cx();
enum ConstTag {
SCRIPT_INT,
SCRIPT_DOUBLE,
SCRIPT_ATOM,
SCRIPT_TRUE,
SCRIPT_FALSE,
SCRIPT_NULL,
SCRIPT_OBJECT,
SCRIPT_VOID,
SCRIPT_HOLE,
SCRIPT_BIGINT
};
ConstTag tag;
if (mode == XDR_ENCODE) {
if (vp.isInt32()) {
tag = SCRIPT_INT;
} else if (vp.isDouble()) {
tag = SCRIPT_DOUBLE;
} else if (vp.isString()) {
tag = SCRIPT_ATOM;
} else if (vp.isTrue()) {
tag = SCRIPT_TRUE;
} else if (vp.isFalse()) {
tag = SCRIPT_FALSE;
} else if (vp.isNull()) {
tag = SCRIPT_NULL;
} else if (vp.isObject()) {
tag = SCRIPT_OBJECT;
} else if (vp.isMagic(JS_ELEMENTS_HOLE)) {
tag = SCRIPT_HOLE;
} else if (vp.isBigInt()) {
tag = SCRIPT_BIGINT;
} else {
MOZ_ASSERT(vp.isUndefined());
tag = SCRIPT_VOID;
}
}
MOZ_TRY(xdr->codeEnum32(&tag));
switch (tag) {
case SCRIPT_INT: {
uint32_t i;
if (mode == XDR_ENCODE) {
i = uint32_t(vp.toInt32());
}
MOZ_TRY(xdr->codeUint32(&i));
if (mode == XDR_DECODE) {
vp.set(Int32Value(int32_t(i)));
}
break;
}
case SCRIPT_DOUBLE: {
double d;
if (mode == XDR_ENCODE) {
d = vp.toDouble();
}
MOZ_TRY(xdr->codeDouble(&d));
if (mode == XDR_DECODE) {
vp.set(DoubleValue(d));
}
break;
}
case SCRIPT_ATOM: {
RootedAtom atom(cx);
if (mode == XDR_ENCODE) {
atom = &vp.toString()->asAtom();
}
MOZ_TRY(XDRAtom(xdr, &atom));
if (mode == XDR_DECODE) {
vp.set(StringValue(atom));
}
break;
}
case SCRIPT_TRUE:
if (mode == XDR_DECODE) {
vp.set(BooleanValue(true));
}
break;
case SCRIPT_FALSE:
if (mode == XDR_DECODE) {
vp.set(BooleanValue(false));
}
break;
case SCRIPT_NULL:
if (mode == XDR_DECODE) {
vp.set(NullValue());
}
break;
case SCRIPT_OBJECT: {
RootedObject obj(cx);
if (mode == XDR_ENCODE) {
obj = &vp.toObject();
}
MOZ_TRY(XDRObjectLiteral(xdr, &obj));
if (mode == XDR_DECODE) {
vp.setObject(*obj);
}
break;
}
case SCRIPT_VOID:
if (mode == XDR_DECODE) {
vp.set(UndefinedValue());
}
break;
case SCRIPT_HOLE:
if (mode == XDR_DECODE) {
vp.setMagic(JS_ELEMENTS_HOLE);
}
break;
case SCRIPT_BIGINT: {
RootedBigInt bi(cx);
if (mode == XDR_ENCODE) {
bi = vp.toBigInt();
}
MOZ_TRY(XDRBigInt(xdr, &bi));
if (mode == XDR_DECODE) {
vp.setBigInt(bi);
}
break;
}
default:
// Fail in debug, but only soft-fail in release
MOZ_ASSERT(false, "Bad XDR value kind");
return xdr->fail(JS::TranscodeResult_Failure_BadDecode);
}
return Ok();
}
template XDRResult js::XDRScriptConst(XDRState<XDR_ENCODE>*,
MutableHandleValue);
template XDRResult js::XDRScriptConst(XDRState<XDR_DECODE>*,
MutableHandleValue);
// Code lazy scripts's closed over bindings.
template <XDRMode mode>
/* static */
XDRResult BaseScript::XDRLazyScriptData(XDRState<mode>* xdr,
HandleScriptSourceObject sourceObject,
Handle<BaseScript*> lazy,
bool hasFieldInitializers) {
JSContext* cx = xdr->cx();
RootedAtom atom(cx);
RootedFunction func(cx);
if (hasFieldInitializers) {
uint32_t numFieldInitializers;
if (mode == XDR_ENCODE) {
numFieldInitializers = lazy->getFieldInitializers().numFieldInitializers;
}
MOZ_TRY(xdr->codeUint32(&numFieldInitializers));
if (mode == XDR_DECODE) {
lazy->setFieldInitializers(FieldInitializers(numFieldInitializers));
}
}
mozilla::Span<JS::GCCellPtr> gcThings =
lazy->data_ ? lazy->data_->gcthings() : mozilla::Span<JS::GCCellPtr>();
for (JS::GCCellPtr& elem : gcThings) {
JS::TraceKind kind = elem.kind();
MOZ_TRY(xdr->codeEnum32(&kind));
switch (kind) {
case JS::TraceKind::Object: {
if (mode == XDR_ENCODE) {
func = &elem.as<JSObject>().as<JSFunction>();
}
MOZ_TRY(XDRInterpretedFunction(xdr, nullptr, sourceObject, &func));
if (mode == XDR_DECODE) {
func->setEnclosingLazyScript(lazy);
elem = JS::GCCellPtr(func);
}
break;
}
case JS::TraceKind::String: {
if (mode == XDR_ENCODE) {
gc::Cell* cell = elem.asCell();
MOZ_ASSERT_IF(cell, cell->is<JSAtom>());
atom = static_cast<JSAtom*>(cell);
}
MOZ_TRY(XDRAtom(xdr, &atom));
if (mode == XDR_DECODE) {
elem = JS::GCCellPtr(static_cast<JSString*>(atom));
}
break;
}
case JS::TraceKind::Null: {
// This is default so nothing to do.
MOZ_ASSERT(!elem);
break;
}
default: {
// Fail in debug, but only soft-fail in release
MOZ_ASSERT(false, "Bad XDR class kind");
return xdr->fail(JS::TranscodeResult_Failure_BadDecode);
}
}
}
return Ok();
}
static inline uint32_t FindScopeIndex(mozilla::Span<const JS::GCCellPtr> scopes,
Scope& scope) {
unsigned length = scopes.size();
for (uint32_t i = 0; i < length; ++i) {
if (scopes[i].asCell() == &scope) {
return i;
}
}
MOZ_CRASH("Scope not found");
}
template <XDRMode mode>
static XDRResult XDRInnerObject(XDRState<mode>* xdr,
js::PrivateScriptData* data,
HandleScriptSourceObject sourceObject,
MutableHandleObject inner) {
enum class ClassKind { RegexpObject, JSFunction, JSObject, ArrayObject };
JSContext* cx = xdr->cx();
ClassKind classk;
if (mode == XDR_ENCODE) {
if (inner->is<RegExpObject>()) {
classk = ClassKind::RegexpObject;
} else if (inner->is<JSFunction>()) {
classk = ClassKind::JSFunction;
} else if (inner->is<PlainObject>()) {
classk = ClassKind::JSObject;
} else if (inner->is<ArrayObject>()) {
classk = ClassKind::ArrayObject;
} else {
MOZ_CRASH("Cannot encode this class of object.");
}
}
MOZ_TRY(xdr->codeEnum32(&classk));
switch (classk) {
case ClassKind::RegexpObject: {
Rooted<RegExpObject*> regexp(cx);
if (mode == XDR_ENCODE) {
regexp = &inner->as<RegExpObject>();
}
MOZ_TRY(XDRScriptRegExpObject(xdr, ®exp));
if (mode == XDR_DECODE) {
inner.set(regexp);
}
break;
}
case ClassKind::JSFunction: {
/* Code the nested function's enclosing scope. */
uint32_t funEnclosingScopeIndex = 0;
RootedScope funEnclosingScope(cx);
if (mode == XDR_ENCODE) {
RootedFunction function(cx, &inner->as<JSFunction>());
if (function->isAsmJSNative()) {
return xdr->fail(JS::TranscodeResult_Failure_AsmJSNotSupported);
}
MOZ_ASSERT(function->enclosingScope());
funEnclosingScope = function->enclosingScope();
funEnclosingScopeIndex =
FindScopeIndex(data->gcthings(), *funEnclosingScope);
}
MOZ_TRY(xdr->codeUint32(&funEnclosingScopeIndex));
if (mode == XDR_DECODE) {
funEnclosingScope =
&data->gcthings()[funEnclosingScopeIndex].as<Scope>();
}
// Code nested function and script.
RootedFunction tmp(cx);
if (mode == XDR_ENCODE) {
tmp = &inner->as<JSFunction>();
}
MOZ_TRY(
XDRInterpretedFunction(xdr, funEnclosingScope, sourceObject, &tmp));
if (mode == XDR_DECODE) {
inner.set(tmp);
}
break;
}
case ClassKind::JSObject:
case ClassKind::ArrayObject: {
/* Code object literal. */
RootedObject tmp(cx);
if (mode == XDR_ENCODE) {
tmp = inner.get();
}
MOZ_TRY(XDRObjectLiteral(xdr, &tmp));
if (mode == XDR_DECODE) {
inner.set(tmp);
}
break;
}
default: {
// Fail in debug, but only soft-fail in release
MOZ_ASSERT(false, "Bad XDR class kind");
return xdr->fail(JS::TranscodeResult_Failure_BadDecode);
}
}
return Ok();
}
template <XDRMode mode>
static XDRResult XDRScope(XDRState<mode>* xdr, js::PrivateScriptData* data,
HandleScope scriptEnclosingScope,
HandleObject funOrMod, bool isFirstScope,
MutableHandleScope scope) {
JSContext* cx = xdr->cx();
ScopeKind scopeKind;
RootedScope enclosing(cx);
RootedFunction fun(cx);
RootedModuleObject module(cx);
uint32_t enclosingIndex = 0;
// The enclosingScope is encoded using an integer index into the scope array.
// This means that scopes must be topologically sorted.
if (mode == XDR_ENCODE) {
scopeKind = scope->kind();
if (isFirstScope) {
enclosingIndex = UINT32_MAX;
} else {
MOZ_ASSERT(scope->enclosing());
enclosingIndex = FindScopeIndex(data->gcthings(), *scope->enclosing());
}
}
MOZ_TRY(xdr->codeEnum32(&scopeKind));
MOZ_TRY(xdr->codeUint32(&enclosingIndex));
if (mode == XDR_DECODE) {
if (isFirstScope) {
MOZ_ASSERT(enclosingIndex == UINT32_MAX);
enclosing = scriptEnclosingScope;
} else {
enclosing = &data->gcthings()[enclosingIndex].as<Scope>();
}
if (funOrMod && funOrMod->is<ModuleObject>()) {
module.set(funOrMod.as<ModuleObject>());
} else if (funOrMod && funOrMod->is<JSFunction>()) {
fun.set(funOrMod.as<JSFunction>());
}
}
switch (scopeKind) {
case ScopeKind::Function:
MOZ_TRY(FunctionScope::XDR(xdr, fun, enclosing, scope));
break;
case ScopeKind::FunctionBodyVar:
MOZ_TRY(VarScope::XDR(xdr, scopeKind, enclosing, scope));
break;
case ScopeKind::Lexical:
case ScopeKind::SimpleCatch:
case ScopeKind::Catch:
case ScopeKind::NamedLambda:
case ScopeKind::StrictNamedLambda:
case ScopeKind::FunctionLexical:
MOZ_TRY(LexicalScope::XDR(xdr, scopeKind, enclosing, scope));
break;
case ScopeKind::With:
MOZ_TRY(WithScope::XDR(xdr, enclosing, scope));
break;
case ScopeKind::Eval:
case ScopeKind::StrictEval:
MOZ_TRY(EvalScope::XDR(xdr, scopeKind, enclosing, scope));
break;
case ScopeKind::Global:
case ScopeKind::NonSyntactic:
MOZ_TRY(GlobalScope::XDR(xdr, scopeKind, scope));
break;
case ScopeKind::Module:
MOZ_TRY(ModuleScope::XDR(xdr, module, enclosing, scope));
break;
case ScopeKind::WasmInstance:
MOZ_CRASH("NYI");
break;
case ScopeKind::WasmFunction:
MOZ_CRASH("wasm functions cannot be nested in JSScripts");
break;
default:
// Fail in debug, but only soft-fail in release
MOZ_ASSERT(false, "Bad XDR scope kind");
return xdr->fail(JS::TranscodeResult_Failure_BadDecode);
}
return Ok();
}
template <XDRMode mode>
static XDRResult XDRScriptGCThing(XDRState<mode>* xdr, PrivateScriptData* data,
HandleScriptSourceObject sourceObject,
HandleScope scriptEnclosingScope,
HandleObject funOrMod, bool* isFirstScope,
JS::GCCellPtr* thingp) {
JSContext* cx = xdr->cx();
JS::TraceKind kind = thingp->kind();
MOZ_TRY(xdr->codeEnum32(&kind));
switch (kind) {
case JS::TraceKind::Object: {
RootedObject obj(cx);
if (mode == XDR_ENCODE) {
obj = &thingp->as<JSObject>();
}
MOZ_TRY(XDRInnerObject(xdr, data, sourceObject, &obj));
if (mode == XDR_DECODE) {
*thingp = JS::GCCellPtr(obj.get());
}
break;
}
case JS::TraceKind::Scope: {
RootedScope scope(cx);
if (mode == XDR_ENCODE) {
scope = &thingp->as<Scope>();
}
MOZ_TRY(XDRScope(xdr, data, scriptEnclosingScope, funOrMod, *isFirstScope,
&scope));
if (mode == XDR_DECODE) {
*thingp = JS::GCCellPtr(scope.get());
}
*isFirstScope = false;
break;
}
case JS::TraceKind::BigInt: {
RootedBigInt bi(cx);
if (mode == XDR_ENCODE) {
bi = &thingp->as<BigInt>();
}
MOZ_TRY(XDRBigInt(xdr, &bi));
if (mode == XDR_DECODE) {
*thingp = JS::GCCellPtr(bi.get());
}
break;
}
default:
// Fail in debug, but only soft-fail in release.
MOZ_ASSERT(false, "Bad XDR class kind");
return xdr->fail(JS::TranscodeResult_Failure_BadDecode);
}
return Ok();
}
bool js::BaseScript::isUsingInterpreterTrampoline(JSRuntime* rt) const {
return jitCodeRaw_ == rt->jitRuntime()->interpreterStub().value;
}
js::ScriptSource* js::BaseScript::maybeForwardedScriptSource() const {
return MaybeForwarded(sourceObject())->source();
}
void js::BaseScript::setEnclosingScript(BaseScript* enclosingScript) {
MOZ_ASSERT(enclosingScript);
warmUpData_.initEnclosingScript(enclosingScript);
}
void js::BaseScript::setEnclosingScope(Scope* enclosingScope) {
if (warmUpData_.isEnclosingScript()) {
warmUpData_.clearEnclosingScript();
}
MOZ_ASSERT(enclosingScope);
warmUpData_.initEnclosingScope(enclosingScope);
}
void js::BaseScript::finalize(JSFreeOp* fop) {
// Scripts with bytecode may have optional data stored in per-runtime or
// per-zone maps. Note that a failed compilation must not have entries since
// the script itself will not be marked as having bytecode.
if (hasBytecode()) {
JSScript* script = this->asJSScript();
if (coverage::IsLCovEnabled()) {
coverage::CollectScriptCoverage(script, true);
}
script->destroyScriptCounts();
DebugAPI::destroyDebugScript(fop, script);
}
fop->runtime()->geckoProfiler().onScriptFinalized(this);
#ifdef MOZ_VTUNE
if (zone()->scriptVTuneIdMap) {
// Note: we should only get here if the VTune JIT profiler is running.
zone()->scriptVTuneIdMap->remove(this);
}
#endif
if (warmUpData_.isJitScript()) {
JSScript* script = this->asJSScript();
script->releaseJitScriptOnFinalize(fop);
}
if (data_) {
size_t size = data_->allocationSize();
AlwaysPoison(data_, JS_POISONED_JSSCRIPT_DATA_PATTERN, size,
MemCheckKind::MakeNoAccess);
fop->free_(this, data_, size, MemoryUse::ScriptPrivateData);
}
freeSharedData();
}
js::Scope* js::BaseScript::releaseEnclosingScope() {
Scope* enclosing = warmUpData_.toEnclosingScope();
warmUpData_.clearEnclosingScope();
return enclosing;
}
void js::BaseScript::swapData(UniquePtr<PrivateScriptData>& other) {
PrivateScriptData* tmp = other.release();
if (data_) {
// When disconnecting script data from the BaseScript, we must pre-barrier
// all edges contained in it. Those edges are no longer reachable from
// current location in the graph.
if (zone()->needsIncrementalBarrier()) {
data_->trace(zone()->barrierTracer());
}
RemoveCellMemory(this, data_->allocationSize(),
MemoryUse::ScriptPrivateData);
}
std::swap(tmp, data_);
if (data_) {
AddCellMemory(this, data_->allocationSize(), MemoryUse::ScriptPrivateData);
}
other.reset(tmp);
}
template <XDRMode mode>
/* static */
XDRResult js::PrivateScriptData::XDR(XDRState<mode>* xdr, HandleScript script,
HandleScriptSourceObject sourceObject,
HandleScope scriptEnclosingScope,
HandleObject funOrMod) {
uint32_t ngcthings = 0;
JSContext* cx = xdr->cx();
PrivateScriptData* data = nullptr;
if (mode == XDR_ENCODE) {
data = script->data_;
ngcthings = data->gcthings().size();
}
MOZ_TRY(xdr->codeUint32(&ngcthings));
if (mode == XDR_DECODE) {
if (!JSScript::createPrivateScriptData(cx, script, ngcthings)) {
return xdr->fail(JS::TranscodeResult_Throw);
}
data = script->data_;
}
// Code the field initilizer data.
if (funOrMod && funOrMod->is<JSFunction>() &&
funOrMod->as<JSFunction>().isClassConstructor()) {
uint32_t numFieldInitializers;
if (mode == XDR_ENCODE) {
numFieldInitializers = data->getFieldInitializers().numFieldInitializers;
}
MOZ_TRY(xdr->codeUint32(&numFieldInitializers));
if (mode == XDR_DECODE) {
data->setFieldInitializers(FieldInitializers(numFieldInitializers));
}
}
bool isFirstScope = true;
for (JS::GCCellPtr& gcThing : data->gcthings()) {
MOZ_TRY(XDRScriptGCThing(xdr, data, sourceObject, scriptEnclosingScope,
funOrMod, &isFirstScope, &gcThing));
}
// Verify marker to detect data corruption after decoding GC things. A
// mismatch here indicates we will almost certainly crash in release.
MOZ_TRY(xdr->codeMarker(0xF83B989A));
return Ok();
}
/* static */ size_t ImmutableScriptData::AllocationSize(
uint32_t codeLength, uint32_t noteLength, uint32_t numResumeOffsets,
uint32_t numScopeNotes, uint32_t numTryNotes) {
size_t size = sizeof(ImmutableScriptData);
size += sizeof(Flags);
size += codeLength * sizeof(jsbytecode);
size += noteLength * sizeof(SrcNote);
#ifdef DEBUG
// The compact arrays need to maintain uint32_t alignment. This should have
// been done by padding out source notes.
MOZ_ASSERT(size % sizeof(uint32_t) == 0,
"Source notes should have been padded already");
#endif
unsigned numOptionalArrays = unsigned(numResumeOffsets > 0) +
unsigned(numScopeNotes > 0) +
unsigned(numTryNotes > 0);
size += numOptionalArrays * sizeof(Offset);
size += numResumeOffsets * sizeof(uint32_t);
size += numScopeNotes * sizeof(ScopeNote);
size += numTryNotes * sizeof(TryNote);
return size;
}
// Placement-new elements of an array. This should optimize away for types with
// trivial default initiation.
template <typename T>
void ImmutableScriptData::initElements(size_t offset, size_t length) {
uintptr_t base = reinterpret_cast<uintptr_t>(this);
DefaultInitializeElements<T>(reinterpret_cast<void*>(base + offset), length);
}
// Initialize the optional arrays in the trailing allocation. This is a set of
// offsets that delimit each optional array followed by the arrays themselves.
// See comment before 'ImmutableScriptData' for more details.
void ImmutableScriptData::initOptionalArrays(size_t* pcursor,
ImmutableScriptData::Flags* flags,
uint32_t numResumeOffsets,
uint32_t numScopeNotes,
uint32_t numTryNotes) {
size_t cursor = (*pcursor);
// The byte arrays must have already been padded.
MOZ_ASSERT(cursor % sizeof(uint32_t) == 0);
// Each non-empty optional array needs will need an offset to its end.
unsigned numOptionalArrays = unsigned(numResumeOffsets > 0) +
unsigned(numScopeNotes > 0) +
unsigned(numTryNotes > 0);
// Default-initialize the optional-offsets.
static_assert(alignof(ImmutableScriptData) >= alignof(Offset),
"Incompatible alignment");
initElements<Offset>(cursor, numOptionalArrays);
cursor += numOptionalArrays * sizeof(Offset);
// Offset between optional-offsets table and the optional arrays. This is
// later used to access the optional-offsets table as well as first optional
// array.
optArrayOffset_ = cursor;
// Each optional array that follows must store an end-offset in the offset
// table. Assign table entries by using this 'offsetIndex'. The index 0 is
// reserved for implicit value 'optArrayOffset'.
int offsetIndex = 0;
// Default-initialize optional 'resumeOffsets'.
MOZ_ASSERT(resumeOffsetsOffset() == cursor);
if (numResumeOffsets > 0) {
static_assert(sizeof(Offset) >= alignof(uint32_t),
"Incompatible alignment");
initElements<uint32_t>(cursor, numResumeOffsets);
cursor += numResumeOffsets * sizeof(uint32_t);
setOptionalOffset(++offsetIndex, cursor);
}
flags->resumeOffsetsEndIndex = offsetIndex;
// Default-initialize optional 'scopeNotes'.
MOZ_ASSERT(scopeNotesOffset() == cursor);
if (numScopeNotes > 0) {
static_assert(sizeof(uint32_t) >= alignof(ScopeNote),
"Incompatible alignment");
initElements<ScopeNote>(cursor, numScopeNotes);
cursor += numScopeNotes * sizeof(ScopeNote);
setOptionalOffset(++offsetIndex, cursor);
}
flags->scopeNotesEndIndex = offsetIndex;
// Default-initialize optional 'tryNotes'
MOZ_ASSERT(tryNotesOffset() == cursor);
if (numTryNotes > 0) {
static_assert(sizeof(ScopeNote) >= alignof(TryNote),
"Incompatible alignment");
initElements<TryNote>(cursor, numTryNotes);
cursor += numTryNotes * sizeof(TryNote);
setOptionalOffset(++offsetIndex, cursor);
}
flags->tryNotesEndIndex = offsetIndex;
MOZ_ASSERT(endOffset() == cursor);
(*pcursor) = cursor;
}
ImmutableScriptData::ImmutableScriptData(uint32_t codeLength,
uint32_t noteLength,
uint32_t numResumeOffsets,
uint32_t numScopeNotes,
uint32_t numTryNotes)
: codeLength_(codeLength) {
// Variable-length data begins immediately after ImmutableScriptData itself.
size_t cursor = sizeof(*this);
// The following arrays are byte-aligned with additional padding to ensure
// that together they maintain uint32_t-alignment.
{
MOZ_ASSERT(cursor % CodeNoteAlign == 0);
// Zero-initialize 'flags'
static_assert(CodeNoteAlign >= alignof(Flags), "Incompatible alignment");
new (offsetToPointer<void>(cursor)) Flags{};
cursor += sizeof(Flags);
static_assert(alignof(Flags) >= alignof(jsbytecode),
"Incompatible alignment");
initElements<jsbytecode>(cursor, codeLength);
cursor += codeLength * sizeof(jsbytecode);
static_assert(alignof(jsbytecode) >= alignof(SrcNote),
"Incompatible alignment");
initElements<SrcNote>(cursor, noteLength);
cursor += noteLength * sizeof(SrcNote);
MOZ_ASSERT(cursor % CodeNoteAlign == 0);
}
// Initialization for remaining arrays.
initOptionalArrays(&cursor, &flagsRef(), numResumeOffsets, numScopeNotes,
numTryNotes);
// Check that we correctly recompute the expected values.
MOZ_ASSERT(this->codeLength() == codeLength);
MOZ_ASSERT(this->noteLength() == noteLength);
// Sanity check
MOZ_ASSERT(AllocationSize(codeLength, noteLength, numResumeOffsets,
numScopeNotes, numTryNotes) == cursor);
}
template <XDRMode mode>
/* static */
XDRResult ImmutableScriptData::XDR(XDRState<mode>* xdr,
UniquePtr<ImmutableScriptData>& isd) {
uint32_t codeLength = 0;
uint32_t noteLength = 0;
uint32_t numResumeOffsets = 0;
uint32_t numScopeNotes = 0;
uint32_t numTryNotes = 0;
JSContext* cx = xdr->cx();
if (mode == XDR_ENCODE) {
codeLength = isd->codeLength();
noteLength = isd->noteLength();
numResumeOffsets = isd->resumeOffsets().size();
numScopeNotes = isd->scopeNotes().size();
numTryNotes = isd->tryNotes().size();
}
MOZ_TRY(xdr->codeUint32(&codeLength));
MOZ_TRY(xdr->codeUint32(¬eLength));
MOZ_TRY(xdr->codeUint32(&numResumeOffsets));
MOZ_TRY(xdr->codeUint32(&numScopeNotes));
MOZ_TRY(xdr->codeUint32(&numTryNotes));
if (mode == XDR_DECODE) {
isd =
ImmutableScriptData::new_(cx, codeLength, noteLength, numResumeOffsets,
numScopeNotes, numTryNotes);
if (!isd) {
return xdr->fail(JS::TranscodeResult_Throw);
}
}
MOZ_TRY(xdr->codeUint32(&isd->mainOffset));
MOZ_TRY(xdr->codeUint32(&isd->nfixed));
MOZ_TRY(xdr->codeUint32(&isd->nslots));
MOZ_TRY(xdr->codeUint32(&isd->bodyScopeIndex));
MOZ_TRY(xdr->codeUint32(&isd->numICEntries));
MOZ_TRY(xdr->codeUint16(&isd->funLength));
MOZ_TRY(xdr->codeUint16(&isd->numBytecodeTypeSets));
static_assert(sizeof(jsbytecode) == 1);
static_assert(sizeof(SrcNote) == 1);
jsbytecode* code = isd->code();
SrcNote* notes = isd->notes();
MOZ_TRY(xdr->codeBytes(code, codeLength));
MOZ_TRY(xdr->codeBytes(notes, noteLength));
for (uint32_t& elem : isd->resumeOffsets()) {
MOZ_TRY(xdr->codeUint32(&elem));
}
for (ScopeNote& elem : isd->scopeNotes()) {
MOZ_TRY(xdr->codeUint32(&elem.index));
MOZ_TRY(xdr->codeUint32(&elem.start));
MOZ_TRY(xdr->codeUint32(&elem.length));
MOZ_TRY(xdr->codeUint32(&elem.parent));
}
for (TryNote& elem : isd->tryNotes()) {
MOZ_TRY(xdr->codeUint32(&elem.kind_));
MOZ_TRY(xdr->codeUint32(&elem.stackDepth));
MOZ_TRY(xdr->codeUint32(&elem.start));
MOZ_TRY(xdr->codeUint32(&elem.length));
}
return Ok();
}
template
/* static */
XDRResult
ImmutableScriptData::XDR(XDRState<XDR_ENCODE>* xdr,
js::UniquePtr<ImmutableScriptData>& script);
template
/* static */
XDRResult
ImmutableScriptData::XDR(XDRState<XDR_DECODE>* xdr,
js::UniquePtr<ImmutableScriptData>& script);
/* static */ size_t RuntimeScriptData::AllocationSize(uint32_t natoms) {
size_t size = sizeof(RuntimeScriptData);
size += natoms * sizeof(GCPtrAtom);
return size;
}
// Placement-new elements of an array. This should optimize away for types with
// trivial default initiation.
template <typename T>
void RuntimeScriptData::initElements(size_t offset, size_t length) {
uintptr_t base = reinterpret_cast<uintptr_t>(this);
DefaultInitializeElements<T>(reinterpret_cast<void*>(base + offset), length);
}
RuntimeScriptData::RuntimeScriptData(uint32_t natoms) : natoms_(natoms) {
// Variable-length data begins immediately after RuntimeScriptData itself.
size_t cursor = sizeof(*this);
// Default-initialize trailing arrays.
static_assert(alignof(RuntimeScriptData) >= alignof(GCPtrAtom),
"Incompatible alignment");
initElements<GCPtrAtom>(cursor, natoms);
cursor += natoms * sizeof(GCPtrAtom);
// Check that we correctly recompute the expected values.
MOZ_ASSERT(this->natoms() == natoms);
// Sanity check
MOZ_ASSERT(AllocationSize(natoms) == cursor);
}
template <XDRMode mode>
/* static */
XDRResult RuntimeScriptData::XDR(XDRState<mode>* xdr, HandleScript script) {
uint32_t natoms = 0;
JSContext* cx = xdr->cx();