execution.cc 15.7 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 8
#include "src/bootstrapper.h"
#include "src/codegen.h"
9
#include "src/isolate-inl.h"
10
#include "src/messages.h"
11
#include "src/vm-state-inl.h"
12

13 14
namespace v8 {
namespace internal {
15

16 17 18 19 20 21
StackGuard::StackGuard()
    : isolate_(NULL) {
}


void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) {
22
  DCHECK(isolate_ != NULL);
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(isolate_ != NULL);
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
MUST_USE_RESULT MaybeHandle<Object> Invoke(Isolate* isolate, bool is_construct,
                                           Handle<Object> target,
57
                                           Handle<Object> receiver, int argc,
58 59
                                           Handle<Object> args[],
                                           Handle<Object> new_target) {
60
  DCHECK(!receiver->IsJSGlobalObject());
61

62
  // Entering JavaScript.
63
  VMState<JS> state(isolate);
64
  CHECK(AllowJavascriptExecution::IsAllowed(isolate));
65 66 67
  if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) {
    isolate->ThrowIllegalOperation();
    isolate->ReportPendingMessages();
68
    return MaybeHandle<Object>();
69
  }
70 71

  // Placeholder for return value.
72
  Object* value = NULL;
73

74
  typedef Object* (*JSEntryFunction)(Object* new_target, Object* target,
75
                                     Object* receiver, int argc,
76
                                     Object*** args);
77

78 79 80
  Handle<Code> code = is_construct
      ? isolate->factory()->js_construct_entry_code()
      : isolate->factory()->js_entry_code();
81

82 83
  {
    // Save and restore context around invocation and block the
84
    // allocation of handles without explicit handle scopes.
85
    SaveContext save(isolate);
86
    SealHandleScope shs(isolate);
87
    JSEntryFunction stub_entry = FUNCTION_CAST<JSEntryFunction>(code->entry());
88 89

    // Call the function through the right JS entry stub.
90
    Object* orig_func = *new_target;
91
    Object* func = *target;
92 93
    Object* recv = *receiver;
    Object*** argv = reinterpret_cast<Object***>(args);
94 95 96
    if (FLAG_profile_deserialization && target->IsJSFunction()) {
      PrintDeserializedCodeInfo(Handle<JSFunction>::cast(target));
    }
97 98
    value = CALL_GENERATED_CODE(isolate, stub_entry, orig_func, func, recv,
                                argc, argv);
99 100
  }

101
#ifdef VERIFY_HEAP
102 103 104
  if (FLAG_verify_heap) {
    value->ObjectVerify();
  }
105 106
#endif

107
  // Update the pending exception flag and return the value.
108
  bool has_exception = value->IsException();
109
  DCHECK(has_exception == isolate->has_pending_exception());
110
  if (has_exception) {
111
    isolate->ReportPendingMessages();
112
    return MaybeHandle<Object>();
113
  } else {
114
    isolate->clear_pending_message();
115 116
  }

117
  return Handle<Object>(value, isolate);
118 119
}

120 121
}  // namespace

122

123
// static
124 125 126
MaybeHandle<Object> Execution::Call(Isolate* isolate, Handle<Object> callable,
                                    Handle<Object> receiver, int argc,
                                    Handle<Object> argv[]) {
127 128 129
  // 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.
130
  if (receiver->IsJSGlobalObject()) {
131
    receiver =
132
        handle(Handle<JSGlobalObject>::cast(receiver)->global_proxy(), isolate);
133 134 135 136 137 138 139 140 141 142 143 144 145 146
  }

  // api callbacks can be called directly.
  if (callable->IsJSFunction() &&
      Handle<JSFunction>::cast(callable)->shared()->IsApiFunction()) {
    Handle<JSFunction> function = Handle<JSFunction>::cast(callable);
    SaveContext save(isolate);
    isolate->set_context(function->context());
    // Do proper receiver conversion for non-strict mode api functions.
    if (!receiver->IsJSReceiver() &&
        is_sloppy(function->shared()->language_mode())) {
      if (receiver->IsUndefined() || receiver->IsNull()) {
        receiver = handle(function->global_proxy(), isolate);
      } else {
147 148
        ASSIGN_RETURN_ON_EXCEPTION(isolate, receiver,
                                   Object::ToObject(isolate, receiver), Object);
149 150
      }
    }
151
    DCHECK(function->context()->global_object()->IsJSGlobalObject());
152 153 154 155 156 157 158 159 160 161
    auto value = Builtins::InvokeApiFunction(function, receiver, argc, argv);
    bool has_exception = value.is_null();
    DCHECK(has_exception == isolate->has_pending_exception());
    if (has_exception) {
      isolate->ReportPendingMessages();
      return MaybeHandle<Object>();
    } else {
      isolate->clear_pending_message();
    }
    return value;
162
  }
163
  return Invoke(isolate, false, callable, receiver, argc, argv,
164 165 166
                isolate->factory()->undefined_value());
}

167

