execution.cc 18.2 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/execution.h"
6

7
#include "src/api.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/messages.h"
13
#include "src/runtime-profiler.h"
14
#include "src/vm-state-inl.h"
15

16 17
namespace v8 {
namespace internal {
18

19
StackGuard::StackGuard() : isolate_(nullptr) {}
20 21

void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) {
22
  DCHECK_NOT_NULL(isolate_);
23 24
  thread_local_.set_jslimit(kInterruptLimit);
  thread_local_.set_climit(kInterruptLimit);
25 26 27 28 29
  isolate_->heap()->SetStackLimits();
}


void StackGuard::reset_limits(const ExecutionAccess& lock) {
30
  DCHECK_NOT_NULL(isolate_);
31 32
  thread_local_.set_jslimit(thread_local_.real_jslimit_);
  thread_local_.set_climit(thread_local_.real_climit_);
33 34 35 36
  isolate_->heap()->SetStackLimits();
}


37
static void PrintDeserializedCodeInfo(Handle<JSFunction> function) {
38 39
  if (function->code() == function->shared()->code() &&
      function->shared()->deserialized()) {
40
    PrintF("[Running deserialized script");
41
    Object* script = function->shared()->script();
42 43 44 45 46 47 48
    if (script->IsScript()) {
      Object* name = Script::cast(script)->name();
      if (name->IsString()) {
        PrintF(": %s", String::cast(name)->ToCString().get());
      }
    }
    PrintF("]\n");
49 50 51 52
  }
}


53 54
namespace {

55 56 57
MUST_USE_RESULT MaybeHandle<Object> Invoke(
    Isolate* isolate, bool is_construct, Handle<Object> target,
    Handle<Object> receiver, int argc, Handle<Object> args[],
58 59
    Handle<Object> new_target, Execution::MessageHandling message_handling,
    Execution::Target execution_target) {
60
  DCHECK(!receiver->IsJSGlobalObject());
61

62 63 64 65 66 67 68 69
#ifdef USE_SIMULATOR
  // Simulators use separate stacks for C++ and JS. JS stack overflow checks
  // are performed whenever a JS function is called. However, it can be the case
  // that the C++ stack grows faster than the JS stack, resulting in an overflow
  // there. Add a check here to make that less likely.
  StackLimitCheck check(isolate);
  if (check.HasOverflowed()) {
    isolate->StackOverflow();
70 71 72
    if (message_handling == Execution::MessageHandling::kReport) {
      isolate->ReportPendingMessages();
    }
73 74 75 76
    return MaybeHandle<Object>();
  }
#endif

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  // api callbacks can be called directly.
  if (target->IsJSFunction()) {
    Handle<JSFunction> function = Handle<JSFunction>::cast(target);
    if ((!is_construct || function->IsConstructor()) &&
        function->shared()->IsApiFunction()) {
      SaveContext save(isolate);
      isolate->set_context(function->context());
      DCHECK(function->context()->global_object()->IsJSGlobalObject());
      if (is_construct) receiver = isolate->factory()->the_hole_value();
      auto value = Builtins::InvokeApiFunction(
          isolate, is_construct, function, receiver, argc, args,
          Handle<HeapObject>::cast(new_target));
      bool has_exception = value.is_null();
      DCHECK(has_exception == isolate->has_pending_exception());
      if (has_exception) {
92 93 94
        if (message_handling == Execution::MessageHandling::kReport) {
          isolate->ReportPendingMessages();
        }
95 96 97 98 99 100 101 102
        return MaybeHandle<Object>();
      } else {
        isolate->clear_pending_message();
      }
      return value;
    }
  }

103
  // Entering JavaScript.
104
  VMState<JS> state(isolate);
105
  CHECK(AllowJavascriptExecution::IsAllowed(isolate));
106 107
  if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) {
    isolate->ThrowIllegalOperation();
108 109 110
    if (message_handling == Execution::MessageHandling::kReport) {
      isolate->ReportPendingMessages();
    }
111
    return MaybeHandle<Object>();
112
  }
113 114

