forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.h
2193 lines (1803 loc) · 71.7 KB
/
Stack.h
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: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
* 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/. */
#ifndef vm_Stack_h
#define vm_Stack_h
#include "mozilla/Atomics.h"
#include "mozilla/Maybe.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Variant.h"
#include "jsfun.h"
#include "jsscript.h"
#include "jsutil.h"
#include "asmjs/WasmFrameIterator.h"
#include "gc/Rooting.h"
#include "jit/JitFrameIterator.h"
#ifdef CHECK_OSIPOINT_REGISTERS
#include "jit/Registers.h" // for RegisterDump
#endif
#include "js/RootingAPI.h"
#include "vm/SavedFrame.h"
struct JSCompartment;
namespace JS {
namespace dbg {
class AutoEntryMonitor;
} // namespace dbg
} // namespace JS
namespace js {
class ArgumentsObject;
class InterpreterRegs;
class CallObject;
class FrameIter;
class ScopeObject;
class ScriptFrameIter;
class SPSProfiler;
class InterpreterFrame;
class StaticBlockObject;
class ClonedBlockObject;
class ScopeCoordinate;
class SavedFrame;
namespace jit {
class CommonFrameLayout;
}
// VM stack layout
//
// A JSRuntime's stack consists of a linked list of activations. Every activation
// contains a number of scripted frames that are either running in the interpreter
// (InterpreterActivation) or JIT code (JitActivation). The frames inside a single
// activation are contiguous: whenever C++ calls back into JS, a new activation is
// pushed.
//
// Every activation is tied to a single JSContext and JSCompartment. This means we
// can reconstruct a given context's stack by skipping activations belonging to other
// contexts. This happens whenever an embedding enters the JS engine on cx1 and
// then, from a native called by the JS engine, reenters the VM on cx2.
// Interpreter frames (InterpreterFrame)
//
// Each interpreter script activation (global or function code) is given a
// fixed-size header (js::InterpreterFrame). The frame contains bookkeeping
// information about the activation and links to the previous frame.
//
// The values after an InterpreterFrame in memory are its locals followed by its
// expression stack. InterpreterFrame::argv_ points to the frame's arguments.
// Missing formal arguments are padded with |undefined|, so the number of
// arguments is always >= the number of formals.
//
// The top of an activation's current frame's expression stack is pointed to by
// the activation's "current regs", which contains the stack pointer 'sp'. In
// the interpreter, sp is adjusted as individual values are pushed and popped
// from the stack and the InterpreterRegs struct (pointed to by the
// InterpreterActivation) is a local var of js::Interpret.
enum MaybeCheckAliasing { CHECK_ALIASING = true, DONT_CHECK_ALIASING = false };
enum MaybeCheckLexical { CheckLexical = true, DontCheckLexical = false };
/*****************************************************************************/
namespace jit {
class BaselineFrame;
class RematerializedFrame;
} // namespace jit
/*
* Pointer to either a ScriptFrameIter::Data, an InterpreterFrame, or a Baseline
* JIT frame.
*
* The Debugger may cache ScriptFrameIter::Data as a bookmark to reconstruct a
* ScriptFrameIter without doing a full stack walk.
*
* There is no way to directly create such an AbstractFramePtr. To do so, the
* user must call ScriptFrameIter::copyDataAsAbstractFramePtr().
*
* ScriptFrameIter::abstractFramePtr() will never return an AbstractFramePtr
* that is in fact a ScriptFrameIter::Data.
*
* To recover a ScriptFrameIter settled at the location pointed to by an
* AbstractFramePtr, use the THIS_FRAME_ITER macro in Debugger.cpp. As an
* aside, no asScriptFrameIterData() is provided because C++ is stupid and
* cannot forward declare inner classes.
*/
class AbstractFramePtr
{
friend class FrameIter;
uintptr_t ptr_;
enum {
Tag_ScriptFrameIterData = 0x0,
Tag_InterpreterFrame = 0x1,
Tag_BaselineFrame = 0x2,
Tag_RematerializedFrame = 0x3,
TagMask = 0x3
};
public:
AbstractFramePtr()
: ptr_(0)
{}
MOZ_IMPLICIT AbstractFramePtr(InterpreterFrame* fp)
: ptr_(fp ? uintptr_t(fp) | Tag_InterpreterFrame : 0)
{
MOZ_ASSERT_IF(fp, asInterpreterFrame() == fp);
}
MOZ_IMPLICIT AbstractFramePtr(jit::BaselineFrame* fp)
: ptr_(fp ? uintptr_t(fp) | Tag_BaselineFrame : 0)
{
MOZ_ASSERT_IF(fp, asBaselineFrame() == fp);
}
MOZ_IMPLICIT AbstractFramePtr(jit::RematerializedFrame* fp)
: ptr_(fp ? uintptr_t(fp) | Tag_RematerializedFrame : 0)
{
MOZ_ASSERT_IF(fp, asRematerializedFrame() == fp);
}
static AbstractFramePtr FromRaw(void* raw) {
AbstractFramePtr frame;
frame.ptr_ = uintptr_t(raw);
return frame;
}
bool isScriptFrameIterData() const {
return !!ptr_ && (ptr_ & TagMask) == Tag_ScriptFrameIterData;
}
bool isInterpreterFrame() const {
return (ptr_ & TagMask) == Tag_InterpreterFrame;
}
InterpreterFrame* asInterpreterFrame() const {
MOZ_ASSERT(isInterpreterFrame());
InterpreterFrame* res = (InterpreterFrame*)(ptr_ & ~TagMask);
MOZ_ASSERT(res);
return res;
}
bool isBaselineFrame() const {
return (ptr_ & TagMask) == Tag_BaselineFrame;
}
jit::BaselineFrame* asBaselineFrame() const {
MOZ_ASSERT(isBaselineFrame());
jit::BaselineFrame* res = (jit::BaselineFrame*)(ptr_ & ~TagMask);
MOZ_ASSERT(res);
return res;
}
bool isRematerializedFrame() const {
return (ptr_ & TagMask) == Tag_RematerializedFrame;
}
jit::RematerializedFrame* asRematerializedFrame() const {
MOZ_ASSERT(isRematerializedFrame());
jit::RematerializedFrame* res = (jit::RematerializedFrame*)(ptr_ & ~TagMask);
MOZ_ASSERT(res);
return res;
}
void* raw() const { return reinterpret_cast<void*>(ptr_); }
bool operator ==(const AbstractFramePtr& other) const { return ptr_ == other.ptr_; }
bool operator !=(const AbstractFramePtr& other) const { return ptr_ != other.ptr_; }
explicit operator bool() const { return !!ptr_; }
inline JSObject* scopeChain() const;
inline CallObject& callObj() const;
inline bool initFunctionScopeObjects(JSContext* cx);
inline void pushOnScopeChain(ScopeObject& scope);
inline JSCompartment* compartment() const;
inline bool hasCallObj() const;
inline bool isFunctionFrame() const;
inline bool isGlobalOrModuleFrame() const;
inline bool isGlobalFrame() const;
inline bool isModuleFrame() const;
inline bool isEvalFrame() const;
inline bool isDebuggerEvalFrame() const;
inline bool hasCachedSavedFrame() const;
inline void setHasCachedSavedFrame();
inline JSScript* script() const;
inline JSFunction* callee() const;
inline Value calleev() const;
inline Value& thisArgument() const;
inline Value newTarget() const;
inline bool isNonEvalFunctionFrame() const;
inline bool isNonStrictDirectEvalFrame() const;
inline bool isStrictEvalFrame() const;
inline unsigned numActualArgs() const;
inline unsigned numFormalArgs() const;
inline Value* argv() const;
inline bool hasArgs() const;
inline bool hasArgsObj() const;
inline ArgumentsObject& argsObj() const;
inline void initArgsObj(ArgumentsObject& argsobj) const;
inline bool createSingleton() const;
inline bool copyRawFrameSlots(AutoValueVector* vec) const;
inline Value& unaliasedLocal(uint32_t i);
inline Value& unaliasedFormal(unsigned i, MaybeCheckAliasing checkAliasing = CHECK_ALIASING);
inline Value& unaliasedActual(unsigned i, MaybeCheckAliasing checkAliasing = CHECK_ALIASING);
template <class Op> inline void unaliasedForEachActual(JSContext* cx, Op op);
inline bool prevUpToDate() const;
inline void setPrevUpToDate() const;
inline void unsetPrevUpToDate() const;
inline bool isDebuggee() const;
inline void setIsDebuggee();
inline void unsetIsDebuggee();
inline HandleValue returnValue() const;
inline void setReturnValue(const Value& rval) const;
inline bool freshenBlock(JSContext* cx) const;
inline void popBlock(JSContext* cx) const;
inline void popWith(JSContext* cx) const;
friend void GDBTestInitAbstractFramePtr(AbstractFramePtr&, void*);
friend void GDBTestInitAbstractFramePtr(AbstractFramePtr&, InterpreterFrame*);
friend void GDBTestInitAbstractFramePtr(AbstractFramePtr&, jit::BaselineFrame*);
friend void GDBTestInitAbstractFramePtr(AbstractFramePtr&, jit::RematerializedFrame*);
};
class NullFramePtr : public AbstractFramePtr
{
public:
NullFramePtr()
: AbstractFramePtr()
{ }
};
/*****************************************************************************/
/* Flags specified for a frame as it is constructed. */
enum InitialFrameFlags {
INITIAL_NONE = 0,
INITIAL_CONSTRUCT = 0x20, /* == InterpreterFrame::CONSTRUCTING, asserted below */
};
enum ExecuteType {
EXECUTE_GLOBAL_OR_MODULE = 0x1, /* == InterpreterFrame::GLOBAL_OR_MODULE */
EXECUTE_DIRECT_EVAL = 0x8, /* == InterpreterFrame::EVAL */
EXECUTE_INDIRECT_EVAL = 0x9, /* == InterpreterFrame::GLOBAL | EVAL */
EXECUTE_DEBUG = 0x18, /* == InterpreterFrame::EVAL | DEBUGGER_EVAL */
};
/*****************************************************************************/
class InterpreterFrame
{
public:
enum Flags : uint32_t {
/* Primary frame type */
GLOBAL_OR_MODULE = 0x1, /* frame pushed for a global script */
FUNCTION = 0x2, /* frame pushed for a scripted call */
/* Frame subtypes */
EVAL = 0x8, /* frame pushed for eval() or debugger eval */
/*
* Frame pushed for debugger eval.
* - Don't bother to JIT it, because it's probably short-lived.
* - It is required to have a scope chain object outside the
* js::ScopeObject hierarchy: either a global object, or a
* DebugScopeObject (not a ScopeObject, despite the name)
* - If evalInFramePrev_ is set, then this frame was created for an
* "eval in frame" call, which can push a successor to any live
* frame; so its logical "prev" frame is not necessarily the
* previous frame in memory. Iteration should treat
* evalInFramePrev_ as this frame's previous frame.
*/
DEBUGGER_EVAL = 0x10,
CONSTRUCTING = 0x20, /* frame is for a constructor invocation */
RESUMED_GENERATOR = 0x40, /* frame is for a resumed generator invocation */
/* (0x80 is unused) */
/* Function prologue state */
HAS_CALL_OBJ = 0x100, /* CallObject created for needsCallObject function */
HAS_ARGS_OBJ = 0x200, /* ArgumentsObject created for needsArgsObj script */
/* Lazy frame initialization */
HAS_RVAL = 0x800, /* frame has rval_ set */
HAS_SCOPECHAIN = 0x1000, /* frame has scopeChain_ set */
/* Debugger state */
PREV_UP_TO_DATE = 0x4000, /* see DebugScopes::updateLiveScopes */
/*
* See comment above 'isDebuggee' in jscompartment.h for explanation of
* invariants of debuggee compartments, scripts, and frames.
*/
DEBUGGEE = 0x8000, /* Execution is being observed by Debugger */
/* Used in tracking calls and profiling (see vm/SPSProfiler.cpp) */
HAS_PUSHED_SPS_FRAME = 0x10000, /* SPS was notified of enty */
/*
* If set, we entered one of the JITs and ScriptFrameIter should skip
* this frame.
*/
RUNNING_IN_JIT = 0x20000,
/* Miscellaneous state. */
CREATE_SINGLETON = 0x40000, /* Constructed |this| object should be singleton. */
/*
* If set, this frame has been on the stack when
* |js::SavedStacks::saveCurrentStack| was called, and so there is a
* |js::SavedFrame| object cached for this frame.
*/
HAS_CACHED_SAVED_FRAME = 0x80000,
};
private:
mutable uint32_t flags_; /* bits described by Flags */
union { /* describes what code is executing in a */
JSScript* script; /* global frame */
ModuleObject* module; /* module frame */
} exec;
union { /* describes the arguments of a function */
unsigned nactual; /* for non-eval frames */
JSScript* evalScript; /* the script of an eval-in-function */
} u;
mutable JSObject* scopeChain_; /* if HAS_SCOPECHAIN, current scope chain */
Value rval_; /* if HAS_RVAL, return value of the frame */
ArgumentsObject* argsObj_; /* if HAS_ARGS_OBJ, the call's arguments object */
/*
* Previous frame and its pc and sp. Always nullptr for
* InterpreterActivation's entry frame, always non-nullptr for inline
* frames.
*/
InterpreterFrame* prev_;
jsbytecode* prevpc_;
Value* prevsp_;
void* unused;
/*
* For an eval-in-frame DEBUGGER_EVAL frame, the frame in whose scope
* we're evaluating code. Iteration treats this as our previous frame.
*/
AbstractFramePtr evalInFramePrev_;
Value* argv_; /* If hasArgs(), points to frame's arguments. */
LifoAlloc::Mark mark_; /* Used to release memory for this frame. */
static void staticAsserts() {
JS_STATIC_ASSERT(offsetof(InterpreterFrame, rval_) % sizeof(Value) == 0);
JS_STATIC_ASSERT(sizeof(InterpreterFrame) % sizeof(Value) == 0);
}
/*
* The utilities are private since they are not able to assert that only
* unaliased vars/formals are accessed. Normal code should prefer the
* InterpreterFrame::unaliased* members (or InterpreterRegs::stackDepth for
* the usual "depth is at least" assertions).
*/
Value* slots() const { return (Value*)(this + 1); }
Value* base() const { return slots() + script()->nfixed(); }
friend class FrameIter;
friend class InterpreterRegs;
friend class InterpreterStack;
friend class jit::BaselineFrame;
/*
* Frame initialization, called by InterpreterStack operations after acquiring
* the raw memory for the frame:
*/
/* Used for Invoke and Interpret. */
void initCallFrame(JSContext* cx, InterpreterFrame* prev, jsbytecode* prevpc, Value* prevsp,
JSFunction& callee, JSScript* script, Value* argv, uint32_t nactual,
InterpreterFrame::Flags flags);
/* Used for global and eval frames. */
void initExecuteFrame(JSContext* cx, HandleScript script, AbstractFramePtr prev,
const Value& newTargetValue, HandleObject scopeChain, ExecuteType type);
public:
/*
* Frame prologue/epilogue
*
* Every stack frame must have 'prologue' called before executing the
* first op and 'epilogue' called after executing the last op and before
* popping the frame (whether the exit is exceptional or not).
*
* For inline JS calls/returns, it is easy to call the prologue/epilogue
* exactly once. When calling JS from C++, Invoke/Execute push the stack
* frame but do *not* call the prologue/epilogue. That means Interpret
* must call the prologue/epilogue for the entry frame. This scheme
* simplifies jit compilation.
*
* An important corner case is what happens when an error occurs (OOM,
* over-recursed) after pushing the stack frame but before 'prologue' is
* called or completes fully. To simplify usage, 'epilogue' does not assume
* 'prologue' has completed and handles all the intermediate state details.
*/
bool prologue(JSContext* cx);
void epilogue(JSContext* cx);
bool checkReturn(JSContext* cx, HandleValue thisv);
bool initFunctionScopeObjects(JSContext* cx);
/*
* Initialize local variables of newly-pushed frame. 'var' bindings are
* initialized to undefined and lexical bindings are initialized to
* JS_UNINITIALIZED_LEXICAL.
*/
void initLocals();
/*
* Stack frame type
*
* A stack frame may have one of three types, which determines which
* members of the frame may be accessed and other invariants:
*
* global frame: execution of global code or an eval in global code
* function frame: execution of function code or an eval in a function
* module frame: execution of a module
*/
bool isFunctionFrame() const {
return !!(flags_ & FUNCTION);
}
bool isGlobalOrModuleFrame() const {
return !!(flags_ & GLOBAL_OR_MODULE);
}
bool isGlobalFrame() const {
return isGlobalOrModuleFrame() && !script()->module();
}
bool isModuleFrame() const {
return isGlobalOrModuleFrame() && script()->module();
}
/*
* Eval frames
*
* As noted above, global and function frames may optionally be 'eval
* frames'. Eval code shares its parent's arguments which means that the
* arg-access members of InterpreterFrame may not be used for eval frames.
* Search for 'hasArgs' below for more details.
*
* A further sub-classification of eval frames is whether the frame was
* pushed for an ES5 strict-mode eval().
*/
bool isEvalFrame() const {
return flags_ & EVAL;
}
bool isEvalInFunction() const {
return (flags_ & (EVAL | FUNCTION)) == (EVAL | FUNCTION);
}
bool isNonEvalFunctionFrame() const {
return (flags_ & (FUNCTION | EVAL)) == FUNCTION;
}
inline bool isStrictEvalFrame() const {
return isEvalFrame() && script()->strict();
}
bool isNonStrictEvalFrame() const {
return isEvalFrame() && !script()->strict();
}
bool isNonGlobalEvalFrame() const;
bool isNonStrictDirectEvalFrame() const {
return isNonStrictEvalFrame() && isNonGlobalEvalFrame();
}
/*
* Previous frame
*
* A frame's 'prev' frame is either null or the previous frame pointed to
* by cx->regs->fp when this frame was pushed. Often, given two prev-linked
* frames, the next-frame is a function or eval that was called by the
* prev-frame, but not always: the prev-frame may have called a native that
* reentered the VM through JS_CallFunctionValue on the same context
* (without calling JS_SaveFrameChain) which pushed the next-frame. Thus,
* 'prev' has little semantic meaning and basically just tells the VM what
* to set cx->regs->fp to when this frame is popped.
*/
InterpreterFrame* prev() const {
return prev_;
}
AbstractFramePtr evalInFramePrev() const {
MOZ_ASSERT(isEvalFrame());
return evalInFramePrev_;
}
/*
* (Unaliased) locals and arguments
*
* Only non-eval function frames have arguments. The arguments pushed by
* the caller are the 'actual' arguments. The declared arguments of the
* callee are the 'formal' arguments. When the caller passes less actual
* arguments, missing formal arguments are padded with |undefined|.
*
* When a local/formal variable is "aliased" (accessed by nested closures,
* dynamic scope operations, or 'arguments), the canonical location for
* that value is the slot of an activation object (scope or arguments).
* Currently, aliased locals don't have stack slots assigned to them, but
* all formals are given slots in *both* the stack frame and heap objects,
* even though, as just described, only one should ever be accessed. Thus,
* it is up to the code performing an access to access the correct value.
* These functions assert that accesses to stack values are unaliased.
*/
inline Value& unaliasedLocal(uint32_t i);
bool hasArgs() const { return isNonEvalFunctionFrame(); }
inline Value& unaliasedFormal(unsigned i, MaybeCheckAliasing = CHECK_ALIASING);
inline Value& unaliasedActual(unsigned i, MaybeCheckAliasing = CHECK_ALIASING);
template <class Op> inline void unaliasedForEachActual(Op op);
bool copyRawFrameSlots(AutoValueVector* v);
unsigned numFormalArgs() const { MOZ_ASSERT(hasArgs()); return callee().nargs(); }
unsigned numActualArgs() const { MOZ_ASSERT(hasArgs()); return u.nactual; }
/* Watch out, this exposes a pointer to the unaliased formal arg array. */
Value* argv() const { MOZ_ASSERT(hasArgs()); return argv_; }
/*
* Arguments object
*
* If a non-eval function has script->needsArgsObj, an arguments object is
* created in the prologue and stored in the local variable for the
* 'arguments' binding (script->argumentsLocal). Since this local is
* mutable, the arguments object can be overwritten and we can "lose" the
* arguments object. Thus, InterpreterFrame keeps an explicit argsObj_ field so
* that the original arguments object is always available.
*/
ArgumentsObject& argsObj() const;
void initArgsObj(ArgumentsObject& argsobj);
JSObject* createRestParameter(JSContext* cx);
/*
* Scope chain
*
* In theory, the scope chain would contain an object for every lexical
* scope. However, only objects that are required for dynamic lookup are
* actually created.
*
* Given that an InterpreterFrame corresponds roughly to a ES5 Execution Context
* (ES5 10.3), InterpreterFrame::varObj corresponds to the VariableEnvironment
* component of a Exection Context. Intuitively, the variables object is
* where new bindings (variables and functions) are stored. One might
* expect that this is either the Call object or scopeChain.globalObj for
* function or global code, respectively, however the JSAPI allows calls of
* Execute to specify a variables object on the scope chain other than the
* call/global object. This allows embeddings to run multiple scripts under
* the same global, each time using a new variables object to collect and
* discard the script's global variables.
*/
inline HandleObject scopeChain() const;
inline ScopeObject& aliasedVarScope(ScopeCoordinate sc) const;
inline GlobalObject& global() const;
inline CallObject& callObj() const;
inline JSObject& varObj() const;
inline ClonedBlockObject& extensibleLexicalScope() const;
inline void pushOnScopeChain(ScopeObject& scope);
inline void popOffScopeChain();
inline void replaceInnermostScope(ScopeObject& scope);
/*
* For blocks with aliased locals, these interfaces push and pop entries on
* the scope chain. The "freshen" operation replaces the current block
* with a fresh copy of it, to implement semantics providing distinct
* bindings per iteration of a for-loop.
*/
bool pushBlock(JSContext* cx, StaticBlockObject& block);
void popBlock(JSContext* cx);
bool freshenBlock(JSContext* cx);
/*
* With
*
* Entering/leaving a |with| block pushes/pops an object on the scope chain.
* Pushing uses pushOnScopeChain, popping should use popWith.
*/
void popWith(JSContext* cx);
/*
* Script
*
* All function and global frames have an associated JSScript which holds
* the bytecode being executed for the frame.
*/
JSScript* script() const {
if (isFunctionFrame())
return isEvalFrame() ? u.evalScript : callee().nonLazyScript();
MOZ_ASSERT(isGlobalOrModuleFrame());
return exec.script;
}
/* Return the previous frame's pc. */
jsbytecode* prevpc() {
MOZ_ASSERT(prev_);
return prevpc_;
}
/* Return the previous frame's sp. */
Value* prevsp() {
MOZ_ASSERT(prev_);
return prevsp_;
}
/* Module */
ModuleObject* module() const {
MOZ_ASSERT(isModuleFrame());
return exec.module;
}
ModuleObject* maybeModule() const {
return isModuleFrame() ? module() : nullptr;
}
/*
* Return the 'this' argument passed to a non-eval function frame. This is
* not necessarily the frame's this-binding, for instance non-strict
* functions will box primitive 'this' values and thisArgument() will
* return the original, unboxed Value.
*/
Value& thisArgument() const {
MOZ_ASSERT(isNonEvalFunctionFrame());
return argv()[-1];
}
/*
* Callee
*
* Only function frames have a callee. An eval frame in a function has the
* same callee as its containing function frame.
*/
JSFunction& callee() const {
MOZ_ASSERT(isFunctionFrame());
return calleev().toObject().as<JSFunction>();
}
const Value& calleev() const {
MOZ_ASSERT(isFunctionFrame());
if (isEvalFrame())
return ((const Value*)this)[-1];
return argv()[-2];
}
/*
* New Target
*
* Only function frames have a meaningful newTarget. An eval frame in a
* function will have a copy of the newTarget of the enclosing function
* frame.
*/
Value newTarget() const {
MOZ_ASSERT(isFunctionFrame());
if (isEvalFrame())
return ((Value*)this)[-2];
if (callee().isArrow())
return callee().getExtendedSlot(FunctionExtended::ARROW_NEWTARGET_SLOT);
if (isConstructing()) {
unsigned pushedArgs = Max(numFormalArgs(), numActualArgs());
return argv()[pushedArgs];
}
return UndefinedValue();
}
/* Profiler flags */
bool hasPushedSPSFrame() {
return !!(flags_ & HAS_PUSHED_SPS_FRAME);
}
void setPushedSPSFrame() {
flags_ |= HAS_PUSHED_SPS_FRAME;
}
void unsetPushedSPSFrame() {
flags_ &= ~HAS_PUSHED_SPS_FRAME;
}
/* Return value */
bool hasReturnValue() const {
return flags_ & HAS_RVAL;
}
MutableHandleValue returnValue() {
if (!hasReturnValue())
rval_.setUndefined();
return MutableHandleValue::fromMarkedLocation(&rval_);
}
void markReturnValue() {
flags_ |= HAS_RVAL;
}
void setReturnValue(const Value& v) {
rval_ = v;
markReturnValue();
}
void clearReturnValue() {
rval_.setUndefined();
markReturnValue();
}
void resumeGeneratorFrame(JSObject* scopeChain) {
MOZ_ASSERT(script()->isGenerator());
MOZ_ASSERT(isNonEvalFunctionFrame());
flags_ |= HAS_CALL_OBJ | HAS_SCOPECHAIN;
scopeChain_ = scopeChain;
}
/*
* Other flags
*/
InitialFrameFlags initialFlags() const {
JS_STATIC_ASSERT((int)INITIAL_NONE == 0);
JS_STATIC_ASSERT((int)INITIAL_CONSTRUCT == (int)CONSTRUCTING);
uint32_t mask = CONSTRUCTING;
MOZ_ASSERT((flags_ & mask) != mask);
return InitialFrameFlags(flags_ & mask);
}
void setConstructing() {
flags_ |= CONSTRUCTING;
}
bool isConstructing() const {
return !!(flags_ & CONSTRUCTING);
}
void setResumedGenerator() {
flags_ |= RESUMED_GENERATOR;
}
bool isResumedGenerator() const {
return !!(flags_ & RESUMED_GENERATOR);
}
/*
* These two queries should not be used in general: the presence/absence of
* the call/args object is determined by the static(ish) properties of the
* JSFunction/JSScript. These queries should only be performed when probing
* a stack frame that may be in the middle of the prologue (during which
* time the call/args object are created).
*/
inline bool hasCallObj() const;
bool hasCallObjUnchecked() const {
return flags_ & HAS_CALL_OBJ;
}
bool hasArgsObj() const {
MOZ_ASSERT(script()->needsArgsObj());
return flags_ & HAS_ARGS_OBJ;
}
void setCreateSingleton() {
MOZ_ASSERT(isConstructing());
flags_ |= CREATE_SINGLETON;
}
bool createSingleton() const {
MOZ_ASSERT(isConstructing());
return flags_ & CREATE_SINGLETON;
}
bool isDebuggerEvalFrame() const {
return !!(flags_ & DEBUGGER_EVAL);
}
bool prevUpToDate() const {
return !!(flags_ & PREV_UP_TO_DATE);
}
void setPrevUpToDate() {
flags_ |= PREV_UP_TO_DATE;
}
void unsetPrevUpToDate() {
flags_ &= ~PREV_UP_TO_DATE;
}
bool isDebuggee() const {
return !!(flags_ & DEBUGGEE);
}
void setIsDebuggee() {
flags_ |= DEBUGGEE;
}
inline void unsetIsDebuggee();
bool hasCachedSavedFrame() const {
return flags_ & HAS_CACHED_SAVED_FRAME;
}
void setHasCachedSavedFrame() {
flags_ |= HAS_CACHED_SAVED_FRAME;
}
public:
void mark(JSTracer* trc);
void markValues(JSTracer* trc, unsigned start, unsigned end);
void markValues(JSTracer* trc, Value* sp, jsbytecode* pc);
// Entered Baseline/Ion from the interpreter.
bool runningInJit() const {
return !!(flags_ & RUNNING_IN_JIT);
}
void setRunningInJit() {
flags_ |= RUNNING_IN_JIT;
}
void clearRunningInJit() {
flags_ &= ~RUNNING_IN_JIT;
}
};
static const size_t VALUES_PER_STACK_FRAME = sizeof(InterpreterFrame) / sizeof(Value);
static inline InterpreterFrame::Flags
ToFrameFlags(InitialFrameFlags initial)
{
return InterpreterFrame::Flags(initial);
}
static inline InitialFrameFlags
InitialFrameFlagsFromConstructing(bool b)
{
return b ? INITIAL_CONSTRUCT : INITIAL_NONE;
}
static inline bool
InitialFrameFlagsAreConstructing(InitialFrameFlags initial)
{
return !!(initial & INITIAL_CONSTRUCT);
}
/*****************************************************************************/
class InterpreterRegs
{
public:
Value* sp;
jsbytecode* pc;
private:
InterpreterFrame* fp_;
public:
InterpreterFrame* fp() const { return fp_; }
unsigned stackDepth() const {
MOZ_ASSERT(sp >= fp_->base());
return sp - fp_->base();
}
Value* spForStackDepth(unsigned depth) const {
MOZ_ASSERT(fp_->script()->nfixed() + depth <= fp_->script()->nslots());
return fp_->base() + depth;
}
/* For generators. */
void rebaseFromTo(const InterpreterRegs& from, InterpreterFrame& to) {
fp_ = &to;
sp = to.slots() + (from.sp - from.fp_->slots());
pc = from.pc;
MOZ_ASSERT(fp_);
}
void popInlineFrame() {
pc = fp_->prevpc();
unsigned spForNewTarget = fp_->isResumedGenerator() ? 0 : fp_->isConstructing();
sp = fp_->prevsp() - fp_->numActualArgs() - 1 - spForNewTarget;
fp_ = fp_->prev();
MOZ_ASSERT(fp_);
}
void prepareToRun(InterpreterFrame& fp, JSScript* script) {
pc = script->code();
sp = fp.slots() + script->nfixed();
fp_ = &fp;
}
void setToEndOfScript();
MutableHandleValue stackHandleAt(int i) {
return MutableHandleValue::fromMarkedLocation(&sp[i]);
}
HandleValue stackHandleAt(int i) const {
return HandleValue::fromMarkedLocation(&sp[i]);
}
friend void GDBTestInitInterpreterRegs(InterpreterRegs&, js::InterpreterFrame*,
JS::Value*, uint8_t*);
};
/*****************************************************************************/
class InterpreterStack
{
friend class InterpreterActivation;
static const size_t DEFAULT_CHUNK_SIZE = 4 * 1024;
LifoAlloc allocator_;
// Number of interpreter frames on the stack, for over-recursion checks.
static const size_t MAX_FRAMES = 50 * 1000;
static const size_t MAX_FRAMES_TRUSTED = MAX_FRAMES + 1000;
size_t frameCount_;
inline uint8_t* allocateFrame(JSContext* cx, size_t size);
inline InterpreterFrame*
getCallFrame(JSContext* cx, const CallArgs& args, HandleScript script,
InterpreterFrame::Flags* pflags, Value** pargv);
void releaseFrame(InterpreterFrame* fp) {
frameCount_--;
allocator_.release(fp->mark_);
}
public:
InterpreterStack()
: allocator_(DEFAULT_CHUNK_SIZE),
frameCount_(0)
{ }
~InterpreterStack() {
MOZ_ASSERT(frameCount_ == 0);
}