168
// static
169 170
MaybeHandle<Object> Execution::New(Handle<JSFunction> constructor, int argc,
                                   Handle<Object> argv[]) {
171
  return New(constructor->GetIsolate(), constructor, constructor, argc, argv);
172 173 174
}


175 176 177
// static
MaybeHandle<Object> Execution::New(Isolate* isolate, Handle<Object> constructor,
                                   Handle<Object> new_target, int argc,
178
                                   Handle<Object> argv[]) {
179 180
  return Invoke(isolate, true, constructor,
                isolate->factory()->undefined_value(), argc, argv, new_target);
181 182 183
}


184 185
MaybeHandle<Object> Execution::TryCall(Isolate* isolate,
                                       Handle<Object> callable,
186
                                       Handle<Object> receiver, int argc,
187
                                       Handle<Object> args[],
188 189 190 191
                                       MaybeHandle<Object>* exception_out) {
  bool is_termination = false;
  MaybeHandle<Object> maybe_result;
  if (exception_out != NULL) *exception_out = MaybeHandle<Object>();
192
  // Enter a try-block while executing the JavaScript code. To avoid
193 194 195
  // duplicate error printing it must be non-verbose.  Also, to avoid
  // creating message objects during stack overflow we shouldn't
  // capture messages.
196
  {
197
    v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
198 199 200
    catcher.SetVerbose(false);
    catcher.SetCaptureMessage(false);

201
    maybe_result = Call(isolate, callable, receiver, argc, args);
202 203 204 205 206

    if (maybe_result.is_null()) {
      DCHECK(catcher.HasCaught());
      DCHECK(isolate->has_pending_exception());
      DCHECK(isolate->external_caught_exception());
207 208 209 210 211
      if (isolate->pending_exception() ==
          isolate->heap()->termination_exception()) {
        is_termination = true;
      } else {
        if (exception_out != NULL) {
212 213
          *exception_out = v8::Utils::OpenHandle(*catcher.Exception());
        }
214
      }
215
      isolate->OptionalRescheduleException(true);
216
    }
217

218 219
    DCHECK(!isolate->has_pending_exception());
  }
220 221 222 223

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

224
  return maybe_result;
225 226 227 228
}


void StackGuard::SetStackLimit(uintptr_t limit) {
229
  ExecutionAccess access(isolate_);
230
  // If the current limits are special (e.g. due to a pending interrupt) then
231
  // leave them alone.
232
  uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit);
233 234
  if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
    thread_local_.set_jslimit(jslimit);
235
  }
236 237
  if (thread_local_.climit() == thread_local_.real_climit_) {
    thread_local_.set_climit(limit);
238
  }
239 240
  thread_local_.real_climit_ = limit;
  thread_local_.real_jslimit_ = jslimit;
241 242 243
}


244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
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);
  }
}


265
void StackGuard::DisableInterrupts() {
266
  ExecutionAccess access(isolate_);
267 268 269 270
  reset_limits(access);
}


271
void StackGuard::PushPostponeInterruptsScope(PostponeInterruptsScope* scope) {
272
  ExecutionAccess access(isolate_);
273 274 275 276 277 278 279 280
  // 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;
281 282 283
}


284
void StackGuard::PopPostponeInterruptsScope() {
285
  ExecutionAccess access(isolate_);
286 287
  PostponeInterruptsScope* top = thread_local_.postpone_interrupts_;
  // Make intercepted interrupts active.
288
  DCHECK((thread_local_.interrupt_flags_ & top->intercept_mask_) == 0);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  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;
312
  set_interrupt_limits(access);
313 314 315

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


319
void StackGuard::ClearInterrupt(InterruptFlag flag) {
320
  ExecutionAccess access(isolate_);
321 322 323 324 325
  // Clear the interrupt flag from the chain of PostponeInterruptsScopes.
  for (PostponeInterruptsScope* current = thread_local_.postpone_interrupts_;
       current != NULL;
       current = current->prev_) {
    current->intercepted_flags_ &= ~flag;
326
  }
327 328 329 330

  // Clear the interrupt flag from the active interrupt flags.
  thread_local_.interrupt_flags_ &= ~flag;
  if (!has_pending_interrupts(access)) reset_limits(access);
331 332 333
}


334 335
bool StackGuard::CheckAndClearInterrupt(InterruptFlag flag) {
  ExecutionAccess access(isolate_);
336 337 338
  bool result = (thread_local_.interrupt_flags_ & flag);
  thread_local_.interrupt_flags_ &= ~flag;
  if (!has_pending_interrupts(access)) reset_limits(access);
339 340 341 342
  return result;
}


343
char* StackGuard::ArchiveStackGuard(char* to) {
344
  ExecutionAccess access(isolate_);
345
  MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
346
  ThreadLocal blank;
347 348 349 350 351 352 353

  // 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();
354
  thread_local_ = blank;
355

356 357 358 359 360
  return to + sizeof(ThreadLocal);
}


char* StackGuard::RestoreStackGuard(char* from) {
361
  ExecutionAccess access(isolate_);
362
  MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
363
  isolate_->heap()->SetStackLimits();
364 365 366 367
  return from + sizeof(ThreadLocal);
}


368
void StackGuard::FreeThreadResources() {
369 370 371
  Isolate::PerIsolateThreadData* per_thread =
      isolate_->FindOrAllocatePerThreadDataForThisThread();
  per_thread->set_stack_limit(thread_local_.real_climit_);
372 373 374 375
}


void StackGuard::ThreadLocal::Clear() {
376
  real_jslimit_ = kIllegalLimit;
377
  set_jslimit(kIllegalLimit);
378
  real_climit_ = kIllegalLimit;
379
  set_climit(kIllegalLimit);
380
  postpone_interrupts_ = NULL;
381 382 383 384
  interrupt_flags_ = 0;
}


385
bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) {
386
  bool should_set_stack_limits = false;
387
  if (real_climit_ == kIllegalLimit) {
388
    const uintptr_t kLimitSize = FLAG_stack_size * KB;
389
    DCHECK(GetCurrentStackPosition() > kLimitSize);
390
    uintptr_t limit = GetCurrentStackPosition() - kLimitSize;
391
    real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
392
    set_jslimit(SimulatorStack::JsLimitFromCLimit(isolate, limit));
393
    real_climit_ = limit;
394
    set_climit(limit);
395
    should_set_stack_limits = true;
396
  }
397
  postpone_interrupts_ = NULL;
398
  interrupt_flags_ = 0;
399
  return should_set_stack_limits;
400 401 402 403 404
}