  // Placeholder for return value.
115
  Object* value = nullptr;
116

117
  typedef Object* (*JSEntryFunction)(Object* new_target, Object* target,
118
                                     Object* receiver, int argc,
119
                                     Object*** args);
120

121 122 123 124 125 126 127 128 129 130 131 132
  Handle<Code> code;
  switch (execution_target) {
    case Execution::Target::kCallable:
      code = is_construct ? isolate->factory()->js_construct_entry_code()
                          : isolate->factory()->js_entry_code();
      break;
    case Execution::Target::kRunMicrotasks:
      code = isolate->factory()->js_run_microtasks_entry_code();
      break;
    default:
      UNREACHABLE();
  }
133

134 135
  {
    // Save and restore context around invocation and block the
136
    // allocation of handles without explicit handle scopes.
137
    SaveContext save(isolate);
138
    SealHandleScope shs(isolate);
139
    JSEntryFunction stub_entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
140

141 142
    if (FLAG_clear_exceptions_on_js_entry) isolate->clear_pending_exception();

143
    // Call the function through the right JS entry stub.
144
    Object* orig_func = *new_target;
145
    Object* func = *target;
146 147
    Object* recv = *receiver;
    Object*** argv = reinterpret_cast<Object***>(args);
148 149 150
    if (FLAG_profile_deserialization && target->IsJSFunction()) {
      PrintDeserializedCodeInfo(Handle<JSFunction>::cast(target));
    }
151
    RuntimeCallTimerScope timer(isolate, RuntimeCallCounterId::kJS_Execution);
152 153
    value = CALL_GENERATED_CODE(isolate, stub_entry, orig_func, func, recv,
                                argc, argv);
154 155
  }

156
#ifdef VERIFY_HEAP
157 158 159
  if (FLAG_verify_heap) {
    value->ObjectVerify();
  }
160 161
#endif

162
  // Update the pending exception flag and return the value.
163
  bool has_exception = value->IsException(isolate);
164
  DCHECK(has_exception == isolate->has_pending_exception());
165
  if (has_exception) {
166 167 168
    if (message_handling == Execution::MessageHandling::kReport) {
      isolate->ReportPendingMessages();
    }
169
    return MaybeHandle<Object>();
170
  } else {
171
    isolate->clear_pending_message();
172 173
  }

174
  return Handle<Object>(value, isolate);
175 176
}

177 178 179
MaybeHandle<Object> CallInternal(Isolate* isolate, Handle<Object> callable,
                                 Handle<Object> receiver, int argc,
                                 Handle<Object> argv[],
180 181
                                 Execution::MessageHandling message_handling,
                                 Execution::Target target) {
182 183 184
  // Convert calls on global objects to be calls on the global
  // receiver instead to avoid having a 'this' pointer which refers
  // directly to a global object.
185
  if (receiver->IsJSGlobalObject()) {
186
    receiver =
187
        handle(Handle<JSGlobalObject>::cast(receiver)->global_proxy(), isolate);
188 189
  }
  return Invoke(isolate, false, callable, receiver, argc, argv,
190 191
                isolate->factory()->undefined_value(), message_handling,
                target);
192 193 194 195 196 197 198 199 200
}

}  // namespace

// static
MaybeHandle<Object> Execution::Call(Isolate* isolate, Handle<Object> callable,
                                    Handle<Object> receiver, int argc,
                                    Handle<Object> argv[]) {
  return CallInternal(isolate, callable, receiver, argc, argv,
201
                      MessageHandling::kReport, Execution::Target::kCallable);
202 203
}

204

205
// static
206 207 208
MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
                                   int argc, Handle<Object> argv[]) {
  return New(isolate, constructor, constructor, argc, argv);
209 210 211
}


212 213 214
// static
MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
                                   Handle<Object> new_target, int argc,
215
                                   Handle<Object> argv[]) {
216
  return Invoke(isolate, true, constructor,
217
                isolate->factory()->undefined_value(), argc, argv, new_target,
218
                MessageHandling::kReport, Execution::Target::kCallable);
219 220
}

