Debian Codegraph @

js/src/vm/FrameIter.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "vm/FrameIter-inl.h"
8
9 #include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_CRASH
10 #include "mozilla/MaybeOneOf.h" // mozilla::MaybeOneOf
11
12 #include <stddef.h> // size_t
13 #include <stdint.h> // uint8_t, uint32_t
14 #include <stdlib.h> // getenv
15
16 #include "jit/BaselineFrame.h" // js::jit::BaselineFrame
17 #include "jit/JitFrames.h" // js::jit::EnsureUnwoundJitExitFrame
18 #include "jit/JSJitFrameIter.h" // js::jit::{FrameType,InlineFrameIterator,JSJitFrameIter,MaybeReadFallback,SnapshotIterator}
19 #include "js/ColumnNumber.h" // JS::LimitedColumnNumberOneOrigin, JS::TaggedColumnNumberOneOrigin
20 #include "js/GCAPI.h" // JS::AutoSuppressGCAnalysis
21 #include "js/Principals.h" // JSSubsumesOp
22 #include "js/RootingAPI.h" // JS::Rooted
23 #include "vm/Activation.h" // js::Activation{,Iterator}
24 #include "vm/EnvironmentObject.h" // js::CallObject
25 #include "vm/JitActivation.h" // js::jit::JitActivation
26 #include "vm/JSContext.h" // JSContext
27 #include "vm/JSFunction.h" // JSFunction
28 #include "vm/JSScript.h" // js::PCToLineNumber, JSScript, js::ScriptSource
29 #include "vm/Runtime.h" // JSRuntime
30 #include "vm/Stack.h" // js::{AbstractFramePtr,InterpreterFrame,MaybeCheckAliasing}
31 #include "wasm/WasmFrameIter.h" // js::wasm::WasmFrameIter
32 #include "wasm/WasmInstance.h" // js::wasm::Instance
33
34 #include "jit/JSJitFrameIter-inl.h" // js::jit::JSJitFrameIter::baselineFrame{,NumValueSlots}
35 #include "vm/Stack-inl.h" // js::AbstractFramePtr::*
36
37 namespace JS {
38 class JS_PUBLIC_API Realm;
39 } // namespace JS
40
41 namespace js {
42 class ArgumentsObject;
43 } // namespace js
44
45 using JS::Realm;
46 using JS::Rooted;
47 using JS::Value;
48
49 using js::AbstractFramePtr;
50 using js::ArgumentsObject;
51 using js::CallObject;
52 using js::FrameIter;
53 using js::JitFrameIter;
54 using js::NonBuiltinFrameIter;
55 using js::NonBuiltinScriptFrameIter;
56 using js::OnlyJSJitFrameIter;
57 using js::ScriptSource;
58 using js::jit::JSJitFrameIter;
59
60 JitFrameIter::JitFrameIter(const JitFrameIter& another) { *this = another; }
61
62 JitFrameIter& JitFrameIter::operator=(const JitFrameIter& another) {
63 MOZ_ASSERT(this != &another);
64
65 act_ = another.act_;
66 mustUnwindActivation_ = another.mustUnwindActivation_;
67
68 if (isSome()) {
69 iter_.destroy();
70 }
71 if (!another.isSome()) {
72 return *this;
73 }
74
75 if (another.isJSJit()) {
76 iter_.construct<jit::JSJitFrameIter>(another.asJSJit());
77 } else {
78 MOZ_ASSERT(another.isWasm());
79 iter_.construct<wasm::WasmFrameIter>(another.asWasm());
80 }
81
82 return *this;
83 }
84
85 JitFrameIter::JitFrameIter(jit::JitActivation* act, bool mustUnwindActivation) {
86 act_ = act;
87 mustUnwindActivation_ = mustUnwindActivation;
88 MOZ_ASSERT(act->hasExitFP(),
89 "packedExitFP is used to determine if JSJit or wasm");
90 if (act->hasJSExitFP()) {
91 iter_.construct<jit::JSJitFrameIter>(act);
92 } else {
93 MOZ_ASSERT(act->hasWasmExitFP());
94 iter_.construct<wasm::WasmFrameIter>(act);
95 }
96 settle();
97 }
98
99 void JitFrameIter::skipNonScriptedJSFrames() {
100 if (isJSJit()) {
101 // Stop at the first scripted frame.
102 jit::JSJitFrameIter& frames = asJSJit();
103 while (!frames.isScripted() && !frames.done()) {
104 ++frames;
105 }
106 settle();
107 }
108 }
109
110 bool JitFrameIter::isSelfHostedIgnoringInlining() const {
111 MOZ_ASSERT(!done());
112
113 if (isWasm()) {
114 return false;
115 }
116
117 return asJSJit().script()->selfHosted();
118 }
119
120 JS::Realm* JitFrameIter::realm() const {
121 MOZ_ASSERT(!done());
122
123 if (isWasm()) {
124 return asWasm().instance()->realm();
125 }
126
127 if (asJSJit().isScripted()) {
128 return asJSJit().script()->realm();
129 }
130
131 MOZ_RELEASE_ASSERT(asJSJit().isTrampolineNative());
132 return asJSJit().callee()->realm();
133 }
134
135 uint8_t* JitFrameIter::resumePCinCurrentFrame() const {
136 if (isWasm()) {
137 return asWasm().resumePCinCurrentFrame();
138 }
139 return asJSJit().resumePCinCurrentFrame();
140 }
141
142 bool JitFrameIter::done() const {
143 if (!isSome()) {
144 return true;
145 }
146 if (isJSJit()) {
147 return asJSJit().done();
148 }
149 if (isWasm()) {
150 return asWasm().done();
151 }
152 MOZ_CRASH("unhandled case");
153 }
154
155 void JitFrameIter::settle() {
156 if (isJSJit()) {
157 const jit::JSJitFrameIter& jitFrame = asJSJit();
158 if (jitFrame.type() != jit::FrameType::WasmToJSJit) {
159 return;
160 }
161
162 // Transition from js jit frames to wasm frames: we're on the
163 // wasm-to-jit fast path. The current stack layout is as follows:
164 // (stack grows downward)
165 //
166 // [--------------------]
167 // [WASM FUNC ]
168 // [WASM JIT EXIT FRAME ]
169 // [JIT WASM ENTRY FRAME] <-- we're here.
170 //
171 // So prevFP points to the wasm jit exit FP, maintaing the invariant in
172 // WasmFrameIter that the first frame is an exit frame and can be
173 // popped.
174
175 wasm::Frame* prevFP = (wasm::Frame*)jitFrame.prevFp();
176
177 if (mustUnwindActivation_) {
178 act_->setWasmExitFP(prevFP);
179 }
180
181 iter_.destroy();
182 iter_.construct<wasm::WasmFrameIter>(act_, prevFP);
183 MOZ_ASSERT(!asWasm().done());
184 return;
185 }
186
187 if (isWasm()) {
188 const wasm::WasmFrameIter& wasmFrame = asWasm();
189 if (!wasmFrame.done() || !wasmFrame.unwoundCallerFPIsJSJit()) {
190 return;
191 }
192
193 // Transition from wasm frames to jit frames: we're on the
194 // jit-to-wasm fast path. The current stack layout is as follows:
195 // (stack grows downward)
196 //
197 // [--------------------]
198 // [JIT FRAME ]
199 // [WASM JIT ENTRY FRAME] <-- we're here
200 //
201 // The wasm iterator has saved the previous jit frame pointer for us.
202
203 uint8_t* prevFP = wasmFrame.unwoundCallerFP();
204
205 if (mustUnwindActivation_) {
206 act_->setJSExitFP(prevFP);
207 }
208
209 iter_.destroy();
210 iter_.construct<jit::JSJitFrameIter>(act_, prevFP, mustUnwindActivation_);
211 MOZ_ASSERT(!asJSJit().done());
212 return;
213 }
214 }
215
216 void JitFrameIter::operator++() {
217 MOZ_ASSERT(isSome());
218 if (isJSJit()) {
219 const jit::JSJitFrameIter& jitFrame = asJSJit();
220
221 jit::JitFrameLayout* prevFrame = nullptr;
222 if (mustUnwindActivation_ && jitFrame.isScripted()) {
223 prevFrame = jitFrame.jsFrame();
224 }
225
226 ++asJSJit();
227
228 if (prevFrame) {
229 // Unwind the frame by updating packedExitFP. This is necessary
230 // so that (1) debugger exception unwind and leave frame hooks
231 // don't see this frame when they use ScriptFrameIter, and (2)
232 // ScriptFrameIter does not crash when accessing an IonScript
233 // that's destroyed by the ionScript->decref call.
234 EnsureUnwoundJitExitFrame(act_, prevFrame);
235 }
236 } else if (isWasm()) {
237 ++asWasm();
238 } else {
239 MOZ_CRASH("unhandled case");
240 }
241 settle();
242 }
243
244 OnlyJSJitFrameIter::OnlyJSJitFrameIter(jit::JitActivation* act)
245 : JitFrameIter(act) {
246 settle();
247 }
248
249 OnlyJSJitFrameIter::OnlyJSJitFrameIter(const ActivationIterator& iter)
250 : OnlyJSJitFrameIter(iter->asJit()) {}
251
252 /*****************************************************************************/
253
254 void FrameIter::popActivation() {
255 ++data_.activations_;
256 settleOnActivation();
257 }
258
259 bool FrameIter::principalsSubsumeFrame() const {
260 // If the caller supplied principals, only show frames which are
261 // subsumed (of the same origin or of an origin accessible) by these
262 // principals.
263
264 MOZ_ASSERT(!done());
265
266 if (!data_.principals_) {
267 return true;
268 }
269
270 JSSubsumesOp subsumes = data_.cx_->runtime()->securityCallbacks->subsumes;
271 if (!subsumes) {
272 return true;
273 }
274
275 JS::AutoSuppressGCAnalysis nogc;
276 return subsumes(data_.principals_, realm()->principals());
277 }
278
279 void FrameIter::popInterpreterFrame() {
280 MOZ_ASSERT(data_.state_ == INTERP);
281
282 ++data_.interpFrames_;
283
284 if (data_.interpFrames_.done()) {
285 popActivation();
286 } else {
287 data_.pc_ = data_.interpFrames_.pc();
288 }
289 }
290
291 void FrameIter::settleOnActivation() {
292 MOZ_ASSERT(!data_.cx_->inUnsafeCallWithABI);
293
294 while (true) {
295 if (data_.activations_.done()) {
296 data_.state_ = DONE;
297 return;
298 }
299
300 Activation* activation = data_.activations_.activation();
301
302 if (activation->isJit()) {
303 data_.jitFrames_ = JitFrameIter(activation->asJit());
304 data_.jitFrames_.skipNonScriptedJSFrames();
305 if (data_.jitFrames_.done()) {
306 // It's possible to have an JitActivation with no scripted
307 // frames, for instance if we hit an over-recursion during
308 // bailout.
309 ++data_.activations_;
310 continue;
311 }
312 data_.state_ = JIT;
313 nextJitFrame();
314 return;
315 }
316
317 MOZ_ASSERT(activation->isInterpreter());
318
319 InterpreterActivation* interpAct = activation->asInterpreter();
320 data_.interpFrames_ = InterpreterFrameIterator(interpAct);
321
322 // If we OSR'ed into JIT code, skip the interpreter frame so that
323 // the same frame is not reported twice.
324 if (data_.interpFrames_.frame()->runningInJit()) {
325 ++data_.interpFrames_;
326 if (data_.interpFrames_.done()) {
327 ++data_.activations_;
328 continue;
329 }
330 }
331
332 MOZ_ASSERT(!data_.interpFrames_.frame()->runningInJit());
333 data_.pc_ = data_.interpFrames_.pc();
334 data_.state_ = INTERP;
335 return;
336 }
337 }
338
339 FrameIter::Data::Data(JSContext* cx, DebuggerEvalOption debuggerEvalOption,
340 JSPrincipals* principals)
341 : cx_(cx),
342 debuggerEvalOption_(debuggerEvalOption),
343 principals_(principals),
344 state_(DONE),
345 pc_(nullptr),
346 interpFrames_(nullptr),
347 activations_(cx),
348 ionInlineFrameNo_(0) {}
349
350 FrameIter::Data::Data(const FrameIter::Data& other) = default;
351
352 FrameIter::FrameIter(JSContext* cx, DebuggerEvalOption debuggerEvalOption)
353 : data_(cx, debuggerEvalOption, nullptr),
354 ionInlineFrames_(cx, (js::jit::JSJitFrameIter*)nullptr) {
355 settleOnActivation();
356
357 // No principals so we can see all frames.
358 MOZ_ASSERT_IF(!done(), principalsSubsumeFrame());
359 }
360
361 FrameIter::FrameIter(JSContext* cx, DebuggerEvalOption debuggerEvalOption,
362 JSPrincipals* principals)
363 : data_(cx, debuggerEvalOption, principals),
364 ionInlineFrames_(cx, (js::jit::JSJitFrameIter*)nullptr) {
365 settleOnActivation();
366
367 // If we're not allowed to see this frame, call operator++ to skip this (and
368 // other) cross-origin frames.
369 if (!done() && !principalsSubsumeFrame()) {
370 ++*this;
371 }
372 }
373
374 FrameIter::FrameIter(const FrameIter& other)
375 : data_(other.data_),
376 ionInlineFrames_(other.data_.cx_,
377 isIonScripted() ? &other.ionInlineFrames_ : nullptr) {}
378
379 FrameIter::FrameIter(const Data& data)
380 : data_(data),
381 ionInlineFrames_(data.cx_, isIonScripted() ? &jsJitFrame() : nullptr) {
382 MOZ_ASSERT(data.cx_);
383 if (isIonScripted()) {
384 while (ionInlineFrames_.frameNo() != data.ionInlineFrameNo_) {
385 ++ionInlineFrames_;
386 }
387 }
388 }
389
390 void FrameIter::nextJitFrame() {
391 MOZ_ASSERT(data_.jitFrames_.isSome());
392
393 if (isJSJit()) {
394 if (jsJitFrame().isIonScripted()) {
395 ionInlineFrames_.resetOn(&jsJitFrame());
396 data_.pc_ = ionInlineFrames_.pc();
397 } else {
398 MOZ_ASSERT(jsJitFrame().isBaselineJS());
399 jsJitFrame().baselineScriptAndPc(nullptr, &data_.pc_);
400 }
401 return;
402 }
403
404 MOZ_ASSERT(isWasm());
405 wasmFrame().enableInlinedFrames();
406 data_.pc_ = nullptr;
407 }
408
409 void FrameIter::popJitFrame() {
410 MOZ_ASSERT(data_.state_ == JIT);
411 MOZ_ASSERT(data_.jitFrames_.isSome());
412
413 if (isJSJit() && jsJitFrame().isIonScripted() && ionInlineFrames_.more()) {
414 ++ionInlineFrames_;
415 data_.pc_ = ionInlineFrames_.pc();
416 return;
417 }
418
419 ++data_.jitFrames_;
420 data_.jitFrames_.skipNonScriptedJSFrames();
421
422 if (!data_.jitFrames_.done()) {
423 nextJitFrame();
424 } else {
425 data_.jitFrames_.reset();
426 popActivation();
427 }
428 }
429
430 FrameIter& FrameIter::operator++() {
431 while (true) {
432 switch (data_.state_) {
433 case DONE:
434 MOZ_CRASH("Unexpected state");
435 case INTERP:
436 if (interpFrame()->isDebuggerEvalFrame() &&
437 data_.debuggerEvalOption_ == FOLLOW_DEBUGGER_EVAL_PREV_LINK) {
438 AbstractFramePtr eifPrev = interpFrame()->evalInFramePrev();
439
440 popInterpreterFrame();
441
442 while (!hasUsableAbstractFramePtr() ||
443 abstractFramePtr() != eifPrev) {
444 if (data_.state_ == JIT) {
445 popJitFrame();
446 } else {
447 popInterpreterFrame();
448 }
449 }
450
451 break;
452 }
453 popInterpreterFrame();
454 break;
455 case JIT:
456 popJitFrame();
457 break;
458 }
459
460 if (done() || principalsSubsumeFrame()) {
461 break;
462 }
463 }
464
465 return *this;
466 }
467
468 FrameIter::Data* FrameIter::copyData() const {
469 Data* data = data_.cx_->new_<Data>(data_);
470 if (!data) {
471 return nullptr;
472 }
473
474 if (data && isIonScripted()) {
475 data->ionInlineFrameNo_ = ionInlineFrames_.frameNo();
476 }
477 return data;
478 }
479
480 void* FrameIter::rawFramePtr() const {
481 switch (data_.state_) {
482 case DONE:
483 return nullptr;
484 case INTERP:
485 return interpFrame();
486 case JIT:
487 if (isJSJit()) {
488 return jsJitFrame().fp();
489 }
490 MOZ_ASSERT(isWasm());
491 return nullptr;
492 }
493 MOZ_CRASH("Unexpected state");
494 }
495
496 JS::Compartment* FrameIter::compartment() const {
497 switch (data_.state_) {
498 case DONE:
499 break;
500 case INTERP:
501 case JIT:
502 return data_.activations_->compartment();
503 }
504 MOZ_CRASH("Unexpected state");
505 }
506
507 Realm* FrameIter::realm() const {
508 MOZ_ASSERT(!done());
509
510 if (hasScript()) {
511 return script()->realm();
512 }
513
514 return wasmInstance()->realm();
515 }
516
517 bool FrameIter::isEvalFrame() const {
518 switch (data_.state_) {
519 case DONE:
520 break;
521 case INTERP:
522 return interpFrame()->isEvalFrame();
523 case JIT:
524 if (isJSJit()) {
525 if (jsJitFrame().isBaselineJS()) {
526 return jsJitFrame().baselineFrame()->isEvalFrame();
527 }
528 MOZ_ASSERT(!script()->isForEval());
529 return false;
530 }
531 MOZ_ASSERT(isWasm());
532 return false;
533 }
534 MOZ_CRASH("Unexpected state");
535 }
536
537 bool FrameIter::isModuleFrame() const {
538 MOZ_ASSERT(!done());
539
540 if (hasScript()) {
541 return script()->isModule();
542 }
543 MOZ_CRASH("Unexpected state");
544 }
545
546 bool FrameIter::isFunctionFrame() const {
547 MOZ_ASSERT(!done());
548 switch (data_.state_) {
549 case DONE:
550 break;
551 case INTERP:
552 return interpFrame()->isFunctionFrame();
553 case JIT:
554 if (isJSJit()) {
555 if (jsJitFrame().isBaselineJS()) {
556 return jsJitFrame().baselineFrame()->isFunctionFrame();
557 }
558 return script()->isFunction();
559 }
560 MOZ_ASSERT(isWasm());
561 return false;
562 }
563 MOZ_CRASH("Unexpected state");
564 }
565
566 JSAtom* FrameIter::maybeFunctionDisplayAtom() const {
567 switch (data_.state_) {
568 case DONE:
569 break;
570 case INTERP:
571 case JIT:
572 if (isWasm()) {
573 return wasmFrame().functionDisplayAtom();
574 }
575 if (isFunctionFrame()) {
576 return calleeTemplate()->fullDisplayAtom();
577 }
578 return nullptr;
579 }
580
581 MOZ_CRASH("Unexpected state");
582 }
583
584 ScriptSource* FrameIter::scriptSource() const {
585 switch (data_.state_) {
586 case DONE:
587 break;
588 case INTERP:
589 case JIT:
590 return script()->scriptSource();
591 }
592
593 MOZ_CRASH("Unexpected state");
594 }
595
596 const char* FrameIter::filename() const {
597 switch (data_.state_) {
598 case DONE:
599 break;
600 case INTERP:
601 case JIT:
602 if (isWasm()) {
603 return wasmFrame().filename();
604 }
605 return script()->filename();
606 }
607
608 MOZ_CRASH("Unexpected state");
609 }
610
611 const char16_t* FrameIter::displayURL() const {
612 switch (data_.state_) {
613 case DONE:
614 break;
615 case INTERP:
616 case JIT:
617 if (isWasm()) {
618 return wasmFrame().displayURL();
619 }
620 ScriptSource* ss = script()->scriptSource();
621 return ss->hasDisplayURL() ? ss->displayURL() : nullptr;
622 }
623 MOZ_CRASH("Unexpected state");
624 }
625
626 unsigned FrameIter::computeLine(JS::TaggedColumnNumberOneOrigin* column) const {
627 switch (data_.state_) {
628 case DONE:
629 break;
630 case INTERP:
631 case JIT:
632 if (isWasm()) {
633 return wasmFrame().computeLine(column);
634 }
635 JS::LimitedColumnNumberOneOrigin columnNumber;
636 unsigned lineNumber = PCToLineNumber(script(), pc(), &columnNumber);
637 if (column) {
638 *column = JS::TaggedColumnNumberOneOrigin(columnNumber);
639 }
640 return lineNumber;
641 }
642
643 MOZ_CRASH("Unexpected state");
644 }
645
646 bool FrameIter::mutedErrors() const {
647 switch (data_.state_) {
648 case DONE:
649 break;
650 case INTERP:
651 case JIT:
652 if (isWasm()) {
653 return wasmFrame().mutedErrors();
654 }
655 return script()->mutedErrors();
656 }
657 MOZ_CRASH("Unexpected state");
658 }
659
660 bool FrameIter::isConstructing() const {
661 switch (data_.state_) {
662 case DONE:
663 break;
664 case JIT:
665 MOZ_ASSERT(isJSJit());
666 if (jsJitFrame().isIonScripted()) {
667 return ionInlineFrames_.isConstructing();
668 }
669 MOZ_ASSERT(jsJitFrame().isBaselineJS());
670 return jsJitFrame().isConstructing();
671 case INTERP:
672 return interpFrame()->isConstructing();
673 }
674
675 MOZ_CRASH("Unexpected state");
676 }
677
678 bool FrameIter::ensureHasRematerializedFrame(JSContext* cx) {
679 MOZ_ASSERT(isIon());
680 return !!activation()->asJit()->getRematerializedFrame(cx, jsJitFrame());
681 }
682
683 bool FrameIter::hasUsableAbstractFramePtr() const {
684 switch (data_.state_) {
685 case DONE:
686 return false;
687 case JIT:
688 if (isJSJit()) {
689 if (jsJitFrame().isBaselineJS()) {
690 return true;
691 }
692
693 MOZ_ASSERT(jsJitFrame().isIonScripted());
694 return !!activation()->asJit()->lookupRematerializedFrame(
695 jsJitFrame().fp(), ionInlineFrames_.frameNo());
696 }
697 MOZ_ASSERT(isWasm());
698 return wasmFrame().debugEnabled();
699 case INTERP:
700 return true;
701 }
702 MOZ_CRASH("Unexpected state");
703 }
704
705 AbstractFramePtr FrameIter::abstractFramePtr() const {
706 MOZ_ASSERT(hasUsableAbstractFramePtr());
707 switch (data_.state_) {
708 case DONE:
709 break;
710 case JIT: {
711 if (isJSJit()) {
712 if (jsJitFrame().isBaselineJS()) {
713 return jsJitFrame().baselineFrame();
714 }
715 MOZ_ASSERT(isIonScripted());
716 return activation()->asJit()->lookupRematerializedFrame(
717 jsJitFrame().fp(), ionInlineFrames_.frameNo());
718 }
719 MOZ_ASSERT(isWasm());
720 MOZ_ASSERT(wasmFrame().debugEnabled());
721 return wasmFrame().debugFrame();
722 }
723 case INTERP:
724 MOZ_ASSERT(interpFrame());
725 return AbstractFramePtr(interpFrame());
726 }
727 MOZ_CRASH("Unexpected state");
728 }
729
730 void FrameIter::updatePcQuadratic() {
731 switch (data_.state_) {
732 case DONE:
733 break;
734 case INTERP: {
735 InterpreterFrame* frame = interpFrame();
736 InterpreterActivation* activation = data_.activations_->asInterpreter();
737
738 // Look for the current frame.
739 data_.interpFrames_ = InterpreterFrameIterator(activation);
740 while (data_.interpFrames_.frame() != frame) {
741 ++data_.interpFrames_;
742 }
743
744 // Update the pc.
745 MOZ_ASSERT(data_.interpFrames_.frame() == frame);
746 data_.pc_ = data_.interpFrames_.pc();
747 return;
748 }
749 case JIT:
750 if (jsJitFrame().isBaselineJS()) {
751 jit::BaselineFrame* frame = jsJitFrame().baselineFrame();
752 jit::JitActivation* activation = data_.activations_->asJit();
753
754 // activation's exitFP may be invalid, so create a new
755 // activation iterator.
756 data_.activations_ = ActivationIterator(data_.cx_);
757 while (data_.activations_.activation() != activation) {
758 ++data_.activations_;
759 }
760
761 // Look for the current frame.
762 data_.jitFrames_ = JitFrameIter(data_.activations_->asJit());
763 while (!isJSJit() || !jsJitFrame().isBaselineJS() ||
764 jsJitFrame().baselineFrame() != frame) {
765 ++data_.jitFrames_;
766 }
767
768 // Update the pc.
769 MOZ_ASSERT(jsJitFrame().baselineFrame() == frame);
770 jsJitFrame().baselineScriptAndPc(nullptr, &data_.pc_);
771 return;
772 }
773 break;
774 }
775 MOZ_CRASH("Unexpected state");
776 }
777
778 void FrameIter::wasmUpdateBytecodeOffset() {
779 MOZ_RELEASE_ASSERT(isWasm(), "Unexpected state");
780
781 wasm::DebugFrame* frame = wasmFrame().debugFrame();
782
783 // Relookup the current frame, updating the bytecode offset in the process.
784 data_.jitFrames_ = JitFrameIter(data_.activations_->asJit());
785 while (!isWasm() || wasmFrame().debugFrame() != frame) {
786 ++data_.jitFrames_;
787 }
788
789 MOZ_ASSERT(wasmFrame().debugFrame() == frame);
790 }
791
792 JSFunction* FrameIter::calleeTemplate() const {
793 switch (data_.state_) {
794 case DONE:
795 break;
796 case INTERP:
797 MOZ_ASSERT(isFunctionFrame());
798 return &interpFrame()->callee();
799 case JIT:
800 if (jsJitFrame().isBaselineJS()) {
801 return jsJitFrame().callee();
802 }
803 MOZ_ASSERT(jsJitFrame().isIonScripted());
804 return ionInlineFrames_.calleeTemplate();
805 }
806 MOZ_CRASH("Unexpected state");
807 }
808
809 JSFunction* FrameIter::callee(JSContext* cx) const {
810 switch (data_.state_) {
811 case DONE:
812 break;
813 case INTERP:
814 return calleeTemplate();
815 case JIT:
816 if (isIonScripted()) {
817 jit::MaybeReadFallback recover(cx, activation()->asJit(),
818 &jsJitFrame());
819 return ionInlineFrames_.callee(recover);
820 }
821 MOZ_ASSERT(jsJitFrame().isBaselineJS());
822 return calleeTemplate();
823 }
824 MOZ_CRASH("Unexpected state");
825 }
826
827 bool FrameIter::matchCallee(JSContext* cx, JS::Handle<JSFunction*> fun) const {
828 // Use the calleeTemplate to rule out a match without needing to invalidate to
829 // find the actual callee. The real callee my be a clone of the template which
830 // should *not* be considered a match.
831 Rooted<JSFunction*> currentCallee(cx, calleeTemplate());
832
833 if (currentCallee->nargs() != fun->nargs()) {
834 return false;
835 }
836
837 if (currentCallee->flags().stableAcrossClones() !=
838 fun->flags().stableAcrossClones()) {
839 return false;
840 }
841
842 // The calleeTemplate for a callee will always have the same BaseScript. If
843 // the script clones do not use the same script, they also have a different
844 // group and Ion will not inline them interchangeably.
845 //
846 // See: js::jit::InlineFrameIterator::findNextFrame()
847 if (currentCallee->hasBaseScript() && fun->hasBaseScript()) {
848 if (currentCallee->baseScript() != fun->baseScript()) {
849 return false;
850 }
851 }
852
853 return callee(cx) == fun;
854 }
855
856 unsigned FrameIter::numActualArgs() const {
857 switch (data_.state_) {
858 case DONE:
859 break;
860 case INTERP:
861 MOZ_ASSERT(isFunctionFrame());
862 return interpFrame()->numActualArgs();
863 case JIT:
864 if (isIonScripted()) {
865 return ionInlineFrames_.numActualArgs();
866 }
867 MOZ_ASSERT(jsJitFrame().isBaselineJS());
868 return jsJitFrame().numActualArgs();
869 }
870 MOZ_CRASH("Unexpected state");
871 }
872
873 unsigned FrameIter::numFormalArgs() const {
874 return script()->function()->nargs();
875 }
876
877 Value FrameIter::unaliasedActual(unsigned i,
878 MaybeCheckAliasing checkAliasing) const {
879 return abstractFramePtr().unaliasedActual(i, checkAliasing);
880 }
881
882 JSObject* FrameIter::environmentChain(JSContext* cx) const {
883 switch (data_.state_) {
884 case DONE:
885 break;
886 case JIT:
887 if (isJSJit()) {
888 if (isIonScripted()) {
889 jit::MaybeReadFallback recover(cx, activation()->asJit(),
890 &jsJitFrame());
891 return ionInlineFrames_.environmentChain(recover);
892 }
893 return jsJitFrame().baselineFrame()->environmentChain();
894 }
895 MOZ_ASSERT(isWasm());
896 return wasmFrame().debugFrame()->environmentChain();
897 case INTERP:
898 return interpFrame()->environmentChain();
899 }
900 MOZ_CRASH("Unexpected state");
901 }
902
903 bool FrameIter::hasInitialEnvironment(JSContext* cx) const {
904 if (hasUsableAbstractFramePtr()) {
905 return abstractFramePtr().hasInitialEnvironment();
906 }
907
908 if (isWasm()) {
909 // See JSFunction::needsFunctionEnvironmentObjects().
910 return false;
911 }
912
913 MOZ_ASSERT(isJSJit());
914 MOZ_ASSERT(isIonScripted());
915
916 bool hasInitialEnv = false;
917 jit::MaybeReadFallback recover(cx, activation()->asJit(), &jsJitFrame());
918 ionInlineFrames_.environmentChain(recover, &hasInitialEnv);
919
920 return hasInitialEnv;
921 }
922
923 CallObject& FrameIter::callObj(JSContext* cx) const {
924 MOZ_ASSERT(calleeTemplate()->needsCallObject());
925 MOZ_ASSERT(hasInitialEnvironment(cx));
926
927 JSObject* pobj = environmentChain(cx);
928 while (!pobj->is<CallObject>()) {
929 pobj = pobj->enclosingEnvironment();
930 }
931 return pobj->as<CallObject>();
932 }
933
934 bool FrameIter::hasArgsObj() const { return abstractFramePtr().hasArgsObj(); }
935
936 ArgumentsObject& FrameIter::argsObj() const {
937 MOZ_ASSERT(hasArgsObj());
938 return abstractFramePtr().argsObj();
939 }
940
941 Value FrameIter::thisArgument(JSContext* cx) const {
942 MOZ_ASSERT(isFunctionFrame());
943
944 switch (data_.state_) {
945 case DONE:
946 break;
947 case JIT:
948 if (isIonScripted()) {
949 jit::MaybeReadFallback recover(cx, activation()->asJit(),
950 &jsJitFrame());
951 return ionInlineFrames_.thisArgument(recover);
952 }
953 return jsJitFrame().baselineFrame()->thisArgument();
954 case INTERP:
955 return interpFrame()->thisArgument();
956 }
957 MOZ_CRASH("Unexpected state");
958 }
959
960 Value FrameIter::returnValue() const {
961 switch (data_.state_) {
962 case DONE:
963 break;
964 case JIT:
965 if (jsJitFrame().isBaselineJS()) {
966 return jsJitFrame().baselineFrame()->returnValue();
967 }
968 break;
969 case INTERP:
970 return interpFrame()->returnValue();
971 }
972 MOZ_CRASH("Unexpected state");
973 }
974
975 void FrameIter::setReturnValue(const Value& v) {
976 switch (data_.state_) {
977 case DONE:
978 break;
979 case JIT:
980 if (jsJitFrame().isBaselineJS()) {
981 jsJitFrame().baselineFrame()->setReturnValue(v);
982 return;
983 }
984 break;
985 case INTERP:
986 interpFrame()->setReturnValue(v);
987 return;
988 }
989 MOZ_CRASH("Unexpected state");
990 }
991
992 size_t FrameIter::numFrameSlots() const {
993 switch (data_.state_) {
994 case DONE:
995 break;
996 case JIT: {
997 if (isIonScripted()) {
998 return ionInlineFrames_.snapshotIterator().numAllocations() -
999 ionInlineFrames_.script()->nfixed();
1000 }
1001 uint32_t numValueSlots = jsJitFrame().baselineFrameNumValueSlots();
1002 return numValueSlots - jsJitFrame().script()->nfixed();
1003 }
1004 case INTERP:
1005 MOZ_ASSERT(data_.interpFrames_.sp() >= interpFrame()->base());
1006 return data_.interpFrames_.sp() - interpFrame()->base();
1007 }
1008 MOZ_CRASH("Unexpected state");
1009 }
1010
1011 Value FrameIter::frameSlotValue(size_t index) const {
1012 switch (data_.state_) {
1013 case DONE:
1014 break;
1015 case JIT:
1016 if (isIonScripted()) {
1017 jit::SnapshotIterator si(ionInlineFrames_.snapshotIterator());
1018 index += ionInlineFrames_.script()->nfixed();
1019 return si.maybeReadAllocByIndex(index);
1020 }
1021 index += jsJitFrame().script()->nfixed();
1022 return *jsJitFrame().baselineFrame()->valueSlot(index);
1023 case INTERP:
1024 return interpFrame()->base()[index];
1025 }
1026 MOZ_CRASH("Unexpected state");
1027 }
1028
1029 #ifdef DEBUG
1030 bool js::SelfHostedFramesVisible() {
1031 static bool checked = false;
1032 static bool visible = false;
1033 if (!checked) {
1034 checked = true;
1035 char* env = getenv("MOZ_SHOW_ALL_JS_FRAMES");
1036 visible = !!env;
1037 }
1038 return visible;
1039 }
1040 #endif
1041
1042 void NonBuiltinFrameIter::settle() {
1043 if (!SelfHostedFramesVisible()) {
1044 while (!done() && hasScript() && script()->selfHosted()) {
1045 FrameIter::operator++();
1046 }
1047 }
1048 }
1049
1050 void NonBuiltinScriptFrameIter::settle() {
1051 if (!SelfHostedFramesVisible()) {
1052 while (!done() && script()->selfHosted()) {
1053 ScriptFrameIter::operator++();
1054 }
1055 }
1056 }
1057
1058 bool FrameIter::inPrologue() const {
1059 if (pc() < script()->main()) {
1060 return true;
1061 }
1062 // If we do a VM call before pushing locals in baseline, the stack frame will
1063 // not include space for those locals.
1064 if (pc() == script()->code() && isBaseline() &&
1065 jsJitFrame().baselineFrameNumValueSlots() < script()->nfixed()) {
1066 return true;
1067 }
1068
1069 return false;
1070 }