void StackGuard::ClearThread(const ExecutionAccess& lock) {
  thread_local_.Clear();
405
  isolate_->heap()->SetStackLimits();
406 407 408 409
}


void StackGuard::InitThread(const ExecutionAccess& lock) {
410 411 412 413
  if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits();
  Isolate::PerIsolateThreadData* per_thread =
      isolate_->FindOrAllocatePerThreadDataForThisThread();
  uintptr_t stored_limit = per_thread->stack_limit();
414
  // You should hold the ExecutionAccess lock when you call this.
415
  if (stored_limit != 0) {
416
    SetStackLimit(stored_limit);
417
  }
418 419 420
}


421 422 423 424 425 426 427
// --- C a l l s   t o   n a t i v e s ---


Handle<String> Execution::GetStackTraceLine(Handle<Object> recv,
                                            Handle<JSFunction> fun,
                                            Handle<Object> pos,
                                            Handle<Object> is_global) {
428
  Isolate* isolate = fun->GetIsolate();
429
  Handle<Object> args[] = { recv, fun, pos, is_global };
430
  MaybeHandle<Object> maybe_result =
431
      TryCall(isolate, isolate->get_stack_trace_line_fun(),
432
              isolate->factory()->undefined_value(), arraysize(args), args);
433 434 435
  Handle<Object> result;
  if (!maybe_result.ToHandle(&result) || !result->IsString()) {
    return isolate->factory()->empty_string();
436 437
  }

438 439 440 441
  return Handle<String>::cast(result);
}


442
void StackGuard::HandleGCInterrupt() {
443 444 445 446 447 448
  if (CheckAndClearInterrupt(GC_REQUEST)) {
    isolate_->heap()->HandleGCRequest();
  }
}


449
Object* StackGuard::HandleInterrupts() {
450 451 452 453 454
  if (FLAG_verify_predictable) {
    // Advance synthetic time by making a time request.
    isolate_->heap()->MonotonicallyIncreasingTimeInMs();
  }

455
  if (CheckAndClearInterrupt(GC_REQUEST)) {
456
    isolate_->heap()->HandleGCRequest();
457
  }
458

459
  if (CheckDebugBreak() || CheckDebugCommand()) {
460
    isolate_->debug()->HandleDebugBreak();
461
  }
462

463 464 465
  if (CheckAndClearInterrupt(TERMINATE_EXECUTION)) {
    return isolate_->TerminateExecution();
  }
466

467 468 469
  if (CheckAndClearInterrupt(DEOPT_MARKED_ALLOCATION_SITES)) {
    isolate_->heap()->DeoptMarkedAllocationSites();
  }
470

471
  if (CheckAndClearInterrupt(INSTALL_CODE)) {
472
    DCHECK(isolate_->concurrent_recompilation_enabled());
473
    isolate_->optimizing_compile_dispatcher()->InstallOptimizedFunctions();
474
  }
475

476
  if (CheckAndClearInterrupt(API_INTERRUPT)) {
477 478
    // Callbacks must be invoked outside of ExecusionAccess lock.
    isolate_->InvokeApiInterruptCallbacks();
479
  }
480

481 482
  isolate_->counters()->stack_interrupts()->Increment();
  isolate_->counters()->runtime_profiler_ticks()->Increment();
483
  isolate_->runtime_profiler()->MarkCandidatesForOptimization();
484

485
  return isolate_->heap()->undefined_value();
486 487
}

488 489
}  // namespace internal
}  // namespace v8