221 222 223 224
MaybeHandle<Object> Execution::TryCall(
    Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
    int argc, Handle<Object> args[], MessageHandling message_handling,
    MaybeHandle<Object>* exception_out, Target target) {
225 226
  bool is_termination = false;
  MaybeHandle<Object> maybe_result;
227
  if (exception_out != nullptr) *exception_out = MaybeHandle<Object>();
228 229
  DCHECK_IMPLIES(message_handling == MessageHandling::kKeepPending,
                 exception_out == nullptr);
230
  // Enter a try-block while executing the JavaScript code. To avoid
231 232 233
  // duplicate error printing it must be non-verbose.  Also, to avoid
  // creating message objects during stack overflow we shouldn't
  // capture messages.
234
  {
235
    v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
236 237 238
    catcher.SetVerbose(false);
    catcher.SetCaptureMessage(false);

239 240
    maybe_result = CallInternal(isolate, callable, receiver, argc, args,
                                message_handling, target);
241 242 243

    if (maybe_result.is_null()) {
      DCHECK(isolate->has_pending_exception());
244 245 246 247
      if (isolate->pending_exception() ==
          isolate->heap()->termination_exception()) {
        is_termination = true;
      } else {
248 249 250
        if (exception_out != nullptr) {
          DCHECK(catcher.HasCaught());
          DCHECK(isolate->external_caught_exception());
251 252
          *exception_out = v8::Utils::OpenHandle(*catcher.Exception());
        }
253
      }
254 255 256
      if (message_handling == MessageHandling::kReport) {
        isolate->OptionalRescheduleException(true);
      }
257
    }
258
  }
259 260 261 262

  // Re-request terminate execution interrupt to trigger later.
  if (is_termination) isolate->stack_guard()->RequestTerminateExecution();

263
  return maybe_result;
264 265
}

266 267 268 269 270 271 272
MaybeHandle<Object> Execution::RunMicrotasks(
    Isolate* isolate, MessageHandling message_handling,
    MaybeHandle<Object>* exception_out) {
  auto undefined = isolate->factory()->undefined_value();
  return TryCall(isolate, undefined, undefined, 0, {}, message_handling,
                 exception_out, Target::kRunMicrotasks);
}
273 274

void StackGuard::SetStackLimit(uintptr_t limit) {
275
  ExecutionAccess access(isolate_);
276
  // If the current limits are special (e.g. due to a pending interrupt) then
277
  // leave them alone.
278
  uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
279 280
  if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
    thread_local_.set_jslimit(jslimit);
281
  }
282 283
  if (thread_local_.climit() == thread_local_.real_climit_) {
    thread_local_.set_climit(limit);
284
  }
285 286
  thread_local_.real_climit_ = limit;
  thread_local_.real_jslimit_ = jslimit;
287 288 289
}


290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
void StackGuard::AdjustStackLimitForSimulator() {
  ExecutionAccess access(isolate_);
  uintptr_t climit = thread_local_.real_climit_;
  // If the current limits are special (e.g. due to a pending interrupt) then
  // leave them alone.
  uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, climit);
  if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
    thread_local_.set_jslimit(jslimit);
    isolate_->heap()->SetStackLimits();
  }
}


void StackGuard::EnableInterrupts() {
  ExecutionAccess access(isolate_);
  if (has_pending_interrupts(access)) {
    set_interrupt_limits(access);
  }
}


311
void StackGuard::DisableInterrupts() {
312
  ExecutionAccess access(isolate_);
313 314 315 316
  reset_limits(access);
}


317
void StackGuard::PushPostponeInterruptsScope(PostponeInterruptsScope* scope) {
318
  ExecutionAccess access(isolate_);
319 320 321 322 323 324 325 326
  // Intercept already requested interrupts.
  int intercepted = thread_local_.interrupt_flags_ & scope->intercept_mask_;
  scope->intercepted_flags_ = intercepted;
  thread_local_.interrupt_flags_ &= ~intercepted;
  if (!has_pending_interrupts(access)) reset_limits(access);
  // Add scope to the chain.
  scope->prev_ = thread_local_.postpone_interrupts_;
  thread_local_.postpone_interrupts_ = scope;
327 328 329
}


330
void StackGuard::PopPostponeInterruptsScope() {
331
  ExecutionAccess access(isolate_);
332 333
  PostponeInterruptsScope* top = thread_local_.postpone_interrupts_;
  // Make intercepted interrupts active.
334
  DCHECK_EQ(thread_local_.interrupt_flags_ & top->intercept_mask_, 0);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
  thread_local_.interrupt_flags_ |= top->intercepted_flags_;
  if (has_pending_interrupts(access)) set_interrupt_limits(access);
  // Remove scope from chain.
  thread_local_.postpone_interrupts_ = top->prev_;
}


