V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
execution.cc
1 // Copyright 2014 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/execution.h"
6 
7 #include "src/api-inl.h"
8 #include "src/bootstrapper.h"
9 #include "src/compiler-dispatcher/optimizing-compile-dispatcher.h"
10 #include "src/debug/debug.h"
11 #include "src/isolate-inl.h"
12 #include "src/runtime-profiler.h"
13 #include "src/vm-state-inl.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) {
19  DCHECK_NOT_NULL(isolate_);
20  thread_local_.set_jslimit(kInterruptLimit);
21  thread_local_.set_climit(kInterruptLimit);
22  isolate_->heap()->SetStackLimits();
23 }
24 
25 
26 void StackGuard::reset_limits(const ExecutionAccess& lock) {
27  DCHECK_NOT_NULL(isolate_);
28  thread_local_.set_jslimit(thread_local_.real_jslimit_);
29  thread_local_.set_climit(thread_local_.real_climit_);
30  isolate_->heap()->SetStackLimits();
31 }
32 
33 
34 static void PrintDeserializedCodeInfo(Handle<JSFunction> function) {
35  if (function->code() == function->shared()->GetCode() &&
36  function->shared()->deserialized()) {
37  PrintF("[Running deserialized script");
38  Object* script = function->shared()->script();
39  if (script->IsScript()) {
40  Object* name = Script::cast(script)->name();
41  if (name->IsString()) {
42  PrintF(": %s", String::cast(name)->ToCString().get());
43  }
44  }
45  PrintF("]\n");
46  }
47 }
48 
49 
50 namespace {
51 
52 V8_WARN_UNUSED_RESULT MaybeHandle<Object> Invoke(
53  Isolate* isolate, bool is_construct, Handle<Object> target,
54  Handle<Object> receiver, int argc, Handle<Object> args[],
55  Handle<Object> new_target, Execution::MessageHandling message_handling,
56  Execution::Target execution_target) {
57  DCHECK(!receiver->IsJSGlobalObject());
58 
59 #ifdef USE_SIMULATOR
60  // Simulators use separate stacks for C++ and JS. JS stack overflow checks
61  // are performed whenever a JS function is called. However, it can be the case
62  // that the C++ stack grows faster than the JS stack, resulting in an overflow
63  // there. Add a check here to make that less likely.
64  StackLimitCheck check(isolate);
65  if (check.HasOverflowed()) {
66  isolate->StackOverflow();
67  if (message_handling == Execution::MessageHandling::kReport) {
68  isolate->ReportPendingMessages();
69  }
70  return MaybeHandle<Object>();
71  }
72 #endif
73 
74  // api callbacks can be called directly, unless we want to take the detour
75  // through JS to set up a frame for break-at-entry.
76  if (target->IsJSFunction()) {
77  Handle<JSFunction> function = Handle<JSFunction>::cast(target);
78  if ((!is_construct || function->IsConstructor()) &&
79  function->shared()->IsApiFunction() &&
80  !function->shared()->BreakAtEntry()) {
81  SaveContext save(isolate);
82  isolate->set_context(function->context());
83  DCHECK(function->context()->global_object()->IsJSGlobalObject());
84  if (is_construct) receiver = isolate->factory()->the_hole_value();
85  auto value = Builtins::InvokeApiFunction(
86  isolate, is_construct, function, receiver, argc, args,
87  Handle<HeapObject>::cast(new_target));
88  bool has_exception = value.is_null();
89  DCHECK(has_exception == isolate->has_pending_exception());
90  if (has_exception) {
91  if (message_handling == Execution::MessageHandling::kReport) {
92  isolate->ReportPendingMessages();
93  }
94  return MaybeHandle<Object>();
95  } else {
96  isolate->clear_pending_message();
97  }
98  return value;
99  }
100  }
101 
102  // Entering JavaScript.
103  VMState<JS> state(isolate);
104  CHECK(AllowJavascriptExecution::IsAllowed(isolate));
105  if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) {
106  isolate->ThrowIllegalOperation();
107  if (message_handling == Execution::MessageHandling::kReport) {
108  isolate->ReportPendingMessages();
109  }
110  return MaybeHandle<Object>();
111  }
112  if (!DumpOnJavascriptExecution::IsAllowed(isolate)) {
113  V8::GetCurrentPlatform()->DumpWithoutCrashing();
114  return isolate->factory()->undefined_value();
115  }
116 
117  // Placeholder for return value.
118  Object* value = nullptr;
119 
120  using JSEntryFunction =
121  GeneratedCode<Object*(Object * new_target, Object * target,
122  Object * receiver, int argc, Object*** args)>;
123 
124  Handle<Code> code;
125  switch (execution_target) {
126  case Execution::Target::kCallable:
127  code = is_construct ? isolate->factory()->js_construct_entry_code()
128  : isolate->factory()->js_entry_code();
129  break;
130  case Execution::Target::kRunMicrotasks:
131  code = isolate->factory()->js_run_microtasks_entry_code();
132  break;
133  default:
134  UNREACHABLE();
135  }
136 
137  {
138  // Save and restore context around invocation and block the
139  // allocation of handles without explicit handle scopes.
140  SaveContext save(isolate);
141  SealHandleScope shs(isolate);
142  JSEntryFunction stub_entry =
143  JSEntryFunction::FromAddress(isolate, code->entry());
144 
145  if (FLAG_clear_exceptions_on_js_entry) isolate->clear_pending_exception();
146 
147  // Call the function through the right JS entry stub.
148  Object* orig_func = *new_target;
149  Object* func = *target;
150  Object* recv = *receiver;
151  Object*** argv = reinterpret_cast<Object***>(args);
152  if (FLAG_profile_deserialization && target->IsJSFunction()) {
153  PrintDeserializedCodeInfo(Handle<JSFunction>::cast(target));
154  }
155  RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kJS_Execution);
156  value = stub_entry.Call(orig_func, func, recv, argc, argv);
157  }
158 
159 #ifdef VERIFY_HEAP
160  if (FLAG_verify_heap) {
161  value->ObjectVerify(isolate);
162  }
163 #endif
164 
165  // Update the pending exception flag and return the value.
166  bool has_exception = value->IsException(isolate);
167  DCHECK(has_exception == isolate->has_pending_exception());
168  if (has_exception) {
169  if (message_handling == Execution::MessageHandling::kReport) {
170  isolate->ReportPendingMessages();
171  }
172  return MaybeHandle<Object>();
173  } else {
174  isolate->clear_pending_message();
175  }
176 
177  return Handle<Object>(value, isolate);
178 }
179 
180 MaybeHandle<Object> CallInternal(Isolate* isolate, Handle<Object> callable,
181  Handle<Object> receiver, int argc,
182  Handle<Object> argv[],
183  Execution::MessageHandling message_handling,
184  Execution::Target target) {
185  // Convert calls on global objects to be calls on the global
186  // receiver instead to avoid having a 'this' pointer which refers
187  // directly to a global object.
188  if (receiver->IsJSGlobalObject()) {
189  receiver =
190  handle(Handle<JSGlobalObject>::cast(receiver)->global_proxy(), isolate);
191  }
192  return Invoke(isolate, false, callable, receiver, argc, argv,
193  isolate->factory()->undefined_value(), message_handling,
194  target);
195 }
196 
197 } // namespace
198 
199 // static
200 MaybeHandle<Object> Execution::Call(Isolate* isolate, Handle<Object> callable,
201  Handle<Object> receiver, int argc,
202  Handle<Object> argv[]) {
203  return CallInternal(isolate, callable, receiver, argc, argv,
204  MessageHandling::kReport, Execution::Target::kCallable);
205 }
206 
207 
208 // static
209 MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
210  int argc, Handle<Object> argv[]) {
211  return New(isolate, constructor, constructor, argc, argv);
212 }
213 
214 
215 // static
216 MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
217  Handle<Object> new_target, int argc,
218  Handle<Object> argv[]) {
219  return Invoke(isolate, true, constructor,
220  isolate->factory()->undefined_value(), argc, argv, new_target,
221  MessageHandling::kReport, Execution::Target::kCallable);
222 }
223 
224 MaybeHandle<Object> Execution::TryCall(
225  Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
226  int argc, Handle<Object> args[], MessageHandling message_handling,
227  MaybeHandle<Object>* exception_out, Target target) {
228  bool is_termination = false;
229  MaybeHandle<Object> maybe_result;
230  if (exception_out != nullptr) *exception_out = MaybeHandle<Object>();
231  DCHECK_IMPLIES(message_handling == MessageHandling::kKeepPending,
232  exception_out == nullptr);
233  // Enter a try-block while executing the JavaScript code. To avoid
234  // duplicate error printing it must be non-verbose. Also, to avoid
235  // creating message objects during stack overflow we shouldn't
236  // capture messages.
237  {
238  v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
239  catcher.SetVerbose(false);
240  catcher.SetCaptureMessage(false);
241 
242  maybe_result = CallInternal(isolate, callable, receiver, argc, args,
243  message_handling, target);
244 
245  if (maybe_result.is_null()) {
246  DCHECK(isolate->has_pending_exception());
247  if (isolate->pending_exception() ==
248  ReadOnlyRoots(isolate).termination_exception()) {
249  is_termination = true;
250  } else {
251  if (exception_out != nullptr) {
252  DCHECK(catcher.HasCaught());
253  DCHECK(isolate->external_caught_exception());
254  *exception_out = v8::Utils::OpenHandle(*catcher.Exception());
255  }
256  }
257  if (message_handling == MessageHandling::kReport) {
258  isolate->OptionalRescheduleException(true);
259  }
260  }
261  }
262 
263  // Re-request terminate execution interrupt to trigger later.
264  if (is_termination) isolate->stack_guard()->RequestTerminateExecution();
265 
266  return maybe_result;
267 }
268 
269 MaybeHandle<Object> Execution::RunMicrotasks(
270  Isolate* isolate, MessageHandling message_handling,
271  MaybeHandle<Object>* exception_out) {
272  auto undefined = isolate->factory()->undefined_value();
273  return TryCall(isolate, undefined, undefined, 0, {}, message_handling,
274  exception_out, Target::kRunMicrotasks);
275 }
276 
277 void StackGuard::SetStackLimit(uintptr_t limit) {
278  ExecutionAccess access(isolate_);
279  // If the current limits are special (e.g. due to a pending interrupt) then
280  // leave them alone.
281  uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
282  if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
283  thread_local_.set_jslimit(jslimit);
284  }
285  if (thread_local_.climit() == thread_local_.real_climit_) {
286  thread_local_.set_climit(limit);
287  }
288  thread_local_.real_climit_ = limit;
289  thread_local_.real_jslimit_ = jslimit;
290 }
291 
292 
293 void StackGuard::AdjustStackLimitForSimulator() {
294  ExecutionAccess access(isolate_);
295  uintptr_t climit = thread_local_.real_climit_;
296  // If the current limits are special (e.g. due to a pending interrupt) then
297  // leave them alone.
298  uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, climit);
299  if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
300  thread_local_.set_jslimit(jslimit);
301  isolate_->heap()->SetStackLimits();
302  }
303 }
304 
305 
306 void StackGuard::EnableInterrupts() {
307  ExecutionAccess access(isolate_);
308  if (has_pending_interrupts(access)) {
309  set_interrupt_limits(access);
310  }
311 }
312 
313 
314 void StackGuard::DisableInterrupts() {
315  ExecutionAccess access(isolate_);
316  reset_limits(access);
317 }
318 
319 void StackGuard::PushInterruptsScope(InterruptsScope* scope) {
320  ExecutionAccess access(isolate_);
321  DCHECK_NE(scope->mode_, InterruptsScope::kNoop);
322  if (scope->mode_ == InterruptsScope::kPostponeInterrupts) {
323  // Intercept already requested interrupts.
324  int intercepted = thread_local_.interrupt_flags_ & scope->intercept_mask_;
325  scope->intercepted_flags_ = intercepted;
326  thread_local_.interrupt_flags_ &= ~intercepted;
327  } else {
328  DCHECK_EQ(scope->mode_, InterruptsScope::kRunInterrupts);
329  // Restore postponed interrupts.
330  int restored_flags = 0;
331  for (InterruptsScope* current = thread_local_.interrupt_scopes_;
332  current != nullptr; current = current->prev_) {
333  restored_flags |= (current->intercepted_flags_ & scope->intercept_mask_);
334  current->intercepted_flags_ &= ~scope->intercept_mask_;
335  }
336  thread_local_.interrupt_flags_ |= restored_flags;
337  }
338  if (!has_pending_interrupts(access)) reset_limits(access);
339  // Add scope to the chain.
340  scope->prev_ = thread_local_.interrupt_scopes_;
341  thread_local_.interrupt_scopes_ = scope;
342 }
343 
344 void StackGuard::PopInterruptsScope() {
345  ExecutionAccess access(isolate_);
346  InterruptsScope* top = thread_local_.interrupt_scopes_;
347  DCHECK_NE(top->mode_, InterruptsScope::kNoop);
348  if (top->mode_ == InterruptsScope::kPostponeInterrupts) {
349  // Make intercepted interrupts active.
350  DCHECK_EQ(thread_local_.interrupt_flags_ & top->intercept_mask_, 0);
351  thread_local_.interrupt_flags_ |= top->intercepted_flags_;
352  } else {
353  DCHECK_EQ(top->mode_, InterruptsScope::kRunInterrupts);
354  // Postpone existing interupts if needed.
355  if (top->prev_) {
356  for (int interrupt = 1; interrupt < ALL_INTERRUPTS;
357  interrupt = interrupt << 1) {
358  InterruptFlag flag = static_cast<InterruptFlag>(interrupt);
359  if ((thread_local_.interrupt_flags_ & flag) &&
360  top->prev_->Intercept(flag)) {
361  thread_local_.interrupt_flags_ &= ~flag;
362  }
363  }
364  }
365  }
366  if (has_pending_interrupts(access)) set_interrupt_limits(access);
367  // Remove scope from chain.
368  thread_local_.interrupt_scopes_ = top->prev_;
369 }
370 
371 
372 bool StackGuard::CheckInterrupt(InterruptFlag flag) {
373  ExecutionAccess access(isolate_);
374  return thread_local_.interrupt_flags_ & flag;
375 }
376 
377 
378 void StackGuard::RequestInterrupt(InterruptFlag flag) {
379  ExecutionAccess access(isolate_);
380  // Check the chain of InterruptsScope for interception.
381  if (thread_local_.interrupt_scopes_ &&
382  thread_local_.interrupt_scopes_->Intercept(flag)) {
383  return;
384  }
385 
386  // Not intercepted. Set as active interrupt flag.
387  thread_local_.interrupt_flags_ |= flag;
388  set_interrupt_limits(access);
389 
390  // If this isolate is waiting in a futex, notify it to wake up.
391  isolate_->futex_wait_list_node()->NotifyWake();
392 }
393 
394 
395 void StackGuard::ClearInterrupt(InterruptFlag flag) {
396  ExecutionAccess access(isolate_);
397  // Clear the interrupt flag from the chain of InterruptsScope.
398  for (InterruptsScope* current = thread_local_.interrupt_scopes_;
399  current != nullptr; current = current->prev_) {
400  current->intercepted_flags_ &= ~flag;
401  }
402 
403  // Clear the interrupt flag from the active interrupt flags.
404  thread_local_.interrupt_flags_ &= ~flag;
405  if (!has_pending_interrupts(access)) reset_limits(access);
406 }
407 
408 
409 bool StackGuard::CheckAndClearInterrupt(InterruptFlag flag) {
410  ExecutionAccess access(isolate_);
411  bool result = (thread_local_.interrupt_flags_ & flag);
412  thread_local_.interrupt_flags_ &= ~flag;
413  if (!has_pending_interrupts(access)) reset_limits(access);
414  return result;
415 }
416 
417 
418 char* StackGuard::ArchiveStackGuard(char* to) {
419  ExecutionAccess access(isolate_);
420  MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
421  ThreadLocal blank;
422 
423  // Set the stack limits using the old thread_local_.
424  // TODO(isolates): This was the old semantics of constructing a ThreadLocal
425  // (as the ctor called SetStackLimits, which looked at the
426  // current thread_local_ from StackGuard)-- but is this
427  // really what was intended?
428  isolate_->heap()->SetStackLimits();
429  thread_local_ = blank;
430 
431  return to + sizeof(ThreadLocal);
432 }
433 
434 
435 char* StackGuard::RestoreStackGuard(char* from) {
436  ExecutionAccess access(isolate_);
437  MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
438  isolate_->heap()->SetStackLimits();
439  return from + sizeof(ThreadLocal);
440 }
441 
442 
443 void StackGuard::FreeThreadResources() {
444  Isolate::PerIsolateThreadData* per_thread =
445  isolate_->FindOrAllocatePerThreadDataForThisThread();
446  per_thread->set_stack_limit(thread_local_.real_climit_);
447 }
448 
449 
450 void StackGuard::ThreadLocal::Clear() {
451  real_jslimit_ = kIllegalLimit;
452  set_jslimit(kIllegalLimit);
453  real_climit_ = kIllegalLimit;
454  set_climit(kIllegalLimit);
455  interrupt_scopes_ = nullptr;
456  interrupt_flags_ = 0;
457 }
458 
459 
460 bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
461  bool should_set_stack_limits = false;
462  if (real_climit_ == kIllegalLimit) {
463  const uintptr_t kLimitSize = FLAG_stack_size * KB;
464  DCHECK_GT(GetCurrentStackPosition(), kLimitSize);
465  uintptr_t limit = GetCurrentStackPosition() - kLimitSize;
466  real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
467  set_jslimit(SimulatorStack::JsLimitFromCLimit(isolate, limit));
468  real_climit_ = limit;
469  set_climit(limit);
470  should_set_stack_limits = true;
471  }
472  interrupt_scopes_ = nullptr;
473  interrupt_flags_ = 0;
474  return should_set_stack_limits;
475 }
476 
477 
478 void StackGuard::ClearThread(const ExecutionAccess& lock) {
479  thread_local_.Clear();
480  isolate_->heap()->SetStackLimits();
481 }
482 
483 
484 void StackGuard::InitThread(const ExecutionAccess& lock) {
485  if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
486  Isolate::PerIsolateThreadData* per_thread =
487  isolate_->FindOrAllocatePerThreadDataForThisThread();
488  uintptr_t stored_limit = per_thread->stack_limit();
489  // You should hold the ExecutionAccess lock when you call this.
490  if (stored_limit != 0) {
491  SetStackLimit(stored_limit);
492  }
493 }
494 
495 
496 // --- C a l l s t o n a t i v e s ---
497 
498 
499 Object* StackGuard::HandleInterrupts() {
500  if (FLAG_verify_predictable) {
501  // Advance synthetic time by making a time request.
502  isolate_->heap()->MonotonicallyIncreasingTimeInMs();
503  }
504 
505  bool any_interrupt_handled = false;
506  if (FLAG_trace_interrupts) {
507  PrintF("[Handling interrupts: ");
508  }
509 
510  if (CheckAndClearInterrupt(GC_REQUEST)) {
511  if (FLAG_trace_interrupts) {
512  PrintF("GC_REQUEST");
513  any_interrupt_handled = true;
514  }
515  isolate_->heap()->HandleGCRequest();
516  }
517 
518  if (CheckAndClearInterrupt(TERMINATE_EXECUTION)) {
519  if (FLAG_trace_interrupts) {
520  if (any_interrupt_handled) PrintF(", ");
521  PrintF("TERMINATE_EXECUTION");
522  any_interrupt_handled = true;
523  }
524  return isolate_->TerminateExecution();
525  }
526 
527  if (CheckAndClearInterrupt(DEOPT_MARKED_ALLOCATION_SITES)) {
528  if (FLAG_trace_interrupts) {
529  if (any_interrupt_handled) PrintF(", ");
530  PrintF("DEOPT_MARKED_ALLOCATION_SITES");
531  any_interrupt_handled = true;
532  }
533  isolate_->heap()->DeoptMarkedAllocationSites();
534  }
535 
536  if (CheckAndClearInterrupt(INSTALL_CODE)) {
537  if (FLAG_trace_interrupts) {
538  if (any_interrupt_handled) PrintF(", ");
539  PrintF("INSTALL_CODE");
540  any_interrupt_handled = true;
541  }
542  DCHECK(isolate_->concurrent_recompilation_enabled());
543  isolate_->optimizing_compile_dispatcher()->InstallOptimizedFunctions();
544  }
545 
546  if (CheckAndClearInterrupt(API_INTERRUPT)) {
547  if (FLAG_trace_interrupts) {
548  if (any_interrupt_handled) PrintF(", ");
549  PrintF("API_INTERRUPT");
550  any_interrupt_handled = true;
551  }
552  // Callbacks must be invoked outside of ExecusionAccess lock.
553  isolate_->InvokeApiInterruptCallbacks();
554  }
555 
556  if (FLAG_trace_interrupts) {
557  if (!any_interrupt_handled) {
558  PrintF("No interrupt flags set");
559  }
560  PrintF("]\n");
561  }
562 
563  isolate_->counters()->stack_interrupts()->Increment();
564  isolate_->counters()->runtime_profiler_ticks()->Increment();
565  isolate_->runtime_profiler()->MarkCandidatesForOptimization();
566 
567  return ReadOnlyRoots(isolate_).undefined_value();
568 }
569 
570 } // namespace internal
571 } // namespace v8
Definition: libplatform.h:13