V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
frames.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/frames.h"
6 
7 #include <memory>
8 #include <sstream>
9 
10 #include "src/base/bits.h"
11 #include "src/deoptimizer.h"
12 #include "src/frames-inl.h"
13 #include "src/ic/ic-stats.h"
14 #include "src/objects/code.h"
15 #include "src/objects/slots.h"
16 #include "src/objects/smi.h"
17 #include "src/register-configuration.h"
18 #include "src/safepoint-table.h"
19 #include "src/snapshot/snapshot.h"
20 #include "src/string-stream.h"
21 #include "src/visitors.h"
22 #include "src/vm-state-inl.h"
23 #include "src/wasm/wasm-code-manager.h"
24 #include "src/wasm/wasm-engine.h"
25 #include "src/wasm/wasm-objects-inl.h"
26 #include "src/zone/zone-containers.h"
27 
28 namespace v8 {
29 namespace internal {
30 
31 ReturnAddressLocationResolver StackFrame::return_address_location_resolver_ =
32  nullptr;
33 
34 // Iterator that supports traversing the stack handlers of a
35 // particular frame. Needs to know the top of the handler chain.
37  public:
38  StackHandlerIterator(const StackFrame* frame, StackHandler* handler)
39  : limit_(frame->fp()), handler_(handler) {
40  // Make sure the handler has already been unwound to this frame.
41  DCHECK(frame->sp() <= handler->address());
42  }
43 
44  StackHandler* handler() const { return handler_; }
45 
46  bool done() { return handler_ == nullptr || handler_->address() > limit_; }
47  void Advance() {
48  DCHECK(!done());
49  handler_ = handler_->next();
50  }
51 
52  private:
53  const Address limit_;
54  StackHandler* handler_;
55 };
56 
57 
58 // -------------------------------------------------------------------------
59 
60 
61 #define INITIALIZE_SINGLETON(type, field) field##_(this),
62 StackFrameIteratorBase::StackFrameIteratorBase(Isolate* isolate,
63  bool can_access_heap_objects)
64  : isolate_(isolate),
65  STACK_FRAME_TYPE_LIST(INITIALIZE_SINGLETON) frame_(nullptr),
66  handler_(nullptr),
67  can_access_heap_objects_(can_access_heap_objects) {}
68 #undef INITIALIZE_SINGLETON
69 
70 StackFrameIterator::StackFrameIterator(Isolate* isolate)
71  : StackFrameIterator(isolate, isolate->thread_local_top()) {}
72 
73 StackFrameIterator::StackFrameIterator(Isolate* isolate, ThreadLocalTop* t)
74  : StackFrameIteratorBase(isolate, true) {
75  Reset(t);
76 }
77 
78 void StackFrameIterator::Advance() {
79  DCHECK(!done());
80  // Compute the state of the calling frame before restoring
81  // callee-saved registers and unwinding handlers. This allows the
82  // frame code that computes the caller state to access the top
83  // handler and the value of any callee-saved register if needed.
84  StackFrame::State state;
85  StackFrame::Type type = frame_->GetCallerState(&state);
86 
87  // Unwind handlers corresponding to the current frame.
88  StackHandlerIterator it(frame_, handler_);
89  while (!it.done()) it.Advance();
90  handler_ = it.handler();
91 
92  // Advance to the calling frame.
93  frame_ = SingletonFor(type, &state);
94 
95  // When we're done iterating over the stack frames, the handler
96  // chain must have been completely unwound.
97  DCHECK(!done() || handler_ == nullptr);
98 }
99 
100 
101 void StackFrameIterator::Reset(ThreadLocalTop* top) {
102  StackFrame::State state;
103  StackFrame::Type type = ExitFrame::GetStateForFramePointer(
104  Isolate::c_entry_fp(top), &state);
105  handler_ = StackHandler::FromAddress(Isolate::handler(top));
106  frame_ = SingletonFor(type, &state);
107 }
108 
109 
110 StackFrame* StackFrameIteratorBase::SingletonFor(StackFrame::Type type,
111  StackFrame::State* state) {
112  StackFrame* result = SingletonFor(type);
113  DCHECK((!result) == (type == StackFrame::NONE));
114  if (result) result->state_ = *state;
115  return result;
116 }
117 
118 
119 StackFrame* StackFrameIteratorBase::SingletonFor(StackFrame::Type type) {
120 #define FRAME_TYPE_CASE(type, field) \
121  case StackFrame::type: \
122  return &field##_;
123 
124  switch (type) {
125  case StackFrame::NONE:
126  return nullptr;
127  STACK_FRAME_TYPE_LIST(FRAME_TYPE_CASE)
128  default: break;
129  }
130  return nullptr;
131 
132 #undef FRAME_TYPE_CASE
133 }
134 
135 // -------------------------------------------------------------------------
136 
137 void JavaScriptFrameIterator::Advance() {
138  do {
139  iterator_.Advance();
140  } while (!iterator_.done() && !iterator_.frame()->is_java_script());
141 }
142 
143 // -------------------------------------------------------------------------
144 
145 StackTraceFrameIterator::StackTraceFrameIterator(Isolate* isolate)
146  : iterator_(isolate) {
147  if (!done() && !IsValidFrame(iterator_.frame())) Advance();
148 }
149 
150 StackTraceFrameIterator::StackTraceFrameIterator(Isolate* isolate,
151  StackFrame::Id id)
152  : StackTraceFrameIterator(isolate) {
153  while (!done() && frame()->id() != id) Advance();
154 }
155 
156 void StackTraceFrameIterator::Advance() {
157  do {
158  iterator_.Advance();
159  } while (!done() && !IsValidFrame(iterator_.frame()));
160 }
161 
162 bool StackTraceFrameIterator::IsValidFrame(StackFrame* frame) const {
163  if (frame->is_java_script()) {
164  JavaScriptFrame* jsFrame = static_cast<JavaScriptFrame*>(frame);
165  if (!jsFrame->function()->IsJSFunction()) return false;
166  return jsFrame->function()->shared()->IsSubjectToDebugging();
167  }
168  // apart from javascript, only wasm is valid
169  return frame->is_wasm();
170 }
171 
172 // -------------------------------------------------------------------------
173 
174 namespace {
175 
176 bool IsInterpreterFramePc(Isolate* isolate, Address pc,
177  StackFrame::State* state) {
178  Code interpreter_entry_trampoline =
179  isolate->builtins()->builtin(Builtins::kInterpreterEntryTrampoline);
180  Code interpreter_bytecode_advance =
181  isolate->builtins()->builtin(Builtins::kInterpreterEnterBytecodeAdvance);
182  Code interpreter_bytecode_dispatch =
183  isolate->builtins()->builtin(Builtins::kInterpreterEnterBytecodeDispatch);
184 
185  if (interpreter_entry_trampoline->contains(pc) ||
186  interpreter_bytecode_advance->contains(pc) ||
187  interpreter_bytecode_dispatch->contains(pc)) {
188  return true;
189  } else if (FLAG_interpreted_frames_native_stack) {
190  intptr_t marker = Memory<intptr_t>(
191  state->fp + CommonFrameConstants::kContextOrFrameTypeOffset);
192  MSAN_MEMORY_IS_INITIALIZED(
193  state->fp + StandardFrameConstants::kFunctionOffset, kPointerSize);
194  Object* maybe_function =
195  Memory<Object*>(state->fp + StandardFrameConstants::kFunctionOffset);
196  // There's no need to run a full ContainsSlow if we know the frame can't be
197  // an InterpretedFrame, so we do these fast checks first
198  if (StackFrame::IsTypeMarker(marker) || maybe_function->IsSmi()) {
199  return false;
200  } else if (!isolate->heap()->code_space()->ContainsSlow(pc)) {
201  return false;
202  }
203  interpreter_entry_trampoline =
204  isolate->heap()->GcSafeFindCodeForInnerPointer(pc);
205  return interpreter_entry_trampoline->is_interpreter_trampoline_builtin();
206  } else {
207  return false;
208  }
209 }
210 
211 DISABLE_ASAN Address ReadMemoryAt(Address address) {
212  return Memory<Address>(address);
213 }
214 
215 } // namespace
216 
217 SafeStackFrameIterator::SafeStackFrameIterator(
218  Isolate* isolate,
219  Address fp, Address sp, Address js_entry_sp)
220  : StackFrameIteratorBase(isolate, false),
221  low_bound_(sp),
222  high_bound_(js_entry_sp),
223  top_frame_type_(StackFrame::NONE),
224  external_callback_scope_(isolate->external_callback_scope()) {
225  StackFrame::State state;
226  StackFrame::Type type;
227  ThreadLocalTop* top = isolate->thread_local_top();
228  bool advance_frame = true;
229  if (IsValidTop(top)) {
230  type = ExitFrame::GetStateForFramePointer(Isolate::c_entry_fp(top), &state);
231  top_frame_type_ = type;
232  } else if (IsValidStackAddress(fp)) {
233  DCHECK_NE(fp, kNullAddress);
234  state.fp = fp;
235  state.sp = sp;
236  state.pc_address = StackFrame::ResolveReturnAddressLocation(
237  reinterpret_cast<Address*>(StandardFrame::ComputePCAddress(fp)));
238 
239  // If the top of stack is a return address to the interpreter trampoline,
240  // then we are likely in a bytecode handler with elided frame. In that
241  // case, set the PC properly and make sure we do not drop the frame.
242  if (IsValidStackAddress(sp)) {
243  MSAN_MEMORY_IS_INITIALIZED(sp, kPointerSize);
244  Address tos = ReadMemoryAt(sp);
245  if (IsInterpreterFramePc(isolate, tos, &state)) {
246  state.pc_address = reinterpret_cast<Address*>(sp);
247  advance_frame = false;
248  }
249  }
250 
251  // StackFrame::ComputeType will read both kContextOffset and kMarkerOffset,
252  // we check only that kMarkerOffset is within the stack bounds and do
253  // compile time check that kContextOffset slot is pushed on the stack before
254  // kMarkerOffset.
255  STATIC_ASSERT(StandardFrameConstants::kFunctionOffset <
256  StandardFrameConstants::kContextOffset);
257  Address frame_marker = fp + StandardFrameConstants::kFunctionOffset;
258  if (IsValidStackAddress(frame_marker)) {
259  type = StackFrame::ComputeType(this, &state);
260  top_frame_type_ = type;
261  // We only keep the top frame if we believe it to be interpreted frame.
262  if (type != StackFrame::INTERPRETED) {
263  advance_frame = true;
264  }
265  } else {
266  // Mark the frame as OPTIMIZED if we cannot determine its type.
267  // We chose OPTIMIZED rather than INTERPRETED because it's closer to
268  // the original value of StackFrame::JAVA_SCRIPT here, in that JAVA_SCRIPT
269  // referred to full-codegen frames (now removed from the tree), and
270  // OPTIMIZED refers to turbofan frames, both of which are generated
271  // code. INTERPRETED frames refer to bytecode.
272  // The frame anyways will be skipped.
273  type = StackFrame::OPTIMIZED;
274  // Top frame is incomplete so we cannot reliably determine its type.
275  top_frame_type_ = StackFrame::NONE;
276  }
277  } else {
278  return;
279  }
280  frame_ = SingletonFor(type, &state);
281  if (advance_frame && frame_) Advance();
282 }
283 
284 
285 bool SafeStackFrameIterator::IsValidTop(ThreadLocalTop* top) const {
286  Address c_entry_fp = Isolate::c_entry_fp(top);
287  if (!IsValidExitFrame(c_entry_fp)) return false;
288  // There should be at least one JS_ENTRY stack handler.
289  Address handler = Isolate::handler(top);
290  if (handler == kNullAddress) return false;
291  // Check that there are no js frames on top of the native frames.
292  return c_entry_fp < handler;
293 }
294 
295 
296 void SafeStackFrameIterator::AdvanceOneFrame() {
297  DCHECK(!done());
298  StackFrame* last_frame = frame_;
299  Address last_sp = last_frame->sp(), last_fp = last_frame->fp();
300  // Before advancing to the next stack frame, perform pointer validity tests.
301  if (!IsValidFrame(last_frame) || !IsValidCaller(last_frame)) {
302  frame_ = nullptr;
303  return;
304  }
305 
306  // Advance to the previous frame.
307  StackFrame::State state;
308  StackFrame::Type type = frame_->GetCallerState(&state);
309  frame_ = SingletonFor(type, &state);
310  if (!frame_) return;
311 
312  // Check that we have actually moved to the previous frame in the stack.
313  if (frame_->sp() <= last_sp || frame_->fp() <= last_fp) {
314  frame_ = nullptr;
315  }
316 }
317 
318 
319 bool SafeStackFrameIterator::IsValidFrame(StackFrame* frame) const {
320  return IsValidStackAddress(frame->sp()) && IsValidStackAddress(frame->fp());
321 }
322 
323 
324 bool SafeStackFrameIterator::IsValidCaller(StackFrame* frame) {
325  StackFrame::State state;
326  if (frame->is_entry() || frame->is_construct_entry()) {
327  // See EntryFrame::GetCallerState. It computes the caller FP address
328  // and calls ExitFrame::GetStateForFramePointer on it. We need to be
329  // sure that caller FP address is valid.
330  Address caller_fp =
331  Memory<Address>(frame->fp() + EntryFrameConstants::kCallerFPOffset);
332  if (!IsValidExitFrame(caller_fp)) return false;
333  } else if (frame->is_arguments_adaptor()) {
334  // See ArgumentsAdaptorFrame::GetCallerStackPointer. It assumes that
335  // the number of arguments is stored on stack as Smi. We need to check
336  // that it really an Smi.
337  Object* number_of_args = reinterpret_cast<ArgumentsAdaptorFrame*>(frame)->
338  GetExpression(0);
339  if (!number_of_args->IsSmi()) {
340  return false;
341  }
342  }
343  frame->ComputeCallerState(&state);
344  return IsValidStackAddress(state.sp) && IsValidStackAddress(state.fp) &&
345  SingletonFor(frame->GetCallerState(&state)) != nullptr;
346 }
347 
348 
349 bool SafeStackFrameIterator::IsValidExitFrame(Address fp) const {
350  if (!IsValidStackAddress(fp)) return false;
351  Address sp = ExitFrame::ComputeStackPointer(fp);
352  if (!IsValidStackAddress(sp)) return false;
353  StackFrame::State state;
354  ExitFrame::FillState(fp, sp, &state);
355  MSAN_MEMORY_IS_INITIALIZED(state.pc_address, sizeof(state.pc_address));
356  return *state.pc_address != kNullAddress;
357 }
358 
359 
360 void SafeStackFrameIterator::Advance() {
361  while (true) {
362  AdvanceOneFrame();
363  if (done()) break;
364  ExternalCallbackScope* last_callback_scope = nullptr;
365  while (external_callback_scope_ != nullptr &&
366  external_callback_scope_->scope_address() < frame_->fp()) {
367  // As long as the setup of a frame is not atomic, we may happen to be
368  // in an interval where an ExternalCallbackScope is already created,
369  // but the frame is not yet entered. So we are actually observing
370  // the previous frame.
371  // Skip all the ExternalCallbackScope's that are below the current fp.
372  last_callback_scope = external_callback_scope_;
373  external_callback_scope_ = external_callback_scope_->previous();
374  }
375  if (frame_->is_java_script() || frame_->is_wasm()) break;
376  if (frame_->is_exit() || frame_->is_builtin_exit()) {
377  // Some of the EXIT frames may have ExternalCallbackScope allocated on
378  // top of them. In that case the scope corresponds to the first EXIT
379  // frame beneath it. There may be other EXIT frames on top of the
380  // ExternalCallbackScope, just skip them as we cannot collect any useful
381  // information about them.
382  if (last_callback_scope) {
383  frame_->state_.pc_address =
384  last_callback_scope->callback_entrypoint_address();
385  }
386  break;
387  }
388  }
389 }
390 
391 
392 // -------------------------------------------------------------------------
393 
394 namespace {
395 Code GetContainingCode(Isolate* isolate, Address pc) {
396  return isolate->inner_pointer_to_code_cache()->GetCacheEntry(pc)->code;
397 }
398 } // namespace
399 
400 Code StackFrame::LookupCode() const {
401  Code result = GetContainingCode(isolate(), pc());
402  DCHECK_GE(pc(), result->InstructionStart());
403  DCHECK_LT(pc(), result->InstructionEnd());
404  return result;
405 }
406 
407 void StackFrame::IteratePc(RootVisitor* v, Address* pc_address,
408  Address* constant_pool_address, Code holder) {
409  Address pc = *pc_address;
410  DCHECK(holder->GetHeap()->GcSafeCodeContains(holder, pc));
411  unsigned pc_offset = static_cast<unsigned>(pc - holder->InstructionStart());
412  ObjectPtr code = holder;
413  v->VisitRootPointer(Root::kTop, nullptr, ObjectSlot(&code));
414  if (code == holder) return;
415  holder = Code::unchecked_cast(code);
416  pc = holder->InstructionStart() + pc_offset;
417  *pc_address = pc;
418  if (FLAG_enable_embedded_constant_pool && constant_pool_address) {
419  *constant_pool_address = holder->constant_pool();
420  }
421 }
422 
423 
424 void StackFrame::SetReturnAddressLocationResolver(
425  ReturnAddressLocationResolver resolver) {
426  DCHECK_NULL(return_address_location_resolver_);
427  return_address_location_resolver_ = resolver;
428 }
429 
430 StackFrame::Type StackFrame::ComputeType(const StackFrameIteratorBase* iterator,
431  State* state) {
432  DCHECK_NE(state->fp, kNullAddress);
433 
434  MSAN_MEMORY_IS_INITIALIZED(
435  state->fp + CommonFrameConstants::kContextOrFrameTypeOffset,
436  kPointerSize);
437  intptr_t marker = Memory<intptr_t>(
438  state->fp + CommonFrameConstants::kContextOrFrameTypeOffset);
439  if (!iterator->can_access_heap_objects_) {
440  // TODO(titzer): "can_access_heap_objects" is kind of bogus. It really
441  // means that we are being called from the profiler, which can interrupt
442  // the VM with a signal at any arbitrary instruction, with essentially
443  // anything on the stack. So basically none of these checks are 100%
444  // reliable.
445  MSAN_MEMORY_IS_INITIALIZED(
446  state->fp + StandardFrameConstants::kFunctionOffset, kPointerSize);
447  Object* maybe_function =
448  Memory<Object*>(state->fp + StandardFrameConstants::kFunctionOffset);
449  if (!StackFrame::IsTypeMarker(marker)) {
450  if (maybe_function->IsSmi()) {
451  return NATIVE;
452  } else if (IsInterpreterFramePc(iterator->isolate(), *(state->pc_address),
453  state)) {
454  return INTERPRETED;
455  } else {
456  return OPTIMIZED;
457  }
458  }
459  } else {
460  Address pc = *(state->pc_address);
461  // If the {pc} does not point into WebAssembly code we can rely on the
462  // returned {wasm_code} to be null and fall back to {GetContainingCode}.
463  wasm::WasmCode* wasm_code =
464  iterator->isolate()->wasm_engine()->code_manager()->LookupCode(pc);
465  if (wasm_code != nullptr) {
466  switch (wasm_code->kind()) {
467  case wasm::WasmCode::kFunction:
468  return WASM_COMPILED;
469  case wasm::WasmCode::kWasmToJsWrapper:
470  return WASM_TO_JS;
471  case wasm::WasmCode::kLazyStub:
472  return WASM_COMPILE_LAZY;
473  case wasm::WasmCode::kRuntimeStub:
474  return STUB;
475  case wasm::WasmCode::kInterpreterEntry:
476  return WASM_INTERPRETER_ENTRY;
477  default:
478  UNREACHABLE();
479  }
480  } else {
481  // Look up the code object to figure out the type of the stack frame.
482  Code code_obj = GetContainingCode(iterator->isolate(), pc);
483  if (!code_obj.is_null()) {
484  switch (code_obj->kind()) {
485  case Code::BUILTIN:
486  if (StackFrame::IsTypeMarker(marker)) break;
487  if (code_obj->is_interpreter_trampoline_builtin()) {
488  return INTERPRETED;
489  }
490  if (code_obj->is_turbofanned()) {
491  // TODO(bmeurer): We treat frames for BUILTIN Code objects as
492  // OptimizedFrame for now (all the builtins with JavaScript
493  // linkage are actually generated with TurboFan currently, so
494  // this is sound).
495  return OPTIMIZED;
496  }
497  return BUILTIN;
498  case Code::OPTIMIZED_FUNCTION:
499  return OPTIMIZED;
500  case Code::WASM_FUNCTION:
501  return WASM_COMPILED;
502  case Code::WASM_TO_JS_FUNCTION:
503  return WASM_TO_JS;
504  case Code::JS_TO_WASM_FUNCTION:
505  return JS_TO_WASM;
506  case Code::WASM_INTERPRETER_ENTRY:
507  return WASM_INTERPRETER_ENTRY;
508  case Code::C_WASM_ENTRY:
509  return C_WASM_ENTRY;
510  default:
511  // All other types should have an explicit marker
512  break;
513  }
514  } else {
515  return NATIVE;
516  }
517  }
518  }
519  DCHECK(StackFrame::IsTypeMarker(marker));
520  StackFrame::Type candidate = StackFrame::MarkerToType(marker);
521  switch (candidate) {
522  case ENTRY:
523  case CONSTRUCT_ENTRY:
524  case EXIT:
525  case BUILTIN_CONTINUATION:
526  case JAVA_SCRIPT_BUILTIN_CONTINUATION:
527  case JAVA_SCRIPT_BUILTIN_CONTINUATION_WITH_CATCH:
528  case BUILTIN_EXIT:
529  case STUB:
530  case INTERNAL:
531  case CONSTRUCT:
532  case ARGUMENTS_ADAPTOR:
533  case WASM_TO_JS:
534  case WASM_COMPILED:
535  return candidate;
536  case JS_TO_WASM:
537  case OPTIMIZED:
538  case INTERPRETED:
539  default:
540  // Unoptimized and optimized JavaScript frames, including
541  // interpreted frames, should never have a StackFrame::Type
542  // marker. If we find one, we're likely being called from the
543  // profiler in a bogus stack frame.
544  return NATIVE;
545  }
546 }
547 
548 
549 #ifdef DEBUG
550 bool StackFrame::can_access_heap_objects() const {
551  return iterator_->can_access_heap_objects_;
552 }
553 #endif
554 
555 
556 StackFrame::Type StackFrame::GetCallerState(State* state) const {
557  ComputeCallerState(state);
558  return ComputeType(iterator_, state);
559 }
560 
561 
562 Address StackFrame::UnpaddedFP() const {
563  return fp();
564 }
565 
566 Code NativeFrame::unchecked_code() const { return Code(); }
567 
568 void NativeFrame::ComputeCallerState(State* state) const {
569  state->sp = caller_sp();
570  state->fp = Memory<Address>(fp() + CommonFrameConstants::kCallerFPOffset);
571  state->pc_address = ResolveReturnAddressLocation(
572  reinterpret_cast<Address*>(fp() + CommonFrameConstants::kCallerPCOffset));
573  state->callee_pc_address = nullptr;
574  state->constant_pool_address = nullptr;
575 }
576 
577 Code EntryFrame::unchecked_code() const {
578  return isolate()->heap()->js_entry_code();
579 }
580 
581 
582 void EntryFrame::ComputeCallerState(State* state) const {
583  GetCallerState(state);
584 }
585 
586 
587 StackFrame::Type EntryFrame::GetCallerState(State* state) const {
588  const int offset = EntryFrameConstants::kCallerFPOffset;
589  Address fp = Memory<Address>(this->fp() + offset);
590  return ExitFrame::GetStateForFramePointer(fp, state);
591 }
592 
593 Code ConstructEntryFrame::unchecked_code() const {
594  return isolate()->heap()->js_construct_entry_code();
595 }
596 
597 
598 Object*& ExitFrame::code_slot() const {
599  const int offset = ExitFrameConstants::kCodeOffset;
600  return Memory<Object*>(fp() + offset);
601 }
602 
603 Code ExitFrame::unchecked_code() const {
604  return Code::unchecked_cast(code_slot());
605 }
606 
607 void ExitFrame::ComputeCallerState(State* state) const {
608  // Set up the caller state.
609  state->sp = caller_sp();
610  state->fp = Memory<Address>(fp() + ExitFrameConstants::kCallerFPOffset);
611  state->pc_address = ResolveReturnAddressLocation(
612  reinterpret_cast<Address*>(fp() + ExitFrameConstants::kCallerPCOffset));
613  state->callee_pc_address = nullptr;
614  if (FLAG_enable_embedded_constant_pool) {
615  state->constant_pool_address = reinterpret_cast<Address*>(
616  fp() + ExitFrameConstants::kConstantPoolOffset);
617  }
618 }
619 
620 
621 void ExitFrame::Iterate(RootVisitor* v) const {
622  // The arguments are traversed as part of the expression stack of
623  // the calling frame.
624  IteratePc(v, pc_address(), constant_pool_address(), LookupCode());
625  v->VisitRootPointer(Root::kTop, nullptr, ObjectSlot(&code_slot()));
626 }
627 
628 
629 Address ExitFrame::GetCallerStackPointer() const {
630  return fp() + ExitFrameConstants::kCallerSPOffset;
631 }
632 
633 
634 StackFrame::Type ExitFrame::GetStateForFramePointer(Address fp, State* state) {
635  if (fp == 0) return NONE;
636  Address sp = ComputeStackPointer(fp);
637  FillState(fp, sp, state);
638  DCHECK_NE(*state->pc_address, kNullAddress);
639 
640  return ComputeFrameType(fp);
641 }
642 
643 StackFrame::Type ExitFrame::ComputeFrameType(Address fp) {
644  // Distinguish between between regular and builtin exit frames.
645  // Default to EXIT in all hairy cases (e.g., when called from profiler).
646  const int offset = ExitFrameConstants::kFrameTypeOffset;
647  Object* marker = Memory<Object*>(fp + offset);
648 
649  if (!marker->IsSmi()) {
650  return EXIT;
651  }
652 
653  intptr_t marker_int = bit_cast<intptr_t>(marker);
654 
655  StackFrame::Type frame_type = static_cast<StackFrame::Type>(marker_int >> 1);
656  if (frame_type == EXIT || frame_type == BUILTIN_EXIT) {
657  return frame_type;
658  }
659 
660  return EXIT;
661 }
662 
663 Address ExitFrame::ComputeStackPointer(Address fp) {
664  MSAN_MEMORY_IS_INITIALIZED(fp + ExitFrameConstants::kSPOffset, kPointerSize);
665  return Memory<Address>(fp + ExitFrameConstants::kSPOffset);
666 }
667 
668 void ExitFrame::FillState(Address fp, Address sp, State* state) {
669  state->sp = sp;
670  state->fp = fp;
671  state->pc_address = ResolveReturnAddressLocation(
672  reinterpret_cast<Address*>(sp - 1 * kPCOnStackSize));
673  state->callee_pc_address = nullptr;
674  // The constant pool recorded in the exit frame is not associated
675  // with the pc in this state (the return address into a C entry
676  // stub). ComputeCallerState will retrieve the constant pool
677  // together with the associated caller pc.
678  state->constant_pool_address = nullptr;
679 }
680 
681 JSFunction* BuiltinExitFrame::function() const {
682  return JSFunction::cast(target_slot_object());
683 }
684 
685 Object* BuiltinExitFrame::receiver() const { return receiver_slot_object(); }
686 
687 bool BuiltinExitFrame::IsConstructor() const {
688  return !new_target_slot_object()->IsUndefined(isolate());
689 }
690 
691 Object* BuiltinExitFrame::GetParameter(int i) const {
692  DCHECK(i >= 0 && i < ComputeParametersCount());
693  int offset =
694  BuiltinExitFrameConstants::kFirstArgumentOffset + i * kPointerSize;
695  return Memory<Object*>(fp() + offset);
696 }
697 
698 int BuiltinExitFrame::ComputeParametersCount() const {
699  Object* argc_slot = argc_slot_object();
700  DCHECK(argc_slot->IsSmi());
701  // Argc also counts the receiver, target, new target, and argc itself as args,
702  // therefore the real argument count is argc - 4.
703  int argc = Smi::ToInt(argc_slot) - 4;
704  DCHECK_GE(argc, 0);
705  return argc;
706 }
707 
708 namespace {
709 void PrintIndex(StringStream* accumulator, StackFrame::PrintMode mode,
710  int index) {
711  accumulator->Add((mode == StackFrame::OVERVIEW) ? "%5d: " : "[%d]: ", index);
712 }
713 
714 const char* StringForStackFrameType(StackFrame::Type type) {
715  switch (type) {
716 #define CASE(value, name) \
717  case StackFrame::value: \
718  return #name;
719  STACK_FRAME_TYPE_LIST(CASE)
720 #undef CASE
721  default:
722  UNREACHABLE();
723  }
724 }
725 } // namespace
726 
727 void StackFrame::Print(StringStream* accumulator, PrintMode mode,
728  int index) const {
729  DisallowHeapAllocation no_gc;
730  PrintIndex(accumulator, mode, index);
731  accumulator->Add(StringForStackFrameType(type()));
732  accumulator->Add(" [pc: %p]\n", reinterpret_cast<void*>(pc()));
733 }
734 
735 void BuiltinExitFrame::Print(StringStream* accumulator, PrintMode mode,
736  int index) const {
737  DisallowHeapAllocation no_gc;
738  Object* receiver = this->receiver();
739  JSFunction* function = this->function();
740 
741  accumulator->PrintSecurityTokenIfChanged(function);
742  PrintIndex(accumulator, mode, index);
743  accumulator->Add("builtin exit frame: ");
744  Code code;
745  if (IsConstructor()) accumulator->Add("new ");
746  accumulator->PrintFunction(function, receiver, &code);
747 
748  accumulator->Add("(this=%o", receiver);
749 
750  // Print the parameters.
751  int parameters_count = ComputeParametersCount();
752  for (int i = 0; i < parameters_count; i++) {
753  accumulator->Add(",%o", GetParameter(i));
754  }
755 
756  accumulator->Add(")\n\n");
757 }
758 
759 Address StandardFrame::GetExpressionAddress(int n) const {
760  const int offset = StandardFrameConstants::kExpressionsOffset;
761  return fp() + offset - n * kPointerSize;
762 }
763 
764 Address InterpretedFrame::GetExpressionAddress(int n) const {
765  const int offset = InterpreterFrameConstants::kExpressionsOffset;
766  return fp() + offset - n * kPointerSize;
767 }
768 
769 Script* StandardFrame::script() const {
770  // This should only be called on frames which override this method.
771  DCHECK(false);
772  return nullptr;
773 }
774 
775 Object* StandardFrame::receiver() const {
776  return ReadOnlyRoots(isolate()).undefined_value();
777 }
778 
779 Object* StandardFrame::context() const {
780  return ReadOnlyRoots(isolate()).undefined_value();
781 }
782 
783 int StandardFrame::position() const {
784  AbstractCode code = AbstractCode::cast(LookupCode());
785  int code_offset = static_cast<int>(pc() - code->InstructionStart());
786  return code->SourcePosition(code_offset);
787 }
788 
789 int StandardFrame::ComputeExpressionsCount() const {
790  Address base = GetExpressionAddress(0);
791  Address limit = sp() - kPointerSize;
792  DCHECK(base >= limit); // stack grows downwards
793  // Include register-allocated locals in number of expressions.
794  return static_cast<int>((base - limit) / kPointerSize);
795 }
796 
797 Object* StandardFrame::GetParameter(int index) const {
798  // StandardFrame does not define any parameters.
799  UNREACHABLE();
800 }
801 
802 int StandardFrame::ComputeParametersCount() const { return 0; }
803 
804 void StandardFrame::ComputeCallerState(State* state) const {
805  state->sp = caller_sp();
806  state->fp = caller_fp();
807  state->pc_address = ResolveReturnAddressLocation(
808  reinterpret_cast<Address*>(ComputePCAddress(fp())));
809  state->callee_pc_address = pc_address();
810  state->constant_pool_address =
811  reinterpret_cast<Address*>(ComputeConstantPoolAddress(fp()));
812 }
813 
814 
815 bool StandardFrame::IsConstructor() const { return false; }
816 
817 void StandardFrame::Summarize(std::vector<FrameSummary>* functions) const {
818  // This should only be called on frames which override this method.
819  UNREACHABLE();
820 }
821 
822 void StandardFrame::IterateCompiledFrame(RootVisitor* v) const {
823  // Make sure that we're not doing "safe" stack frame iteration. We cannot
824  // possibly find pointers in optimized frames in that state.
825  DCHECK(can_access_heap_objects());
826 
827  // Find the code and compute the safepoint information.
828  Address inner_pointer = pc();
829  const wasm::WasmCode* wasm_code =
830  isolate()->wasm_engine()->code_manager()->LookupCode(inner_pointer);
831  SafepointEntry safepoint_entry;
832  uint32_t stack_slots;
833  Code code;
834  bool has_tagged_params = false;
835  if (wasm_code != nullptr) {
836  SafepointTable table(wasm_code->instruction_start(),
837  wasm_code->safepoint_table_offset(),
838  wasm_code->stack_slots());
839  safepoint_entry = table.FindEntry(inner_pointer);
840  stack_slots = wasm_code->stack_slots();
841  has_tagged_params = wasm_code->kind() != wasm::WasmCode::kFunction;
842  } else {
843  InnerPointerToCodeCache::InnerPointerToCodeCacheEntry* entry =
844  isolate()->inner_pointer_to_code_cache()->GetCacheEntry(inner_pointer);
845  if (!entry->safepoint_entry.is_valid()) {
846  entry->safepoint_entry = entry->code->GetSafepointEntry(inner_pointer);
847  DCHECK(entry->safepoint_entry.is_valid());
848  } else {
849  DCHECK(entry->safepoint_entry.Equals(
850  entry->code->GetSafepointEntry(inner_pointer)));
851  }
852 
853  code = entry->code;
854  safepoint_entry = entry->safepoint_entry;
855  stack_slots = code->stack_slots();
856  has_tagged_params = code->has_tagged_params();
857  }
858  uint32_t slot_space = stack_slots * kPointerSize;
859 
860  // Determine the fixed header and spill slot area size.
861  int frame_header_size = StandardFrameConstants::kFixedFrameSizeFromFp;
862  intptr_t marker =
863  Memory<intptr_t>(fp() + CommonFrameConstants::kContextOrFrameTypeOffset);
864  if (StackFrame::IsTypeMarker(marker)) {
865  StackFrame::Type candidate = StackFrame::MarkerToType(marker);
866  switch (candidate) {
867  case ENTRY:
868  case CONSTRUCT_ENTRY:
869  case EXIT:
870  case BUILTIN_CONTINUATION:
871  case JAVA_SCRIPT_BUILTIN_CONTINUATION:
872  case JAVA_SCRIPT_BUILTIN_CONTINUATION_WITH_CATCH:
873  case BUILTIN_EXIT:
874  case ARGUMENTS_ADAPTOR:
875  case STUB:
876  case INTERNAL:
877  case CONSTRUCT:
878  case JS_TO_WASM:
879  case C_WASM_ENTRY:
880  frame_header_size = TypedFrameConstants::kFixedFrameSizeFromFp;
881  break;
882  case WASM_TO_JS:
883  case WASM_COMPILED:
884  case WASM_INTERPRETER_ENTRY:
885  case WASM_COMPILE_LAZY:
886  frame_header_size = WasmCompiledFrameConstants::kFixedFrameSizeFromFp;
887  break;
888  case OPTIMIZED:
889  case INTERPRETED:
890  case BUILTIN:
891  // These frame types have a context, but they are actually stored
892  // in the place on the stack that one finds the frame type.
893  UNREACHABLE();
894  break;
895  case NATIVE:
896  case NONE:
897  case NUMBER_OF_TYPES:
898  case MANUAL:
899  UNREACHABLE();
900  break;
901  }
902  }
903  slot_space -=
904  (frame_header_size + StandardFrameConstants::kFixedFrameSizeAboveFp);
905 
906  ObjectSlot frame_header_base(&Memory<Object*>(fp() - frame_header_size));
907  ObjectSlot frame_header_limit(
908  &Memory<Object*>(fp() - StandardFrameConstants::kCPSlotSize));
909  ObjectSlot parameters_base(&Memory<Object*>(sp()));
910  ObjectSlot parameters_limit(frame_header_base.address() - slot_space);
911 
912  // Visit the parameters that may be on top of the saved registers.
913  if (safepoint_entry.argument_count() > 0) {
914  v->VisitRootPointers(Root::kTop, nullptr, parameters_base,
915  parameters_base + safepoint_entry.argument_count());
916  parameters_base += safepoint_entry.argument_count();
917  }
918 
919  // Skip saved double registers.
920  if (safepoint_entry.has_doubles()) {
921  // Number of doubles not known at snapshot time.
922  DCHECK(!isolate()->serializer_enabled());
923  parameters_base +=
924  RegisterConfiguration::Default()->num_allocatable_double_registers() *
925  kDoubleSize / kPointerSize;
926  }
927 
928  // Visit the registers that contain pointers if any.
929  if (safepoint_entry.HasRegisters()) {
930  for (int i = kNumSafepointRegisters - 1; i >=0; i--) {
931  if (safepoint_entry.HasRegisterAt(i)) {
932  int reg_stack_index = MacroAssembler::SafepointRegisterStackIndex(i);
933  v->VisitRootPointer(Root::kTop, nullptr,
934  parameters_base + reg_stack_index);
935  }
936  }
937  // Skip the words containing the register values.
938  parameters_base += kNumSafepointRegisters;
939  }
940 
941  // We're done dealing with the register bits.
942  uint8_t* safepoint_bits = safepoint_entry.bits();
943  safepoint_bits += kNumSafepointRegisters >> kBitsPerByteLog2;
944 
945  // Visit the rest of the parameters if they are tagged.
946  if (has_tagged_params) {
947  v->VisitRootPointers(Root::kTop, nullptr, parameters_base,
948  parameters_limit);
949  }
950 
951  // Visit pointer spill slots and locals.
952  for (unsigned index = 0; index < stack_slots; index++) {
953  int byte_index = index >> kBitsPerByteLog2;
954  int bit_index = index & (kBitsPerByte - 1);
955  if ((safepoint_bits[byte_index] & (1U << bit_index)) != 0) {
956  v->VisitRootPointer(Root::kTop, nullptr, parameters_limit + index);
957  }
958  }
959 
960  // For the off-heap code cases, we can skip this.
961  if (!code.is_null()) {
962  // Visit the return address in the callee and incoming arguments.
963  IteratePc(v, pc_address(), constant_pool_address(), code);
964  }
965 
966  // If this frame has JavaScript ABI, visit the context (in stub and JS
967  // frames) and the function (in JS frames). If it has WebAssembly ABI, visit
968  // the instance object.
969  v->VisitRootPointers(Root::kTop, nullptr, frame_header_base,
970  frame_header_limit);
971 }
972 
973 void StubFrame::Iterate(RootVisitor* v) const { IterateCompiledFrame(v); }
974 
975 Code StubFrame::unchecked_code() const {
976  return isolate()->FindCodeObject(pc());
977 }
978 
979 
980 Address StubFrame::GetCallerStackPointer() const {
981  return fp() + ExitFrameConstants::kCallerSPOffset;
982 }
983 
984 
985 int StubFrame::GetNumberOfIncomingArguments() const {
986  return 0;
987 }
988 
989 int StubFrame::LookupExceptionHandlerInTable(int* stack_slots) {
990  Code code = LookupCode();
991  DCHECK(code->is_turbofanned());
992  DCHECK_EQ(code->kind(), Code::BUILTIN);
993  HandlerTable table(code);
994  int pc_offset = static_cast<int>(pc() - code->InstructionStart());
995  *stack_slots = code->stack_slots();
996  return table.LookupReturn(pc_offset);
997 }
998 
999 void OptimizedFrame::Iterate(RootVisitor* v) const { IterateCompiledFrame(v); }
1000 
1001 void JavaScriptFrame::SetParameterValue(int index, Object* value) const {
1002  Memory<Object*>(GetParameterSlot(index)) = value;
1003 }
1004 
1005 
1006 bool JavaScriptFrame::IsConstructor() const {
1007  Address fp = caller_fp();
1008  if (has_adapted_arguments()) {
1009  // Skip the arguments adaptor frame and look at the real caller.
1010  fp = Memory<Address>(fp + StandardFrameConstants::kCallerFPOffset);
1011  }
1012  return IsConstructFrame(fp);
1013 }
1014 
1015 
1016 bool JavaScriptFrame::HasInlinedFrames() const {
1017  std::vector<SharedFunctionInfo*> functions;
1018  GetFunctions(&functions);
1019  return functions.size() > 1;
1020 }
1021 
1022 Code JavaScriptFrame::unchecked_code() const { return function()->code(); }
1023 
1024 int JavaScriptFrame::GetNumberOfIncomingArguments() const {
1025  DCHECK(can_access_heap_objects() &&
1026  isolate()->heap()->gc_state() == Heap::NOT_IN_GC);
1027  return function()->shared()->internal_formal_parameter_count();
1028 }
1029 
1030 int OptimizedFrame::GetNumberOfIncomingArguments() const {
1031  Code code = LookupCode();
1032  if (code->kind() == Code::BUILTIN) {
1033  return static_cast<int>(
1034  Memory<intptr_t>(fp() + OptimizedBuiltinFrameConstants::kArgCOffset));
1035  } else {
1036  return JavaScriptFrame::GetNumberOfIncomingArguments();
1037  }
1038 }
1039 
1040 Address JavaScriptFrame::GetCallerStackPointer() const {
1041  return fp() + StandardFrameConstants::kCallerSPOffset;
1042 }
1043 
1044 void JavaScriptFrame::GetFunctions(
1045  std::vector<SharedFunctionInfo*>* functions) const {
1046  DCHECK(functions->empty());
1047  functions->push_back(function()->shared());
1048 }
1049 
1050 void JavaScriptFrame::GetFunctions(
1051  std::vector<Handle<SharedFunctionInfo>>* functions) const {
1052  DCHECK(functions->empty());
1053  std::vector<SharedFunctionInfo*> raw_functions;
1054  GetFunctions(&raw_functions);
1055  for (const auto& raw_function : raw_functions) {
1056  functions->push_back(
1057  Handle<SharedFunctionInfo>(raw_function, function()->GetIsolate()));
1058  }
1059 }
1060 
1061 void JavaScriptFrame::Summarize(std::vector<FrameSummary>* functions) const {
1062  DCHECK(functions->empty());
1063  Code code = LookupCode();
1064  int offset = static_cast<int>(pc() - code->InstructionStart());
1065  AbstractCode abstract_code = AbstractCode::cast(code);
1066  FrameSummary::JavaScriptFrameSummary summary(isolate(), receiver(),
1067  function(), abstract_code,
1068  offset, IsConstructor());
1069  functions->push_back(summary);
1070 }
1071 
1072 JSFunction* JavaScriptFrame::function() const {
1073  return JSFunction::cast(function_slot_object());
1074 }
1075 
1076 Object* JavaScriptFrame::unchecked_function() const {
1077  // During deoptimization of an optimized function, we may have yet to
1078  // materialize some closures on the stack. The arguments marker object
1079  // marks this case.
1080  DCHECK(function_slot_object()->IsJSFunction() ||
1081  ReadOnlyRoots(isolate()).arguments_marker() == function_slot_object());
1082  return function_slot_object();
1083 }
1084 
1085 Object* JavaScriptFrame::receiver() const { return GetParameter(-1); }
1086 
1087 Object* JavaScriptFrame::context() const {
1088  const int offset = StandardFrameConstants::kContextOffset;
1089  Object* maybe_result = Memory<Object*>(fp() + offset);
1090  DCHECK(!maybe_result->IsSmi());
1091  return maybe_result;
1092 }
1093 
1094 Script* JavaScriptFrame::script() const {
1095  return Script::cast(function()->shared()->script());
1096 }
1097 
1098 int JavaScriptFrame::LookupExceptionHandlerInTable(
1099  int* stack_depth, HandlerTable::CatchPrediction* prediction) {
1100  DCHECK_EQ(0, LookupCode()->handler_table_offset());
1101  DCHECK(!LookupCode()->is_optimized_code());
1102  return -1;
1103 }
1104 
1105 void JavaScriptFrame::PrintFunctionAndOffset(JSFunction* function,
1106  AbstractCode code, int code_offset,
1107  FILE* file,
1108  bool print_line_number) {
1109  PrintF(file, "%s", function->IsOptimized() ? "*" : "~");
1110  function->PrintName(file);
1111  PrintF(file, "+%d", code_offset);
1112  if (print_line_number) {
1113  SharedFunctionInfo* shared = function->shared();
1114  int source_pos = code->SourcePosition(code_offset);
1115  Object* maybe_script = shared->script();
1116  if (maybe_script->IsScript()) {
1117  Script* script = Script::cast(maybe_script);
1118  int line = script->GetLineNumber(source_pos) + 1;
1119  Object* script_name_raw = script->name();
1120  if (script_name_raw->IsString()) {
1121  String script_name = String::cast(script->name());
1122  std::unique_ptr<char[]> c_script_name =
1123  script_name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
1124  PrintF(file, " at %s:%d", c_script_name.get(), line);
1125  } else {
1126  PrintF(file, " at <unknown>:%d", line);
1127  }
1128  } else {
1129  PrintF(file, " at <unknown>:<unknown>");
1130  }
1131  }
1132 }
1133 
1134 void JavaScriptFrame::PrintTop(Isolate* isolate, FILE* file, bool print_args,
1135  bool print_line_number) {
1136  // constructor calls
1137  DisallowHeapAllocation no_allocation;
1138  JavaScriptFrameIterator it(isolate);
1139  while (!it.done()) {
1140  if (it.frame()->is_java_script()) {
1141  JavaScriptFrame* frame = it.frame();
1142  if (frame->IsConstructor()) PrintF(file, "new ");
1143  JSFunction* function = frame->function();
1144  int code_offset = 0;
1145  if (frame->is_interpreted()) {
1146  InterpretedFrame* iframe = reinterpret_cast<InterpretedFrame*>(frame);
1147  code_offset = iframe->GetBytecodeOffset();
1148  } else {
1149  Code code = frame->unchecked_code();
1150  code_offset = static_cast<int>(frame->pc() - code->InstructionStart());
1151  }
1152  PrintFunctionAndOffset(function, function->abstract_code(), code_offset,
1153  file, print_line_number);
1154  if (print_args) {
1155  // function arguments
1156  // (we are intentionally only printing the actually
1157  // supplied parameters, not all parameters required)
1158  PrintF(file, "(this=");
1159  frame->receiver()->ShortPrint(file);
1160  const int length = frame->ComputeParametersCount();
1161  for (int i = 0; i < length; i++) {
1162  PrintF(file, ", ");
1163  frame->GetParameter(i)->ShortPrint(file);
1164  }
1165  PrintF(file, ")");
1166  }
1167  break;
1168  }
1169  it.Advance();
1170  }
1171 }
1172 
1173 void JavaScriptFrame::CollectFunctionAndOffsetForICStats(JSFunction* function,
1174  AbstractCode code,
1175  int code_offset) {
1176  auto ic_stats = ICStats::instance();
1177  ICInfo& ic_info = ic_stats->Current();
1178  SharedFunctionInfo* shared = function->shared();
1179 
1180  ic_info.function_name = ic_stats->GetOrCacheFunctionName(function);
1181  ic_info.script_offset = code_offset;
1182 
1183  int source_pos = code->SourcePosition(code_offset);
1184  Object* maybe_script = shared->script();
1185  if (maybe_script->IsScript()) {
1186  Script* script = Script::cast(maybe_script);
1187  ic_info.line_num = script->GetLineNumber(source_pos) + 1;
1188  ic_info.script_name = ic_stats->GetOrCacheScriptName(script);
1189  }
1190 }
1191 
1192 void JavaScriptFrame::CollectTopFrameForICStats(Isolate* isolate) {
1193  // constructor calls
1194  DisallowHeapAllocation no_allocation;
1195  JavaScriptFrameIterator it(isolate);
1196  ICInfo& ic_info = ICStats::instance()->Current();
1197  while (!it.done()) {
1198  if (it.frame()->is_java_script()) {
1199  JavaScriptFrame* frame = it.frame();
1200  if (frame->IsConstructor()) ic_info.is_constructor = true;
1201  JSFunction* function = frame->function();
1202  int code_offset = 0;
1203  if (frame->is_interpreted()) {
1204  InterpretedFrame* iframe = reinterpret_cast<InterpretedFrame*>(frame);
1205  code_offset = iframe->GetBytecodeOffset();
1206  } else {
1207  Code code = frame->unchecked_code();
1208  code_offset = static_cast<int>(frame->pc() - code->InstructionStart());
1209  }
1210  CollectFunctionAndOffsetForICStats(function, function->abstract_code(),
1211  code_offset);
1212  return;
1213  }
1214  it.Advance();
1215  }
1216 }
1217 
1218 Object* JavaScriptFrame::GetParameter(int index) const {
1219  return Memory<Object*>(GetParameterSlot(index));
1220 }
1221 
1222 int JavaScriptFrame::ComputeParametersCount() const {
1223  return GetNumberOfIncomingArguments();
1224 }
1225 
1226 int JavaScriptBuiltinContinuationFrame::ComputeParametersCount() const {
1227  // Assert that the first allocatable register is also the argument count
1228  // register.
1229  DCHECK_EQ(RegisterConfiguration::Default()->GetAllocatableGeneralCode(0),
1230  kJavaScriptCallArgCountRegister.code());
1231  Object* argc_object =
1232  Memory<Object*>(fp() + BuiltinContinuationFrameConstants::kArgCOffset);
1233  return Smi::ToInt(argc_object);
1234 }
1235 
1236 intptr_t JavaScriptBuiltinContinuationFrame::GetSPToFPDelta() const {
1237  Address height_slot =
1238  fp() + BuiltinContinuationFrameConstants::kFrameSPtoFPDeltaAtDeoptimize;
1239  intptr_t height = Smi::ToInt(Smi(Memory<Address>(height_slot)));
1240  return height;
1241 }
1242 
1243 Object* JavaScriptBuiltinContinuationFrame::context() const {
1244  return Memory<Object*>(
1245  fp() + BuiltinContinuationFrameConstants::kBuiltinContextOffset);
1246 }
1247 
1248 void JavaScriptBuiltinContinuationWithCatchFrame::SetException(
1249  Object* exception) {
1250  Address exception_argument_slot =
1251  fp() + JavaScriptFrameConstants::kLastParameterOffset +
1252  kPointerSize; // Skip over return value slot.
1253 
1254  // Only allow setting exception if previous value was the hole.
1255  CHECK_EQ(ReadOnlyRoots(isolate()).the_hole_value(),
1256  Memory<Object*>(exception_argument_slot));
1257  Memory<Object*>(exception_argument_slot) = exception;
1258 }
1259 
1260 FrameSummary::JavaScriptFrameSummary::JavaScriptFrameSummary(
1261  Isolate* isolate, Object* receiver, JSFunction* function,
1262  AbstractCode abstract_code, int code_offset, bool is_constructor)
1263  : FrameSummaryBase(isolate, FrameSummary::JAVA_SCRIPT),
1264  receiver_(receiver, isolate),
1265  function_(function, isolate),
1266  abstract_code_(abstract_code, isolate),
1267  code_offset_(code_offset),
1268  is_constructor_(is_constructor) {
1269  DCHECK(abstract_code->IsBytecodeArray() ||
1270  Code::cast(abstract_code)->kind() != Code::OPTIMIZED_FUNCTION);
1271 }
1272 
1273 bool FrameSummary::JavaScriptFrameSummary::is_subject_to_debugging() const {
1274  return function()->shared()->IsSubjectToDebugging();
1275 }
1276 
1277 int FrameSummary::JavaScriptFrameSummary::SourcePosition() const {
1278  return abstract_code()->SourcePosition(code_offset());
1279 }
1280 
1281 int FrameSummary::JavaScriptFrameSummary::SourceStatementPosition() const {
1282  return abstract_code()->SourceStatementPosition(code_offset());
1283 }
1284 
1285 Handle<Object> FrameSummary::JavaScriptFrameSummary::script() const {
1286  return handle(function_->shared()->script(), isolate());
1287 }
1288 
1289 Handle<String> FrameSummary::JavaScriptFrameSummary::FunctionName() const {
1290  return JSFunction::GetDebugName(function_);
1291 }
1292 
1293 Handle<Context> FrameSummary::JavaScriptFrameSummary::native_context() const {
1294  return handle(function_->context()->native_context(), isolate());
1295 }
1296 
1297 FrameSummary::WasmFrameSummary::WasmFrameSummary(
1298  Isolate* isolate, FrameSummary::Kind kind,
1299  Handle<WasmInstanceObject> instance, bool at_to_number_conversion)
1300  : FrameSummaryBase(isolate, kind),
1301  wasm_instance_(instance),
1302  at_to_number_conversion_(at_to_number_conversion) {}
1303 
1304 Handle<Object> FrameSummary::WasmFrameSummary::receiver() const {
1305  return wasm_instance_->GetIsolate()->global_proxy();
1306 }
1307 
1308 #define WASM_SUMMARY_DISPATCH(type, name) \
1309  type FrameSummary::WasmFrameSummary::name() const { \
1310  DCHECK(kind() == Kind::WASM_COMPILED || kind() == Kind::WASM_INTERPRETED); \
1311  return kind() == Kind::WASM_COMPILED \
1312  ? static_cast<const WasmCompiledFrameSummary*>(this)->name() \
1313  : static_cast<const WasmInterpretedFrameSummary*>(this) \
1314  ->name(); \
1315  }
1316 
1317 WASM_SUMMARY_DISPATCH(uint32_t, function_index)
1318 WASM_SUMMARY_DISPATCH(int, byte_offset)
1319 
1320 #undef WASM_SUMMARY_DISPATCH
1321 
1322 int FrameSummary::WasmFrameSummary::SourcePosition() const {
1323  Handle<WasmModuleObject> module_object(wasm_instance()->module_object(),
1324  isolate());
1325  return WasmModuleObject::GetSourcePosition(module_object, function_index(),
1326  byte_offset(),
1327  at_to_number_conversion());
1328 }
1329 
1330 Handle<Script> FrameSummary::WasmFrameSummary::script() const {
1331  return handle(wasm_instance()->module_object()->script(),
1332  wasm_instance()->GetIsolate());
1333 }
1334 
1335 Handle<String> FrameSummary::WasmFrameSummary::FunctionName() const {
1336  Handle<WasmModuleObject> module_object(wasm_instance()->module_object(),
1337  isolate());
1338  return WasmModuleObject::GetFunctionName(isolate(), module_object,
1339  function_index());
1340 }
1341 
1342 Handle<Context> FrameSummary::WasmFrameSummary::native_context() const {
1343  return handle(wasm_instance()->native_context(), isolate());
1344 }
1345 
1346 FrameSummary::WasmCompiledFrameSummary::WasmCompiledFrameSummary(
1347  Isolate* isolate, Handle<WasmInstanceObject> instance, wasm::WasmCode* code,
1348  int code_offset, bool at_to_number_conversion)
1349  : WasmFrameSummary(isolate, WASM_COMPILED, instance,
1350  at_to_number_conversion),
1351  code_(code),
1352  code_offset_(code_offset) {}
1353 
1354 uint32_t FrameSummary::WasmCompiledFrameSummary::function_index() const {
1355  return code()->index();
1356 }
1357 
1358 int FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
1359  const wasm::WasmCode* code, int offset) {
1360  int position = 0;
1361  // Subtract one because the current PC is one instruction after the call site.
1362  offset--;
1363  for (SourcePositionTableIterator iterator(code->source_positions());
1364  !iterator.done() && iterator.code_offset() <= offset;
1365  iterator.Advance()) {
1366  position = iterator.source_position().ScriptOffset();
1367  }
1368  return position;
1369 }
1370 
1371 int FrameSummary::WasmCompiledFrameSummary::byte_offset() const {
1372  return GetWasmSourcePosition(code_, code_offset());
1373 }
1374 
1375 FrameSummary::WasmInterpretedFrameSummary::WasmInterpretedFrameSummary(
1376  Isolate* isolate, Handle<WasmInstanceObject> instance,
1377  uint32_t function_index, int byte_offset)
1378  : WasmFrameSummary(isolate, WASM_INTERPRETED, instance, false),
1379  function_index_(function_index),
1380  byte_offset_(byte_offset) {}
1381 
1382 FrameSummary::~FrameSummary() {
1383 #define FRAME_SUMMARY_DESTR(kind, type, field, desc) \
1384  case kind: \
1385  field.~type(); \
1386  break;
1387  switch (base_.kind()) {
1388  FRAME_SUMMARY_VARIANTS(FRAME_SUMMARY_DESTR)
1389  default:
1390  UNREACHABLE();
1391  }
1392 #undef FRAME_SUMMARY_DESTR
1393 }
1394 
1395 FrameSummary FrameSummary::GetTop(const StandardFrame* frame) {
1396  std::vector<FrameSummary> frames;
1397  frame->Summarize(&frames);
1398  DCHECK_LT(0, frames.size());
1399  return frames.back();
1400 }
1401 
1402 FrameSummary FrameSummary::GetBottom(const StandardFrame* frame) {
1403  return Get(frame, 0);
1404 }
1405 
1406 FrameSummary FrameSummary::GetSingle(const StandardFrame* frame) {
1407  std::vector<FrameSummary> frames;
1408  frame->Summarize(&frames);
1409  DCHECK_EQ(1, frames.size());
1410  return frames.front();
1411 }
1412 
1413 FrameSummary FrameSummary::Get(const StandardFrame* frame, int index) {
1414  DCHECK_LE(0, index);
1415  std::vector<FrameSummary> frames;
1416  frame->Summarize(&frames);
1417  DCHECK_GT(frames.size(), index);
1418  return frames[index];
1419 }
1420 
1421 #define FRAME_SUMMARY_DISPATCH(ret, name) \
1422  ret FrameSummary::name() const { \
1423  switch (base_.kind()) { \
1424  case JAVA_SCRIPT: \
1425  return java_script_summary_.name(); \
1426  case WASM_COMPILED: \
1427  return wasm_compiled_summary_.name(); \
1428  case WASM_INTERPRETED: \
1429  return wasm_interpreted_summary_.name(); \
1430  default: \
1431  UNREACHABLE(); \
1432  return ret{}; \
1433  } \
1434  }
1435 
1436 FRAME_SUMMARY_DISPATCH(Handle<Object>, receiver)
1437 FRAME_SUMMARY_DISPATCH(int, code_offset)
1438 FRAME_SUMMARY_DISPATCH(bool, is_constructor)
1439 FRAME_SUMMARY_DISPATCH(bool, is_subject_to_debugging)
1440 FRAME_SUMMARY_DISPATCH(Handle<Object>, script)
1441 FRAME_SUMMARY_DISPATCH(int, SourcePosition)
1442 FRAME_SUMMARY_DISPATCH(int, SourceStatementPosition)
1443 FRAME_SUMMARY_DISPATCH(Handle<String>, FunctionName)
1444 FRAME_SUMMARY_DISPATCH(Handle<Context>, native_context)
1445 
1446 #undef FRAME_SUMMARY_DISPATCH
1447 
1448 void OptimizedFrame::Summarize(std::vector<FrameSummary>* frames) const {
1449  DCHECK(frames->empty());
1450  DCHECK(is_optimized());
1451 
1452  // Delegate to JS frame in absence of turbofan deoptimization.
1453  // TODO(turbofan): Revisit once we support deoptimization across the board.
1454  Code code = LookupCode();
1455  if (code->kind() == Code::BUILTIN) {
1456  return JavaScriptFrame::Summarize(frames);
1457  }
1458 
1459  int deopt_index = Safepoint::kNoDeoptimizationIndex;
1460  DeoptimizationData const data = GetDeoptimizationData(&deopt_index);
1461  if (deopt_index == Safepoint::kNoDeoptimizationIndex) {
1462  CHECK(data.is_null());
1463  FATAL("Missing deoptimization information for OptimizedFrame::Summarize.");
1464  }
1465 
1466  // Prepare iteration over translation. Note that the below iteration might
1467  // materialize objects without storing them back to the Isolate, this will
1468  // lead to objects being re-materialized again for each summary.
1469  TranslatedState translated(this);
1470  translated.Prepare(fp());
1471 
1472  // We create the summary in reverse order because the frames
1473  // in the deoptimization translation are ordered bottom-to-top.
1474  bool is_constructor = IsConstructor();
1475  for (auto it = translated.begin(); it != translated.end(); it++) {
1476  if (it->kind() == TranslatedFrame::kInterpretedFunction ||
1477  it->kind() == TranslatedFrame::kJavaScriptBuiltinContinuation ||
1478  it->kind() ==
1479  TranslatedFrame::kJavaScriptBuiltinContinuationWithCatch) {
1480  Handle<SharedFunctionInfo> shared_info = it->shared_info();
1481 
1482  // The translation commands are ordered and the function is always
1483  // at the first position, and the receiver is next.
1484  TranslatedFrame::iterator translated_values = it->begin();
1485 
1486  // Get or materialize the correct function in the optimized frame.
1487  Handle<JSFunction> function =
1488  Handle<JSFunction>::cast(translated_values->GetValue());
1489  translated_values++;
1490 
1491  // Get or materialize the correct receiver in the optimized frame.
1492  Handle<Object> receiver = translated_values->GetValue();
1493  translated_values++;
1494 
1495  // Determine the underlying code object and the position within it from
1496  // the translation corresponding to the frame type in question.
1497  Handle<AbstractCode> abstract_code;
1498  unsigned code_offset;
1499  if (it->kind() == TranslatedFrame::kJavaScriptBuiltinContinuation ||
1500  it->kind() ==
1501  TranslatedFrame::kJavaScriptBuiltinContinuationWithCatch) {
1502  code_offset = 0;
1503  abstract_code =
1504  handle(AbstractCode::cast(isolate()->builtins()->builtin(
1505  Builtins::GetBuiltinFromBailoutId(it->node_id()))),
1506  isolate());
1507  } else {
1508  DCHECK_EQ(it->kind(), TranslatedFrame::kInterpretedFunction);
1509  code_offset = it->node_id().ToInt(); // Points to current bytecode.
1510  abstract_code = handle(shared_info->abstract_code(), isolate());
1511  }
1512 
1513  // Append full summary of the encountered JS frame.
1514  FrameSummary::JavaScriptFrameSummary summary(isolate(), *receiver,
1515  *function, *abstract_code,
1516  code_offset, is_constructor);
1517  frames->push_back(summary);
1518  is_constructor = false;
1519  } else if (it->kind() == TranslatedFrame::kConstructStub) {
1520  // The next encountered JS frame will be marked as a constructor call.
1521  DCHECK(!is_constructor);
1522  is_constructor = true;
1523  }
1524  }
1525 }
1526 
1527 
1528 int OptimizedFrame::LookupExceptionHandlerInTable(
1529  int* stack_slots, HandlerTable::CatchPrediction* prediction) {
1530  // We cannot perform exception prediction on optimized code. Instead, we need
1531  // to use FrameSummary to find the corresponding code offset in unoptimized
1532  // code to perform prediction there.
1533  DCHECK_NULL(prediction);
1534  Code code = LookupCode();
1535  HandlerTable table(code);
1536  int pc_offset = static_cast<int>(pc() - code->InstructionStart());
1537  if (stack_slots) *stack_slots = code->stack_slots();
1538 
1539  // When the return pc has been replaced by a trampoline there won't be
1540  // a handler for this trampoline. Thus we need to use the return pc that
1541  // _used to be_ on the stack to get the right ExceptionHandler.
1542  if (code->kind() == Code::OPTIMIZED_FUNCTION &&
1543  code->marked_for_deoptimization()) {
1544  SafepointTable safepoints(code);
1545  pc_offset = safepoints.find_return_pc(pc_offset);
1546  }
1547  return table.LookupReturn(pc_offset);
1548 }
1549 
1550 DeoptimizationData OptimizedFrame::GetDeoptimizationData(
1551  int* deopt_index) const {
1552  DCHECK(is_optimized());
1553 
1554  JSFunction* opt_function = function();
1555  Code code = opt_function->code();
1556 
1557  // The code object may have been replaced by lazy deoptimization. Fall
1558  // back to a slow search in this case to find the original optimized
1559  // code object.
1560  if (!code->contains(pc())) {
1561  code = isolate()->heap()->GcSafeFindCodeForInnerPointer(pc());
1562  }
1563  DCHECK(!code.is_null());
1564  DCHECK(code->kind() == Code::OPTIMIZED_FUNCTION);
1565 
1566  SafepointEntry safepoint_entry = code->GetSafepointEntry(pc());
1567  *deopt_index = safepoint_entry.deoptimization_index();
1568  if (*deopt_index != Safepoint::kNoDeoptimizationIndex) {
1569  return DeoptimizationData::cast(code->deoptimization_data());
1570  }
1571  return DeoptimizationData();
1572 }
1573 
1574 Object* OptimizedFrame::receiver() const {
1575  Code code = LookupCode();
1576  if (code->kind() == Code::BUILTIN) {
1577  Address argc_ptr = fp() + OptimizedBuiltinFrameConstants::kArgCOffset;
1578  intptr_t argc = *reinterpret_cast<intptr_t*>(argc_ptr);
1579  intptr_t args_size =
1580  (StandardFrameConstants::kFixedSlotCountAboveFp + argc) * kPointerSize;
1581  Address receiver_ptr = fp() + args_size;
1582  return *ObjectSlot(receiver_ptr);
1583  } else {
1584  return JavaScriptFrame::receiver();
1585  }
1586 }
1587 
1588 void OptimizedFrame::GetFunctions(
1589  std::vector<SharedFunctionInfo*>* functions) const {
1590  DCHECK(functions->empty());
1591  DCHECK(is_optimized());
1592 
1593  // Delegate to JS frame in absence of turbofan deoptimization.
1594  // TODO(turbofan): Revisit once we support deoptimization across the board.
1595  Code code = LookupCode();
1596  if (code->kind() == Code::BUILTIN) {
1597  return JavaScriptFrame::GetFunctions(functions);
1598  }
1599 
1600  DisallowHeapAllocation no_gc;
1601  int deopt_index = Safepoint::kNoDeoptimizationIndex;
1602  DeoptimizationData const data = GetDeoptimizationData(&deopt_index);
1603  DCHECK(!data.is_null());
1604  DCHECK_NE(Safepoint::kNoDeoptimizationIndex, deopt_index);
1605  FixedArray const literal_array = data->LiteralArray();
1606 
1607  TranslationIterator it(data->TranslationByteArray(),
1608  data->TranslationIndex(deopt_index)->value());
1609  Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
1610  DCHECK_EQ(Translation::BEGIN, opcode);
1611  it.Next(); // Skip frame count.
1612  int jsframe_count = it.Next();
1613  it.Next(); // Skip update feedback count.
1614 
1615  // We insert the frames in reverse order because the frames
1616  // in the deoptimization translation are ordered bottom-to-top.
1617  while (jsframe_count != 0) {
1618  opcode = static_cast<Translation::Opcode>(it.Next());
1619  if (opcode == Translation::INTERPRETED_FRAME ||
1620  opcode == Translation::JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME ||
1621  opcode ==
1622  Translation::JAVA_SCRIPT_BUILTIN_CONTINUATION_WITH_CATCH_FRAME) {
1623  it.Next(); // Skip bailout id.
1624  jsframe_count--;
1625 
1626  // The second operand of the frame points to the function.
1627  Object* shared = literal_array->get(it.Next());
1628  functions->push_back(SharedFunctionInfo::cast(shared));
1629 
1630  // Skip over remaining operands to advance to the next opcode.
1631  it.Skip(Translation::NumberOfOperandsFor(opcode) - 2);
1632  } else {
1633  // Skip over operands to advance to the next opcode.
1634  it.Skip(Translation::NumberOfOperandsFor(opcode));
1635  }
1636  }
1637 }
1638 
1639 
1640 int OptimizedFrame::StackSlotOffsetRelativeToFp(int slot_index) {
1641  return StandardFrameConstants::kCallerSPOffset -
1642  ((slot_index + 1) * kPointerSize);
1643 }
1644 
1645 
1646 Object* OptimizedFrame::StackSlotAt(int index) const {
1647  return Memory<Object*>(fp() + StackSlotOffsetRelativeToFp(index));
1648 }
1649 
1650 int InterpretedFrame::position() const {
1651  AbstractCode code = AbstractCode::cast(GetBytecodeArray());
1652  int code_offset = GetBytecodeOffset();
1653  return code->SourcePosition(code_offset);
1654 }
1655 
1656 int InterpretedFrame::LookupExceptionHandlerInTable(
1657  int* context_register, HandlerTable::CatchPrediction* prediction) {
1658  HandlerTable table(GetBytecodeArray());
1659  return table.LookupRange(GetBytecodeOffset(), context_register, prediction);
1660 }
1661 
1662 int InterpretedFrame::GetBytecodeOffset() const {
1663  const int index = InterpreterFrameConstants::kBytecodeOffsetExpressionIndex;
1664  DCHECK_EQ(
1665  InterpreterFrameConstants::kBytecodeOffsetFromFp,
1666  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1667  int raw_offset = Smi::ToInt(GetExpression(index));
1668  return raw_offset - BytecodeArray::kHeaderSize + kHeapObjectTag;
1669 }
1670 
1671 int InterpretedFrame::GetBytecodeOffset(Address fp) {
1672  const int offset = InterpreterFrameConstants::kExpressionsOffset;
1673  const int index = InterpreterFrameConstants::kBytecodeOffsetExpressionIndex;
1674  DCHECK_EQ(
1675  InterpreterFrameConstants::kBytecodeOffsetFromFp,
1676  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1677  Address expression_offset = fp + offset - index * kPointerSize;
1678  int raw_offset = Smi::ToInt(Memory<Object*>(expression_offset));
1679  return raw_offset - BytecodeArray::kHeaderSize + kHeapObjectTag;
1680 }
1681 
1682 void InterpretedFrame::PatchBytecodeOffset(int new_offset) {
1683  const int index = InterpreterFrameConstants::kBytecodeOffsetExpressionIndex;
1684  DCHECK_EQ(
1685  InterpreterFrameConstants::kBytecodeOffsetFromFp,
1686  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1687  int raw_offset = new_offset + BytecodeArray::kHeaderSize - kHeapObjectTag;
1688  SetExpression(index, Smi::FromInt(raw_offset));
1689 }
1690 
1691 BytecodeArray InterpretedFrame::GetBytecodeArray() const {
1692  const int index = InterpreterFrameConstants::kBytecodeArrayExpressionIndex;
1693  DCHECK_EQ(
1694  InterpreterFrameConstants::kBytecodeArrayFromFp,
1695  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1696  return BytecodeArray::cast(GetExpression(index));
1697 }
1698 
1699 void InterpretedFrame::PatchBytecodeArray(BytecodeArray bytecode_array) {
1700  const int index = InterpreterFrameConstants::kBytecodeArrayExpressionIndex;
1701  DCHECK_EQ(
1702  InterpreterFrameConstants::kBytecodeArrayFromFp,
1703  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1704  SetExpression(index, bytecode_array);
1705 }
1706 
1707 Object* InterpretedFrame::ReadInterpreterRegister(int register_index) const {
1708  const int index = InterpreterFrameConstants::kRegisterFileExpressionIndex;
1709  DCHECK_EQ(
1710  InterpreterFrameConstants::kRegisterFileFromFp,
1711  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1712  return GetExpression(index + register_index);
1713 }
1714 
1715 void InterpretedFrame::WriteInterpreterRegister(int register_index,
1716  Object* value) {
1717  const int index = InterpreterFrameConstants::kRegisterFileExpressionIndex;
1718  DCHECK_EQ(
1719  InterpreterFrameConstants::kRegisterFileFromFp,
1720  InterpreterFrameConstants::kExpressionsOffset - index * kPointerSize);
1721  return SetExpression(index + register_index, value);
1722 }
1723 
1724 void InterpretedFrame::Summarize(std::vector<FrameSummary>* functions) const {
1725  DCHECK(functions->empty());
1726  AbstractCode abstract_code = AbstractCode::cast(GetBytecodeArray());
1727  FrameSummary::JavaScriptFrameSummary summary(
1728  isolate(), receiver(), function(), abstract_code, GetBytecodeOffset(),
1729  IsConstructor());
1730  functions->push_back(summary);
1731 }
1732 
1733 int ArgumentsAdaptorFrame::GetNumberOfIncomingArguments() const {
1734  return Smi::ToInt(GetExpression(0));
1735 }
1736 
1737 Code ArgumentsAdaptorFrame::unchecked_code() const {
1738  return isolate()->builtins()->builtin(
1739  Builtins::kArgumentsAdaptorTrampoline);
1740 }
1741 
1742 int BuiltinFrame::GetNumberOfIncomingArguments() const {
1743  return Smi::ToInt(GetExpression(0));
1744 }
1745 
1746 void BuiltinFrame::PrintFrameKind(StringStream* accumulator) const {
1747  accumulator->Add("builtin frame: ");
1748 }
1749 
1750 Address InternalFrame::GetCallerStackPointer() const {
1751  // Internal frames have no arguments. The stack pointer of the
1752  // caller is at a fixed offset from the frame pointer.
1753  return fp() + StandardFrameConstants::kCallerSPOffset;
1754 }
1755 
1756 Code InternalFrame::unchecked_code() const { UNREACHABLE(); }
1757 
1758 void WasmCompiledFrame::Print(StringStream* accumulator, PrintMode mode,
1759  int index) const {
1760  PrintIndex(accumulator, mode, index);
1761  accumulator->Add("WASM [");
1762  accumulator->PrintName(script()->name());
1763  Address instruction_start = isolate()
1764  ->wasm_engine()
1765  ->code_manager()
1766  ->LookupCode(pc())
1767  ->instruction_start();
1768  Vector<const uint8_t> raw_func_name =
1769  module_object()->GetRawFunctionName(function_index());
1770  const int kMaxPrintedFunctionName = 64;
1771  char func_name[kMaxPrintedFunctionName + 1];
1772  int func_name_len = std::min(kMaxPrintedFunctionName, raw_func_name.length());
1773  memcpy(func_name, raw_func_name.start(), func_name_len);
1774  func_name[func_name_len] = '\0';
1775  int pos = position();
1776  const wasm::WasmModule* module = wasm_instance()->module_object()->module();
1777  int func_index = function_index();
1778  int func_code_offset = module->functions[func_index].code.offset();
1779  accumulator->Add("], function #%u ('%s'), pc=%p (+0x%x), pos=%d (+%d)\n",
1780  func_index, func_name, reinterpret_cast<void*>(pc()),
1781  static_cast<int>(pc() - instruction_start), pos,
1782  pos - func_code_offset);
1783  if (mode != OVERVIEW) accumulator->Add("\n");
1784 }
1785 
1786 Code WasmCompiledFrame::unchecked_code() const {
1787  return isolate()->FindCodeObject(pc());
1788 }
1789 
1790 void WasmCompiledFrame::Iterate(RootVisitor* v) const {
1791  IterateCompiledFrame(v);
1792 }
1793 
1794 Address WasmCompiledFrame::GetCallerStackPointer() const {
1795  return fp() + ExitFrameConstants::kCallerSPOffset;
1796 }
1797 
1798 wasm::WasmCode* WasmCompiledFrame::wasm_code() const {
1799  return isolate()->wasm_engine()->code_manager()->LookupCode(pc());
1800 }
1801 
1802 WasmInstanceObject* WasmCompiledFrame::wasm_instance() const {
1803  const int offset = WasmCompiledFrameConstants::kWasmInstanceOffset;
1804  Object* instance = Memory<Object*>(fp() + offset);
1805  return WasmInstanceObject::cast(instance);
1806 }
1807 
1808 WasmModuleObject* WasmCompiledFrame::module_object() const {
1809  return wasm_instance()->module_object();
1810 }
1811 
1812 uint32_t WasmCompiledFrame::function_index() const {
1813  return FrameSummary::GetSingle(this).AsWasmCompiled().function_index();
1814 }
1815 
1816 Script* WasmCompiledFrame::script() const { return module_object()->script(); }
1817 
1818 int WasmCompiledFrame::position() const {
1819  return FrameSummary::GetSingle(this).SourcePosition();
1820 }
1821 
1822 void WasmCompiledFrame::Summarize(std::vector<FrameSummary>* functions) const {
1823  DCHECK(functions->empty());
1824  wasm::WasmCode* code = wasm_code();
1825  int offset = static_cast<int>(pc() - code->instruction_start());
1826  Handle<WasmInstanceObject> instance(wasm_instance(), isolate());
1827  FrameSummary::WasmCompiledFrameSummary summary(
1828  isolate(), instance, code, offset, at_to_number_conversion());
1829  functions->push_back(summary);
1830 }
1831 
1832 bool WasmCompiledFrame::at_to_number_conversion() const {
1833  // Check whether our callee is a WASM_TO_JS frame, and this frame is at the
1834  // ToNumber conversion call.
1835  wasm::WasmCode* code =
1836  callee_pc() != kNullAddress
1837  ? isolate()->wasm_engine()->code_manager()->LookupCode(callee_pc())
1838  : nullptr;
1839  if (!code || code->kind() != wasm::WasmCode::kWasmToJsWrapper) return false;
1840  int offset = static_cast<int>(callee_pc() - code->instruction_start());
1841  int pos = FrameSummary::WasmCompiledFrameSummary::GetWasmSourcePosition(
1842  code, offset);
1843  DCHECK(pos == 0 || pos == 1);
1844  // The imported call has position 0, ToNumber has position 1.
1845  return !!pos;
1846 }
1847 
1848 int WasmCompiledFrame::LookupExceptionHandlerInTable(int* stack_slots) {
1849  DCHECK_NOT_NULL(stack_slots);
1850  wasm::WasmCode* code =
1851  isolate()->wasm_engine()->code_manager()->LookupCode(pc());
1852  if (!code->IsAnonymous() && code->handler_table_offset() > 0) {
1853  HandlerTable table(code->instruction_start(), code->handler_table_offset());
1854  int pc_offset = static_cast<int>(pc() - code->instruction_start());
1855  *stack_slots = static_cast<int>(code->stack_slots());
1856  return table.LookupReturn(pc_offset);
1857  }
1858  return -1;
1859 }
1860 
1861 void WasmInterpreterEntryFrame::Iterate(RootVisitor* v) const {
1862  IterateCompiledFrame(v);
1863 }
1864 
1865 void WasmInterpreterEntryFrame::Print(StringStream* accumulator, PrintMode mode,
1866  int index) const {
1867  PrintIndex(accumulator, mode, index);
1868  accumulator->Add("WASM INTERPRETER ENTRY [");
1869  Script* script = this->script();
1870  accumulator->PrintName(script->name());
1871  accumulator->Add("]");
1872  if (mode != OVERVIEW) accumulator->Add("\n");
1873 }
1874 
1875 void WasmInterpreterEntryFrame::Summarize(
1876  std::vector<FrameSummary>* functions) const {
1877  Handle<WasmInstanceObject> instance(wasm_instance(), isolate());
1878  std::vector<std::pair<uint32_t, int>> interpreted_stack =
1879  instance->debug_info()->GetInterpretedStack(fp());
1880 
1881  for (auto& e : interpreted_stack) {
1882  FrameSummary::WasmInterpretedFrameSummary summary(isolate(), instance,
1883  e.first, e.second);
1884  functions->push_back(summary);
1885  }
1886 }
1887 
1888 Code WasmInterpreterEntryFrame::unchecked_code() const { UNREACHABLE(); }
1889 
1890 WasmInstanceObject* WasmInterpreterEntryFrame::wasm_instance() const {
1891  const int offset = WasmCompiledFrameConstants::kWasmInstanceOffset;
1892  Object* instance = Memory<Object*>(fp() + offset);
1893  return WasmInstanceObject::cast(instance);
1894 }
1895 
1896 WasmDebugInfo* WasmInterpreterEntryFrame::debug_info() const {
1897  return wasm_instance()->debug_info();
1898 }
1899 
1900 WasmModuleObject* WasmInterpreterEntryFrame::module_object() const {
1901  return wasm_instance()->module_object();
1902 }
1903 
1904 Script* WasmInterpreterEntryFrame::script() const {
1905  return module_object()->script();
1906 }
1907 
1908 int WasmInterpreterEntryFrame::position() const {
1909  return FrameSummary::GetBottom(this).AsWasmInterpreted().SourcePosition();
1910 }
1911 
1912 Object* WasmInterpreterEntryFrame::context() const {
1913  return wasm_instance()->native_context();
1914 }
1915 
1916 Address WasmInterpreterEntryFrame::GetCallerStackPointer() const {
1917  return fp() + ExitFrameConstants::kCallerSPOffset;
1918 }
1919 
1920 Code WasmCompileLazyFrame::unchecked_code() const { return Code(); }
1921 
1922 WasmInstanceObject* WasmCompileLazyFrame::wasm_instance() const {
1923  return WasmInstanceObject::cast(*wasm_instance_slot());
1924 }
1925 
1926 ObjectSlot WasmCompileLazyFrame::wasm_instance_slot() const {
1927  const int offset = WasmCompileLazyFrameConstants::kWasmInstanceOffset;
1928  return ObjectSlot(&Memory<Object*>(fp() + offset));
1929 }
1930 
1931 void WasmCompileLazyFrame::Iterate(RootVisitor* v) const {
1932  const int header_size = WasmCompileLazyFrameConstants::kFixedFrameSizeFromFp;
1933  ObjectSlot base(&Memory<Object*>(sp()));
1934  ObjectSlot limit(&Memory<Object*>(fp() - header_size));
1935  v->VisitRootPointers(Root::kTop, nullptr, base, limit);
1936  v->VisitRootPointer(Root::kTop, nullptr, wasm_instance_slot());
1937 }
1938 
1939 Address WasmCompileLazyFrame::GetCallerStackPointer() const {
1940  return fp() + WasmCompileLazyFrameConstants::kCallerSPOffset;
1941 }
1942 
1943 namespace {
1944 
1945 void PrintFunctionSource(StringStream* accumulator, SharedFunctionInfo* shared,
1946  Code code) {
1947  if (FLAG_max_stack_trace_source_length != 0 && !code.is_null()) {
1948  std::ostringstream os;
1949  os << "--------- s o u r c e c o d e ---------\n"
1950  << SourceCodeOf(shared, FLAG_max_stack_trace_source_length)
1951  << "\n-----------------------------------------\n";
1952  accumulator->Add(os.str().c_str());
1953  }
1954 }
1955 
1956 
1957 } // namespace
1958 
1959 
1960 void JavaScriptFrame::Print(StringStream* accumulator,
1961  PrintMode mode,
1962  int index) const {
1963  DisallowHeapAllocation no_gc;
1964  Object* receiver = this->receiver();
1965  JSFunction* function = this->function();
1966 
1967  accumulator->PrintSecurityTokenIfChanged(function);
1968  PrintIndex(accumulator, mode, index);
1969  PrintFrameKind(accumulator);
1970  Code code;
1971  if (IsConstructor()) accumulator->Add("new ");
1972  accumulator->PrintFunction(function, receiver, &code);
1973  accumulator->Add(" [%p]", function);
1974 
1975  // Get scope information for nicer output, if possible. If code is nullptr, or
1976  // doesn't contain scope info, scope_info will return 0 for the number of
1977  // parameters, stack local variables, context local variables, stack slots,
1978  // or context slots.
1979  SharedFunctionInfo* shared = function->shared();
1980  ScopeInfo scope_info = shared->scope_info();
1981  Object* script_obj = shared->script();
1982  if (script_obj->IsScript()) {
1983  Script* script = Script::cast(script_obj);
1984  accumulator->Add(" [");
1985  accumulator->PrintName(script->name());
1986 
1987  if (is_interpreted()) {
1988  const InterpretedFrame* iframe =
1989  reinterpret_cast<const InterpretedFrame*>(this);
1990  BytecodeArray bytecodes = iframe->GetBytecodeArray();
1991  int offset = iframe->GetBytecodeOffset();
1992  int source_pos = AbstractCode::cast(bytecodes)->SourcePosition(offset);
1993  int line = script->GetLineNumber(source_pos) + 1;
1994  accumulator->Add(":%d] [bytecode=%p offset=%d]", line,
1995  reinterpret_cast<void*>(bytecodes.ptr()), offset);
1996  } else {
1997  int function_start_pos = shared->StartPosition();
1998  int line = script->GetLineNumber(function_start_pos) + 1;
1999  accumulator->Add(":~%d] [pc=%p]", line, reinterpret_cast<void*>(pc()));
2000  }
2001  }
2002 
2003  accumulator->Add("(this=%o", receiver);
2004 
2005  // Print the parameters.
2006  int parameters_count = ComputeParametersCount();
2007  for (int i = 0; i < parameters_count; i++) {
2008  accumulator->Add(",");
2009  accumulator->Add("%o", GetParameter(i));
2010  }
2011 
2012  accumulator->Add(")");
2013  if (mode == OVERVIEW) {
2014  accumulator->Add("\n");
2015  return;
2016  }
2017  if (is_optimized()) {
2018  accumulator->Add(" {\n// optimized frame\n");
2019  PrintFunctionSource(accumulator, shared, code);
2020  accumulator->Add("}\n");
2021  return;
2022  }
2023  accumulator->Add(" {\n");
2024 
2025  // Compute the number of locals and expression stack elements.
2026  int heap_locals_count = scope_info->ContextLocalCount();
2027  int expressions_count = ComputeExpressionsCount();
2028 
2029  // Try to get hold of the context of this frame.
2030  Context context;
2031  if (this->context() != nullptr && this->context()->IsContext()) {
2032  context = Context::cast(this->context());
2033  while (context->IsWithContext()) {
2034  context = context->previous();
2035  DCHECK(!context.is_null());
2036  }
2037  }
2038 
2039  // Print heap-allocated local variables.
2040  if (heap_locals_count > 0) {
2041  accumulator->Add(" // heap-allocated locals\n");
2042  }
2043  for (int i = 0; i < heap_locals_count; i++) {
2044  accumulator->Add(" var ");
2045  accumulator->PrintName(scope_info->ContextLocalName(i));
2046  accumulator->Add(" = ");
2047  if (!context.is_null()) {
2048  int index = Context::MIN_CONTEXT_SLOTS + i;
2049  if (index < context->length()) {
2050  accumulator->Add("%o", context->get(index));
2051  } else {
2052  accumulator->Add(
2053  "// warning: missing context slot - inconsistent frame?");
2054  }
2055  } else {
2056  accumulator->Add("// warning: no context found - inconsistent frame?");
2057  }
2058  accumulator->Add("\n");
2059  }
2060 
2061  // Print the expression stack.
2062  if (0 < expressions_count) {
2063  accumulator->Add(" // expression stack (top to bottom)\n");
2064  }
2065  for (int i = expressions_count - 1; i >= 0; i--) {
2066  accumulator->Add(" [%02d] : %o\n", i, GetExpression(i));
2067  }
2068 
2069  PrintFunctionSource(accumulator, shared, code);
2070 
2071  accumulator->Add("}\n\n");
2072 }
2073 
2074 
2075 void ArgumentsAdaptorFrame::Print(StringStream* accumulator,
2076  PrintMode mode,
2077  int index) const {
2078  int actual = ComputeParametersCount();
2079  int expected = -1;
2080  JSFunction* function = this->function();
2081  expected = function->shared()->internal_formal_parameter_count();
2082 
2083  PrintIndex(accumulator, mode, index);
2084  accumulator->Add("arguments adaptor frame: %d->%d", actual, expected);
2085  if (mode == OVERVIEW) {
2086  accumulator->Add("\n");
2087  return;
2088  }
2089  accumulator->Add(" {\n");
2090 
2091  // Print actual arguments.
2092  if (actual > 0) accumulator->Add(" // actual arguments\n");
2093  for (int i = 0; i < actual; i++) {
2094  accumulator->Add(" [%02d] : %o", i, GetParameter(i));
2095  if (expected != -1 && i >= expected) {
2096  accumulator->Add(" // not passed to callee");
2097  }
2098  accumulator->Add("\n");
2099  }
2100 
2101  accumulator->Add("}\n\n");
2102 }
2103 
2104 void EntryFrame::Iterate(RootVisitor* v) const {
2105  IteratePc(v, pc_address(), constant_pool_address(), LookupCode());
2106 }
2107 
2108 void StandardFrame::IterateExpressions(RootVisitor* v) const {
2109  const int offset = StandardFrameConstants::kLastObjectOffset;
2110  ObjectSlot base(&Memory<Object*>(sp()));
2111  ObjectSlot limit(&Memory<Object*>(fp() + offset) + 1);
2112  v->VisitRootPointers(Root::kTop, nullptr, base, limit);
2113 }
2114 
2115 void JavaScriptFrame::Iterate(RootVisitor* v) const {
2116  IterateExpressions(v);
2117  IteratePc(v, pc_address(), constant_pool_address(), LookupCode());
2118 }
2119 
2120 void InternalFrame::Iterate(RootVisitor* v) const {
2121  Code code = LookupCode();
2122  IteratePc(v, pc_address(), constant_pool_address(), code);
2123  // Internal frames typically do not receive any arguments, hence their stack
2124  // only contains tagged pointers.
2125  // We are misusing the has_tagged_params flag here to tell us whether
2126  // the full stack frame contains only tagged pointers or only raw values.
2127  // This is used for the WasmCompileLazy builtin, where we actually pass
2128  // untagged arguments and also store untagged values on the stack.
2129  if (code->has_tagged_params()) IterateExpressions(v);
2130 }
2131 
2132 // -------------------------------------------------------------------------
2133 
2134 namespace {
2135 
2136 uint32_t PcAddressForHashing(Isolate* isolate, Address address) {
2137  if (InstructionStream::PcIsOffHeap(isolate, address)) {
2138  // Ensure that we get predictable hashes for addresses in embedded code.
2139  return EmbeddedData::FromBlob(isolate).AddressForHashing(address);
2140  }
2141  return ObjectAddressForHashing(reinterpret_cast<void*>(address));
2142 }
2143 
2144 } // namespace
2145 
2146 InnerPointerToCodeCache::InnerPointerToCodeCacheEntry*
2147  InnerPointerToCodeCache::GetCacheEntry(Address inner_pointer) {
2148  isolate_->counters()->pc_to_code()->Increment();
2149  DCHECK(base::bits::IsPowerOfTwo(kInnerPointerToCodeCacheSize));
2150  uint32_t hash =
2151  ComputeUnseededHash(PcAddressForHashing(isolate_, inner_pointer));
2152  uint32_t index = hash & (kInnerPointerToCodeCacheSize - 1);
2153  InnerPointerToCodeCacheEntry* entry = cache(index);
2154  if (entry->inner_pointer == inner_pointer) {
2155  isolate_->counters()->pc_to_code_cached()->Increment();
2156  DCHECK(entry->code ==
2157  isolate_->heap()->GcSafeFindCodeForInnerPointer(inner_pointer));
2158  } else {
2159  // Because this code may be interrupted by a profiling signal that
2160  // also queries the cache, we cannot update inner_pointer before the code
2161  // has been set. Otherwise, we risk trying to use a cache entry before
2162  // the code has been computed.
2163  entry->code =
2164  isolate_->heap()->GcSafeFindCodeForInnerPointer(inner_pointer);
2165  entry->safepoint_entry.Reset();
2166  entry->inner_pointer = inner_pointer;
2167  }
2168  return entry;
2169 }
2170 } // namespace internal
2171 } // namespace v8
Definition: libplatform.h:13