bool StackGuard::CheckInterrupt(InterruptFlag flag) {
  ExecutionAccess access(isolate_);
  return thread_local_.interrupt_flags_ & flag;
}


void StackGuard::RequestInterrupt(InterruptFlag flag) {
  ExecutionAccess access(isolate_);
  // Check the chain of PostponeInterruptsScopes for interception.
  if (thread_local_.postpone_interrupts_ &&
      thread_local_.postpone_interrupts_->Intercept(flag)) {
    return;
  }

  // Not intercepted.  Set as active interrupt flag.
  thread_local_.interrupt_flags_ |= flag;
358
  set_interrupt_limits(access);
359 360 361

  // If this isolate is waiting in a futex, notify it to wake up.
  isolate_->futex_wait_list_node()->NotifyWake();
362 363 364
}


365
void StackGuard::ClearInterrupt(InterruptFlag flag) {
366
  ExecutionAccess access(isolate_);
367 368
  // Clear the interrupt flag from the chain of PostponeInterruptsScopes.
  for (PostponeInterruptsScope* current = thread_local_.postpone_interrupts_;
369
       current != nullptr; current = current->prev_) {
370
    current->intercepted_flags_ &= ~flag;
371
  }
372 373 374 375

  // Clear the interrupt flag from the active interrupt flags.
  thread_local_.interrupt_flags_ &= ~flag;
  if (!has_pending_interrupts(access)) reset_limits(access);
376 377 378
}


379 380
bool StackGuard::CheckAndClearInterrupt(InterruptFlag flag) {
  ExecutionAccess access(isolate_);
381 382 383
  bool result = (thread_local_.interrupt_flags_ & flag);
  thread_local_.interrupt_flags_ &= ~flag;
  if (!has_pending_interrupts(access)) reset_limits(access);
384 385 386 387
  return result;
}


388
char* StackGuard::ArchiveStackGuard(char* to) {
389
  ExecutionAccess access(isolate_);
390
  MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
391
  ThreadLocal blank;
392 393 394 395 396 397 398

  // Set the stack limits using the old thread_local_.
  // TODO(isolates): This was the old semantics of constructing a ThreadLocal
  //                 (as the ctor called SetStackLimits, which looked at the
  //                 current thread_local_ from StackGuard)-- but is this
  //                 really what was intended?
  isolate_->heap()->SetStackLimits();
399
  thread_local_ = blank;
400

401 402 403 404 405
  return to + sizeof(ThreadLocal);
}


char* StackGuard::RestoreStackGuard(char* from) {
406
  ExecutionAccess access(isolate_);
407
  MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
408
  isolate_->heap()->SetStackLimits();
409 410 411 412
  return from + sizeof(ThreadLocal);
}


413
void StackGuard::FreeThreadResources() {
414 415 416
  Isolate::PerIsolateThreadData* per_thread =
      isolate_->FindOrAllocatePerThreadDataForThisThread();
  per_thread->set_stack_limit(thread_local_.real_climit_);
417 418 419 420
}


void StackGuard::ThreadLocal::Clear() {
421
  real_jslimit_ = kIllegalLimit;
422
  set_jslimit(kIllegalLimit);
423
  real_climit_ = kIllegalLimit;
424
  set_climit(kIllegalLimit);
425
  postpone_interrupts_ = nullptr;
426 427 428 429
  interrupt_flags_ = 0;
}


430
bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
431
  bool should_set_stack_limits = false;
432
  if (real_climit_ == kIllegalLimit) {
433
    const uintptr_t kLimitSize = FLAG_stack_size * KB;
434
    DCHECK_GT(GetCurrentStackPosition(), kLimitSize);
435
    uintptr_t limit = GetCurrentStackPosition() - kLimitSize;
436
    real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
437
    set_jslimit(SimulatorStack::JsLimitFromCLimit(isolate, limit));
438
    real_climit_ = limit;
439
    set_climit(limit);
440
    should_set_stack_limits = true;
441
  }
442
  postpone_interrupts_ = nullptr;
443
  interrupt_flags_ = 0;
444
  return should_set_stack_limits;
445 446 447 448 449
}


