Debian Codegraph @

js/src/jit/JSJitFrameIter.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 "jit/JSJitFrameIter-inl.h"
8
9 #include "jit/CalleeToken.h"
10 #include "jit/IonScript.h"
11 #include "jit/JitcodeMap.h"
12 #include "jit/JitFrames.h"
13 #include "jit/JitRuntime.h"
14 #include "jit/JitScript.h"
15 #include "jit/MacroAssembler.h" // js::jit::Assembler::GetPointer
16 #include "jit/SafepointIndex.h"
17 #include "jit/Safepoints.h"
18 #include "jit/ScriptFromCalleeToken.h"
19 #include "jit/VMFunctions.h"
20 #include "js/friend/DumpFunctions.h" // js::DumpObject, js::DumpValue
21 #include "vm/JitActivation.h"
22
23 #include "vm/JSScript-inl.h"
24
25 using namespace js;
26 using namespace js::jit;
27
28 JSJitFrameIter::JSJitFrameIter(const JitActivation* activation)
29 : current_(activation->jsExitFP()),
30 type_(FrameType::Exit),
31 activation_(activation) {
32 // If we're currently performing a bailout, we have to use the activation's
33 // bailout data when we start iterating over the activation's frames.
34 if (activation_->bailoutData()) {
35 current_ = activation_->bailoutData()->fp();
36 type_ = FrameType::Bailout;
37 }
38 MOZ_ASSERT(!TlsContext.get()->inUnsafeCallWithABI);
39 }
40
41 JSJitFrameIter::JSJitFrameIter(const JitActivation* activation, uint8_t* fp,
42 bool unwinding)
43 : current_(fp), type_(FrameType::Exit), activation_(activation) {
44 // This constructor is only used when resuming iteration after iterating Wasm
45 // frames in the same JitActivation so ignore activation_->bailoutData().
46 if (unwinding) {
47 MOZ_ASSERT(fp == activation->jsExitFP());
48 } else {
49 MOZ_ASSERT(fp > activation->jsOrWasmExitFP());
50 }
51 MOZ_ASSERT(!TlsContext.get()->inUnsafeCallWithABI);
52 }
53
54 bool JSJitFrameIter::checkInvalidation() const {
55 IonScript* dummy;
56 return checkInvalidation(&dummy);
57 }
58
59 bool JSJitFrameIter::checkInvalidation(IonScript** ionScriptOut) const {
60 JSScript* script = this->script();
61 if (isBailoutJS()) {
62 *ionScriptOut = activation_->bailoutData()->ionScript();
63 return !script->hasIonScript() || script->ionScript() != *ionScriptOut;
64 }
65
66 uint8_t* returnAddr = resumePCinCurrentFrame();
67 // N.B. the current IonScript is not the same as the frame's
68 // IonScript if the frame has since been invalidated.
69 bool invalidated = !script->hasIonScript() ||
70 !script->ionScript()->containsReturnAddress(returnAddr);
71 if (!invalidated) {
72 return false;
73 }
74
75 int32_t invalidationDataOffset = ((int32_t*)returnAddr)[-1];
76 uint8_t* ionScriptDataOffset = returnAddr + invalidationDataOffset;
77 IonScript* ionScript = (IonScript*)Assembler::GetPointer(ionScriptDataOffset);
78 MOZ_ASSERT(ionScript->containsReturnAddress(returnAddr));
79 *ionScriptOut = ionScript;
80 return true;
81 }
82
83 CalleeToken JSJitFrameIter::calleeToken() const {
84 return ((JitFrameLayout*)current_)->calleeToken();
85 }
86
87 JSFunction* JSJitFrameIter::callee() const {
88 MOZ_ASSERT(isScripted() || isTrampolineNative());
89 MOZ_ASSERT(isFunctionFrame());
90 return CalleeTokenToFunction(calleeToken());
91 }
92
93 JSFunction* JSJitFrameIter::maybeCallee() const {
94 if (isScripted() && isFunctionFrame()) {
95 return callee();
96 }
97 return nullptr;
98 }
99
100 bool JSJitFrameIter::isBareExit() const {
101 if (type_ != FrameType::Exit) {
102 return false;
103 }
104 return exitFrame()->isBareExit();
105 }
106
107 bool JSJitFrameIter::isUnwoundJitExit() const {
108 if (type_ != FrameType::Exit) {
109 return false;
110 }
111 return exitFrame()->isUnwoundJitExit();
112 }
113
114 bool JSJitFrameIter::isFunctionFrame() const {
115 return CalleeTokenIsFunction(calleeToken());
116 }
117
118 JSScript* JSJitFrameIter::script() const {
119 MOZ_ASSERT(isScripted());
120 JSScript* script = MaybeForwardedScriptFromCalleeToken(calleeToken());
121 MOZ_ASSERT(script);
122 return script;
123 }
124
125 JSScript* JSJitFrameIter::maybeForwardedScript() const {
126 MOZ_ASSERT(isScripted());
127 if (isBaselineJS()) {
128 return MaybeForwardedScriptFromCalleeToken(baselineFrame()->calleeToken());
129 }
130 JSScript* script = MaybeForwardedScriptFromCalleeToken(calleeToken());
131 MOZ_ASSERT(script);
132 return script;
133 }
134
135 void JSJitFrameIter::baselineScriptAndPc(JSScript** scriptRes,
136 jsbytecode** pcRes) const {
137 MOZ_ASSERT(isBaselineJS());
138 JSScript* script = this->script();
139 if (scriptRes) {
140 *scriptRes = script;
141 }
142
143 MOZ_ASSERT(pcRes);
144
145 // The Baseline Interpreter stores the bytecode pc in the frame.
146 if (baselineFrame()->runningInInterpreter()) {
147 MOZ_ASSERT(baselineFrame()->interpreterScript() == script);
148 *pcRes = baselineFrame()->interpreterPC();
149 return;
150 }
151
152 // There must be a BaselineScript with a RetAddrEntry for the current return
153 // address.
154 uint8_t* retAddr = resumePCinCurrentFrame();
155 const RetAddrEntry& entry =
156 script->baselineScript()->retAddrEntryFromReturnAddress(retAddr);
157 *pcRes = entry.pc(script);
158 }
159
160 Value* JSJitFrameIter::actualArgs() const { return jsFrame()->actualArgs(); }
161
162 uint8_t* JSJitFrameIter::prevFp() const { return current()->callerFramePtr(); }
163
164 // Compute the size of a Baseline frame excluding pushed VMFunction arguments or
165 // callee frame headers. This is used to calculate the number of Value slots in
166 // the frame. The caller asserts this matches BaselineFrame::debugFrameSize.
167 static uint32_t ComputeBaselineFrameSize(const JSJitFrameIter& frame) {
168 MOZ_ASSERT(frame.prevType() == FrameType::BaselineJS);
169
170 uint32_t frameSize = frame.current()->callerFramePtr() - frame.fp();
171
172 if (frame.isBaselineStub()) {
173 return frameSize - BaselineStubFrameLayout::Size();
174 }
175
176 // Note: an UnwoundJit exit frame is a JitFrameLayout that was turned into an
177 // ExitFrameLayout by EnsureUnwoundJitExitFrame. We have to use the original
178 // header size here because that's what we have on the stack.
179 if (frame.isScripted() || frame.isUnwoundJitExit()) {
180 return frameSize - JitFrameLayout::Size();
181 }
182
183 if (frame.isExitFrame()) {
184 frameSize -= ExitFrameLayout::Size();
185 if (frame.exitFrame()->isWrapperExit()) {
186 VMFunctionId id = frame.exitFrame()->footer()->functionId();
187 const VMFunctionData& data = GetVMFunction(id);
188 frameSize -= data.explicitStackSlots() * sizeof(void*);
189 }
190 return frameSize;
191 }
192
193 MOZ_CRASH("Unexpected frame");
194 }
195
196 void JSJitFrameIter::operator++() {
197 MOZ_ASSERT(!isEntry());
198
199 // Compute BaselineFrame size. In debug builds this is equivalent to
200 // BaselineFrame::debugFrameSize_. This is asserted at the end of this method.
201 if (current()->prevType() == FrameType::BaselineJS) {
202 uint32_t frameSize = ComputeBaselineFrameSize(*this);
203 baselineFrameSize_ = mozilla::Some(frameSize);
204 } else {
205 baselineFrameSize_ = mozilla::Nothing();
206 }
207
208 cachedSafepointIndex_ = nullptr;
209
210 // If the next frame is the entry frame, just exit. Don't update current_,
211 // since the entry and first frames overlap.
212 if (isEntry(current()->prevType())) {
213 type_ = current()->prevType();
214 return;
215 }
216
217 type_ = current()->prevType();
218 resumePCinCurrentFrame_ = current()->returnAddress();
219 current_ = prevFp();
220
221 MOZ_ASSERT_IF(isBaselineJS(),
222 baselineFrame()->debugFrameSize() == *baselineFrameSize_);
223 }
224
225 uintptr_t* JSJitFrameIter::spillBase() const {
226 MOZ_ASSERT(isIonJS());
227
228 // Get the base address to where safepoint registers are spilled.
229 // Out-of-line calls do not unwind the extra padding space used to
230 // aggregate bailout tables, so we use frameSize instead of frameLocals,
231 // which would only account for local stack slots.
232 return reinterpret_cast<uintptr_t*>(fp() - ionScript()->frameSize());
233 }
234
235 MachineState JSJitFrameIter::machineState() const {
236 MOZ_ASSERT(isIonScripted());
237
238 // The MachineState is used by GCs for tracing call-sites.
239 if (MOZ_UNLIKELY(isBailoutJS())) {
240 return *activation_->bailoutData()->machineState();
241 }
242
243 SafepointReader reader(ionScript(), safepoint());
244
245 FloatRegisterSet fregs = reader.allFloatSpills().set().reduceSetForPush();
246 GeneralRegisterSet regs = reader.allGprSpills().set();
247
248 uintptr_t* spill = spillBase();
249 uint8_t* spillAlign =
250 alignDoubleSpill(reinterpret_cast<uint8_t*>(spill - regs.size()));
251 char* floatSpill = reinterpret_cast<char*>(spillAlign);
252
253 return MachineState::FromSafepoint(fregs, regs, floatSpill, spill);
254 }
255
256 JitFrameLayout* JSJitFrameIter::jsFrame() const {
257 MOZ_ASSERT(isScripted());
258 if (isBailoutJS()) {
259 return (JitFrameLayout*)activation_->bailoutData()->fp();
260 }
261
262 return (JitFrameLayout*)fp();
263 }
264
265 IonScript* JSJitFrameIter::ionScript() const {
266 MOZ_ASSERT(isIonScripted());
267 if (isBailoutJS()) {
268 return activation_->bailoutData()->ionScript();
269 }
270
271 IonScript* ionScript = nullptr;
272 if (checkInvalidation(&ionScript)) {
273 return ionScript;
274 }
275 return ionScriptFromCalleeToken();
276 }
277
278 IonScript* JSJitFrameIter::ionScriptFromCalleeToken() const {
279 MOZ_ASSERT(isIonJS());
280 MOZ_ASSERT(!checkInvalidation());
281 return script()->ionScript();
282 }
283
284 const SafepointIndex* JSJitFrameIter::safepoint() const {
285 MOZ_ASSERT(isIonJS());
286 if (!cachedSafepointIndex_) {
287 cachedSafepointIndex_ =
288 ionScript()->getSafepointIndex(resumePCinCurrentFrame());
289 }
290 return cachedSafepointIndex_;
291 }
292
293 SnapshotOffset JSJitFrameIter::snapshotOffset() const {
294 MOZ_ASSERT(isIonScripted());
295 if (isBailoutJS()) {
296 return activation_->bailoutData()->snapshotOffset();
297 }
298 return osiIndex()->snapshotOffset();
299 }
300
301 const OsiIndex* JSJitFrameIter::osiIndex() const {
302 MOZ_ASSERT(isIonJS());
303 SafepointReader reader(ionScript(), safepoint());
304 return ionScript()->getOsiIndex(reader.osiReturnPointOffset());
305 }
306
307 bool JSJitFrameIter::isConstructing() const {
308 return CalleeTokenIsConstructing(calleeToken());
309 }
310
311 unsigned JSJitFrameIter::numActualArgs() const {
312 if (isScripted()) {
313 return jsFrame()->numActualArgs();
314 }
315
316 MOZ_ASSERT(isExitFrameLayout<NativeExitFrameLayout>());
317 return exitFrame()->as<NativeExitFrameLayout>()->argc();
318 }
319
320 void JSJitFrameIter::dumpBaseline() const {
321 MOZ_ASSERT(isBaselineJS());
322
323 fprintf(stderr, " JS Baseline frame\n");
324 if (isFunctionFrame()) {
325 fprintf(stderr, " callee fun: ");
326 #if defined(DEBUG) || defined(JS_JITSPEW)
327 DumpObject(callee());
328 #else
329 fprintf(stderr, "?\n");
330 #endif
331 } else {
332 fprintf(stderr, " global frame, no callee\n");
333 }
334
335 fprintf(stderr, " file %s line %u\n", script()->filename(),
336 script()->lineno());
337
338 JSContext* cx = TlsContext.get();
339 RootedScript script(cx);
340 jsbytecode* pc;
341 baselineScriptAndPc(script.address(), &pc);
342
343 fprintf(stderr, " script = %p, pc = %p (offset %u)\n", (void*)script, pc,
344 uint32_t(script->pcToOffset(pc)));
345 fprintf(stderr, " current op: %s\n", CodeName(JSOp(*pc)));
346
347 fprintf(stderr, " actual args: %u\n", numActualArgs());
348
349 for (unsigned i = 0; i < baselineFrameNumValueSlots(); i++) {
350 fprintf(stderr, " slot %u: ", i);
351 #if defined(DEBUG) || defined(JS_JITSPEW)
352 Value* v = baselineFrame()->valueSlot(i);
353 DumpValue(*v);
354 #else
355 fprintf(stderr, "?\n");
356 #endif
357 }
358 }
359
360 void JSJitFrameIter::dump() const {
361 switch (type_) {
362 case FrameType::CppToJSJit:
363 fprintf(stderr, " Entry frame\n");
364 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
365 break;
366 case FrameType::BaselineJS:
367 dumpBaseline();
368 break;
369 case FrameType::BaselineStub:
370 fprintf(stderr, " Baseline stub frame\n");
371 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
372 break;
373 case FrameType::Bailout:
374 case FrameType::IonJS: {
375 InlineFrameIterator frames(TlsContext.get(), this);
376 for (;;) {
377 frames.dump();
378 if (!frames.more()) {
379 break;
380 }
381 ++frames;
382 }
383 break;
384 }
385 case FrameType::BaselineInterpreterEntry:
386 fprintf(stderr, " Baseline Interpreter Entry frame\n");
387 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
388 break;
389 case FrameType::Rectifier:
390 fprintf(stderr, " Rectifier frame\n");
391 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
392 break;
393 case FrameType::TrampolineNative:
394 fprintf(stderr, " TrampolineNative frame\n");
395 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
396 break;
397 case FrameType::IonICCall:
398 fprintf(stderr, " Ion IC call\n");
399 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
400 break;
401 case FrameType::WasmToJSJit:
402 fprintf(stderr, " Fast wasm-to-JS entry frame\n");
403 fprintf(stderr, " Caller frame ptr: %p\n", current()->callerFramePtr());
404 break;
405 case FrameType::Exit:
406 fprintf(stderr, " Exit frame\n");
407 break;
408 };
409 fputc('\n', stderr);
410 }
411
412 #ifdef DEBUG
413 bool JSJitFrameIter::verifyReturnAddressUsingNativeToBytecodeMap() {
414 MOZ_ASSERT(resumePCinCurrentFrame_ != nullptr);
415
416 // Only handle Ion frames for now.
417 if (type_ != FrameType::IonJS && type_ != FrameType::BaselineJS) {
418 return true;
419 }
420
421 JSRuntime* rt = TlsContext.get()->runtime();
422
423 // Don't verify while off thread.
424 if (!CurrentThreadCanAccessRuntime(rt)) {
425 return true;
426 }
427
428 // Don't verify if sampling is being suppressed.
429 if (!TlsContext.get()->isProfilerSamplingEnabled()) {
430 return true;
431 }
432
433 if (JS::RuntimeHeapIsMinorCollecting()) {
434 return true;
435 }
436
437 JitRuntime* jitrt = rt->jitRuntime();
438
439 // Look up and print bytecode info for the native address.
440 const JitcodeGlobalEntry* entry =
441 jitrt->getJitcodeGlobalTable()->lookup(resumePCinCurrentFrame_);
442 if (!entry) {
443 return true;
444 }
445
446 JitSpew(JitSpew_Profiling, "Found nativeToBytecode entry for %p: %p - %p",
447 resumePCinCurrentFrame_, entry->nativeStartAddr(),
448 entry->nativeEndAddr());
449
450 BytecodeLocationVector location;
451 uint32_t depth = UINT32_MAX;
452 if (!entry->callStackAtAddr(rt, resumePCinCurrentFrame_, location, &depth)) {
453 return false;
454 }
455 MOZ_ASSERT(depth > 0 && depth != UINT32_MAX);
456 MOZ_ASSERT(location.length() == depth);
457
458 JitSpew(JitSpew_Profiling, "Found bytecode location of depth %u:", depth);
459 for (size_t i = 0; i < location.length(); i++) {
460 JitSpew(JitSpew_Profiling, " %s:%u - %zu",
461 location[i].getDebugOnlyScript()->filename(),
462 location[i].getDebugOnlyScript()->lineno(),
463 size_t(location[i].toRawBytecode() -
464 location[i].getDebugOnlyScript()->code()));
465 }
466
467 if (type_ == FrameType::IonJS) {
468 // Create an InlineFrameIterator here and verify the mapped info against the
469 // iterator info.
470 InlineFrameIterator inlineFrames(TlsContext.get(), this);
471 for (size_t idx = 0; idx < location.length(); idx++) {
472 MOZ_ASSERT(idx < location.length());
473 MOZ_ASSERT_IF(idx < location.length() - 1, inlineFrames.more());
474
475 JitSpew(JitSpew_Profiling, "Match %d: ION %s:%u(%zu) vs N2B %s:%u(%zu)",
476 (int)idx, inlineFrames.script()->filename(),
477 inlineFrames.script()->lineno(),
478 size_t(inlineFrames.pc() - inlineFrames.script()->code()),
479 location[idx].getDebugOnlyScript()->filename(),
480 location[idx].getDebugOnlyScript()->lineno(),
481 size_t(location[idx].toRawBytecode() -
482 location[idx].getDebugOnlyScript()->code()));
483
484 MOZ_ASSERT(inlineFrames.script() == location[idx].getDebugOnlyScript());
485
486 if (inlineFrames.more()) {
487 ++inlineFrames;
488 }
489 }
490 }
491
492 return true;
493 }
494 #endif // DEBUG
495
496 JSJitProfilingFrameIterator::JSJitProfilingFrameIterator(JSContext* cx,
497 void* pc, void* sp) {
498 // If no profilingActivation is live, initialize directly to
499 // end-of-iteration state.
500 if (!cx->profilingActivation()) {
501 type_ = FrameType::CppToJSJit;
502 fp_ = nullptr;
503 resumePCinCurrentFrame_ = nullptr;
504 return;
505 }
506
507 MOZ_ASSERT(cx->profilingActivation()->isJit());
508
509 JitActivation* act = cx->profilingActivation()->asJit();
510
511 // If the top JitActivation has a null lastProfilingFrame, assume that
512 // it's a trivially empty activation, and initialize directly
513 // to end-of-iteration state.
514 if (!act->lastProfilingFrame()) {
515 type_ = FrameType::CppToJSJit;
516 fp_ = nullptr;
517 resumePCinCurrentFrame_ = nullptr;
518 return;
519 }
520
521 // Get the fp from the current profilingActivation
522 fp_ = (uint8_t*)act->lastProfilingFrame();
523
524 // Use fp_ as endStackAddress_. For cases below where we know we're currently
525 // executing JIT code, we use the current stack pointer instead.
526 endStackAddress_ = fp_;
527
528 // Profiler sampling must NOT be suppressed if we are here.
529 MOZ_ASSERT(cx->isProfilerSamplingEnabled());
530
531 // Try initializing with sampler pc
532 if (tryInitWithPC(pc)) {
533 endStackAddress_ = sp;
534 return;
535 }
536
537 if (!IsPortableBaselineInterpreterEnabled()) {
538 // Try initializing with sampler pc using native=>bytecode table.
539 JitcodeGlobalTable* table =
540 cx->runtime()->jitRuntime()->getJitcodeGlobalTable();
541 if (tryInitWithTable(table, pc, /* forLastCallSite = */ false)) {
542 endStackAddress_ = sp;
543 return;
544 }
545
546 // Try initializing with lastProfilingCallSite pc
547 void* lastCallSite = act->lastProfilingCallSite();
548 if (lastCallSite) {
549 if (tryInitWithPC(lastCallSite)) {
550 return;
551 }
552
553 // Try initializing with lastProfilingCallSite pc using native=>bytecode
554 // table.
555 if (tryInitWithTable(table, lastCallSite, /* forLastCallSite = */ true)) {
556 return;
557 }
558 }
559 }
560
561 // If nothing matches, for now just assume we are at the start of the last
562 // frame's baseline jit code or interpreter code.
563 type_ = FrameType::BaselineJS;
564 if (frameScript()->hasBaselineScript()) {
565 resumePCinCurrentFrame_ = frameScript()->baselineScript()->method()->raw();
566 } else if (!IsPortableBaselineInterpreterEnabled()) {
567 MOZ_ASSERT(IsBaselineInterpreterEnabled());
568 resumePCinCurrentFrame_ =
569 cx->runtime()->jitRuntime()->baselineInterpreter().codeRaw();
570 } else {
571 resumePCinCurrentFrame_ = nullptr;
572 }
573 }
574
575 template <typename ReturnType = CommonFrameLayout*>
576 static inline ReturnType GetPreviousRawFrame(CommonFrameLayout* frame) {
577 return ReturnType(frame->callerFramePtr());
578 }
579
580 JSJitProfilingFrameIterator::JSJitProfilingFrameIterator(
581 CommonFrameLayout* fp) {
582 endStackAddress_ = fp;
583 moveToNextFrame(fp);
584 }
585
586 bool JSJitProfilingFrameIterator::tryInitWithPC(void* pc) {
587 JSScript* callee = frameScript();
588
589 // Check for Ion first, since it's more likely for hot code.
590 if (callee->hasIonScript() &&
591 callee->ionScript()->method()->containsNativePC(pc)) {
592 type_ = FrameType::IonJS;
593 resumePCinCurrentFrame_ = pc;
594 return true;
595 }
596
597 // Check for containment in Baseline jitcode second.
598 if (callee->hasBaselineScript() &&
599 callee->baselineScript()->method()->containsNativePC(pc)) {
600 type_ = FrameType::BaselineJS;
601 resumePCinCurrentFrame_ = pc;
602 return true;
603 }
604
605 return false;
606 }
607
608 bool JSJitProfilingFrameIterator::tryInitWithTable(JitcodeGlobalTable* table,
609 void* pc,
610 bool forLastCallSite) {
611 if (!pc) {
612 return false;
613 }
614
615 const JitcodeGlobalEntry* entry = table->lookup(pc);
616 if (!entry) {
617 return false;
618 }
619
620 JSScript* callee = frameScript();
621
622 MOZ_ASSERT(entry->isIon() || entry->isIonIC() || entry->isBaseline() ||
623 entry->isBaselineInterpreter() || entry->isDummy() ||
624 entry->isSelfHostedShared());
625
626 // Treat dummy lookups as an empty frame sequence.
627 if (entry->isDummy()) {
628 type_ = FrameType::CppToJSJit;
629 fp_ = nullptr;
630 resumePCinCurrentFrame_ = nullptr;
631 return true;
632 }
633
634 // For IonICEntry, use the corresponding IonEntry.
635 if (entry->isIonIC()) {
636 entry = table->lookup(entry->asIonIC().rejoinAddr());
637 MOZ_ASSERT(entry);
638 MOZ_RELEASE_ASSERT(entry->isIon());
639 }
640
641 if (entry->isIon()) {
642 // If looked-up callee doesn't match frame callee, don't accept
643 // lastProfilingCallSite
644 if (entry->asIon().getScript(0) != callee) {
645 return false;
646 }
647
648 type_ = FrameType::IonJS;
649 resumePCinCurrentFrame_ = pc;
650 return true;
651 }
652
653 if (entry->isBaseline()) {
654 // If looked-up callee doesn't match frame callee, don't accept
655 // lastProfilingCallSite
656 if (forLastCallSite && entry->asBaseline().script() != callee) {
657 return false;
658 }
659
660 type_ = FrameType::BaselineJS;
661 resumePCinCurrentFrame_ = pc;
662 return true;
663 }
664
665 if (entry->isSelfHostedShared()) {
666 // Shared entries don't track who the callee is, so we can't check
667 // lastProfilingCallSite
668 type_ = FrameType::BaselineJS;
669 resumePCinCurrentFrame_ = pc;
670 return true;
671 }
672
673 if (entry->isBaselineInterpreter()) {
674 type_ = FrameType::BaselineJS;
675 resumePCinCurrentFrame_ = pc;
676 return true;
677 }
678
679 return false;
680 }
681
682 const char* JSJitProfilingFrameIterator::baselineInterpreterLabel() const {
683 MOZ_ASSERT(type_ == FrameType::BaselineJS);
684 return frameScript()->jitScript()->profileString();
685 }
686
687 void JSJitProfilingFrameIterator::baselineInterpreterScriptPC(
688 JSScript** script, jsbytecode** pc, uint64_t* realmID) const {
689 MOZ_ASSERT(type_ == FrameType::BaselineJS);
690 BaselineFrame* blFrame = (BaselineFrame*)(fp_ - BaselineFrame::Size());
691 *script = frameScript();
692 *pc = (*script)->code();
693
694 if (blFrame->runningInInterpreter() &&
695 blFrame->interpreterScript() == *script) {
696 jsbytecode* interpPC = blFrame->interpreterPC();
697 if ((*script)->containsPC(interpPC)) {
698 *pc = interpPC;
699 }
700
701 *realmID = (*script)->realm()->creationOptions().profilerRealmID();
702 }
703 }
704
705 void JSJitProfilingFrameIterator::operator++() {
706 JitFrameLayout* frame = framePtr();
707 moveToNextFrame(frame);
708 }
709
710 void JSJitProfilingFrameIterator::moveToNextFrame(CommonFrameLayout* frame) {
711 /*
712 * fp_ points to a Baseline or Ion frame. The possible call-stacks
713 * patterns occurring between this frame and a previous Ion, Baseline or Entry
714 * frame are as follows:
715 *
716 * <Baseline-Or-Ion>
717 * ^
718 * |
719 * ^--- Ion (or Baseline JSOp::Resume)
720 * |
721 * ^--- Baseline Stub <---- Baseline
722 * |
723 * ^--- IonICCall <---- Ion
724 * |
725 * ^--- WasmToJSJit <---- (other wasm frames, not handled by this iterator)
726 * |
727 * ^--- Entry Frame (BaselineInterpreter) (unwrapped)
728 * |
729 * ^--- Arguments Rectifier (unwrapped)
730 * |
731 * ^--- Trampoline Native (unwrapped)
732 * |
733 * ^--- Entry Frame (CppToJSJit)
734 *
735 * NOTE: Keep this in sync with JitRuntime::generateProfilerExitFrameTailStub!
736 */
737
738 while (true) {
739 // Unwrap baseline interpreter entry frame.
740 if (frame->prevType() == FrameType::BaselineInterpreterEntry) {
741 frame = GetPreviousRawFrame<BaselineInterpreterEntryFrameLayout*>(frame);
742 continue;
743 }
744
745 // Unwrap rectifier frames.
746 if (frame->prevType() == FrameType::Rectifier) {
747 frame = GetPreviousRawFrame<RectifierFrameLayout*>(frame);
748 MOZ_ASSERT(frame->prevType() == FrameType::IonJS ||
749 frame->prevType() == FrameType::BaselineStub ||
750 frame->prevType() == FrameType::TrampolineNative ||
751 frame->prevType() == FrameType::WasmToJSJit ||
752 frame->prevType() == FrameType::CppToJSJit);
753 continue;
754 }
755
756 // Unwrap TrampolineNative frames.
757 if (frame->prevType() == FrameType::TrampolineNative) {
758 frame = GetPreviousRawFrame<TrampolineNativeFrameLayout*>(frame);
759 MOZ_ASSERT(frame->prevType() == FrameType::IonJS ||
760 frame->prevType() == FrameType::BaselineStub ||
761 frame->prevType() == FrameType::Rectifier ||
762 frame->prevType() == FrameType::WasmToJSJit ||
763 frame->prevType() == FrameType::CppToJSJit);
764 continue;
765 }
766
767 break;
768 }
769
770 FrameType prevType = frame->prevType();
771 switch (prevType) {
772 case FrameType::IonJS:
773 case FrameType::BaselineJS:
774 resumePCinCurrentFrame_ = frame->returnAddress();
775 fp_ = GetPreviousRawFrame<uint8_t*>(frame);
776 type_ = prevType;
777 return;
778
779 case FrameType::BaselineStub:
780 case FrameType::IonICCall: {
781 FrameType stubPrevType = (prevType == FrameType::BaselineStub)
782 ? FrameType::BaselineJS
783 : FrameType::IonJS;
784 auto* stubFrame = GetPreviousRawFrame<CommonFrameLayout*>(frame);
785 MOZ_ASSERT(stubFrame->prevType() == stubPrevType);
786 resumePCinCurrentFrame_ = stubFrame->returnAddress();
787 fp_ = GetPreviousRawFrame<uint8_t*>(stubFrame);
788 type_ = stubPrevType;
789 return;
790 }
791
792 case FrameType::WasmToJSJit:
793 // No previous JS JIT frame. Set fp_ to nullptr to indicate the
794 // JSJitProfilingFrameIterator is done(). Also set wasmCallerFP_ so that
795 // the caller can pass it to a Wasm frame iterator.
796 resumePCinCurrentFrame_ = nullptr;
797 fp_ = nullptr;
798 type_ = FrameType::WasmToJSJit;
799 MOZ_ASSERT(!wasmCallerFP_);
800 wasmCallerFP_ = GetPreviousRawFrame<uint8_t*>(frame);
801 MOZ_ASSERT(wasmCallerFP_);
802 MOZ_ASSERT(done());
803 return;
804
805 case FrameType::CppToJSJit:
806 // No previous JS JIT frame. Set fp_ to nullptr to indicate the
807 // JSJitProfilingFrameIterator is done().
808 resumePCinCurrentFrame_ = nullptr;
809 fp_ = nullptr;
810 type_ = FrameType::CppToJSJit;
811 MOZ_ASSERT(!wasmCallerFP_);
812 MOZ_ASSERT(done());
813 return;
814
815 case FrameType::BaselineInterpreterEntry:
816 case FrameType::Rectifier:
817 case FrameType::TrampolineNative:
818 case FrameType::Exit:
819 case FrameType::Bailout:
820 // Rectifier and Baseline Interpreter entry frames are handled before
821 // this switch. The other frame types can't call JS functions directly.
822 break;
823 }
824
825 MOZ_CRASH("Bad frame type.");
826 }