void StackGuard::ClearThread(const ExecutionAccess& lock) {
  thread_local_.Clear();
450
  isolate_->heap()->SetStackLimits();
451 452 453 454
}


void StackGuard::InitThread(const ExecutionAccess& lock) {
455 456 457 458
  if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
  Isolate::PerIsolateThreadData* per_thread =
      isolate_->FindOrAllocatePerThreadDataForThisThread();
  uintptr_t stored_limit = per_thread->stack_limit();
459
  // You should hold the ExecutionAccess lock when you call this.
460
  if (stored_limit != 0) {
461
    SetStackLimit(stored_limit);
462
  }
463 464 465
}


466 467 468
// --- C a l l s   t o   n a t i v e s ---


469
void StackGuard::HandleGCInterrupt() {
470 471 472 473 474 475
  if (CheckAndClearInterrupt(GC_REQUEST)) {
    isolate_->heap()->HandleGCRequest();
  }
}


476
Object* StackGuard::HandleInterrupts() {
477 478 479 480 481
  if (FLAG_verify_predictable) {
    // Advance synthetic time by making a time request.
    isolate_->heap()->MonotonicallyIncreasingTimeInMs();
  }

482 483 484 485 486
  bool any_interrupt_handled = false;
  if (FLAG_trace_interrupts) {
    PrintF("[Handling interrupts: ");
  }

487
  if (CheckAndClearInterrupt(GC_REQUEST)) {
488 489 490 491
    if (FLAG_trace_interrupts) {
      PrintF("GC_REQUEST");
      any_interrupt_handled = true;
    }
492
    isolate_->heap()->HandleGCRequest();
493
  }
494

495
  if (CheckDebugBreak()) {
496 497 498 499 500
    if (FLAG_trace_interrupts) {
      if (any_interrupt_handled) PrintF(", ");
      PrintF("DEBUG_BREAK");
      any_interrupt_handled = true;
    }
501
    isolate_->debug()->HandleDebugBreak(kIgnoreIfTopFrameBlackboxed);
502
  }
503

504
  if (CheckAndClearInterrupt(TERMINATE_EXECUTION)) {
505 506 507 508 509
    if (FLAG_trace_interrupts) {
      if (any_interrupt_handled) PrintF(", ");
      PrintF("TERMINATE_EXECUTION");
      any_interrupt_handled = true;
    }
510 511
    return isolate_->TerminateExecution();
  }
512

513
  if (CheckAndClearInterrupt(DEOPT_MARKED_ALLOCATION_SITES)) {
514 515 516 517 518
    if (FLAG_trace_interrupts) {
      if (any_interrupt_handled) PrintF(", ");
      PrintF("DEOPT_MARKED_ALLOCATION_SITES");
      any_interrupt_handled = true;
    }
519 520
    isolate_->heap()->DeoptMarkedAllocationSites();
  }
521

522
  if (CheckAndClearInterrupt(INSTALL_CODE)) {
523 524 525 526 527
    if (FLAG_trace_interrupts) {
      if (any_interrupt_handled) PrintF(", ");
      PrintF("INSTALL_CODE");
      any_interrupt_handled = true;
    }
528
    DCHECK(isolate_->concurrent_recompilation_enabled());
529
    isolate_->optimizing_compile_dispatcher()->InstallOptimizedFunctions();
530
  }
531

532
  if (CheckAndClearInterrupt(API_INTERRUPT)) {
533 534 535 536 537
    if (FLAG_trace_interrupts) {
      if (any_interrupt_handled) PrintF(", ");
      PrintF("API_INTERRUPT");
      any_interrupt_handled = true;
    }
538 539
    // Callbacks must be invoked outside of ExecusionAccess lock.
    isolate_->InvokeApiInterruptCallbacks();
540
  }
541

542 543 544 545 546 547 548
  if (FLAG_trace_interrupts) {
    if (!any_interrupt_handled) {
      PrintF("No interrupt flags set");
    }
    PrintF("]\n");
  }

549 550
  isolate_->counters()->stack_interrupts()->Increment();
  isolate_->counters()->runtime_profiler_ticks()->Increment();
551
  isolate_->runtime_profiler()->MarkCandidatesForOptimization();
552

553
  return isolate_->heap()->undefined_value();
554 555
}

556 557
}  // namespace internal
}  // namespace v8