2014-03-31 12:01:02 +00:00
|
|
|
// 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.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_EXECUTION_H_
|
|
|
|
#define V8_EXECUTION_H_
|
|
|
|
|
2015-08-11 12:00:01 +00:00
|
|
|
#include "src/base/atomicops.h"
|
2017-02-08 09:38:50 +00:00
|
|
|
#include "src/globals.h"
|
2011-05-06 06:50:20 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2018-12-18 03:34:15 +00:00
|
|
|
class MicrotaskQueue;
|
|
|
|
|
2017-01-27 13:53:13 +00:00
|
|
|
template <typename T>
|
|
|
|
class Handle;
|
|
|
|
|
2015-04-20 13:08:11 +00:00
|
|
|
class Execution final : public AllStatic {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2017-01-17 13:01:03 +00:00
|
|
|
// Whether to report pending messages, or keep them pending on the isolate.
|
|
|
|
enum class MessageHandling { kReport, kKeepPending };
|
2017-11-30 15:27:59 +00:00
|
|
|
enum class Target { kCallable, kRunMicrotasks };
|
2017-01-17 13:01:03 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Call a function, the caller supplies a receiver and an array
|
2015-09-17 17:11:38 +00:00
|
|
|
// of arguments.
|
2008-07-03 15:10:15 +00:00
|
|
|
//
|
2015-09-17 17:11:38 +00:00
|
|
|
// When the function called is not in strict mode, receiver is
|
|
|
|
// converted to an object.
|
2011-09-13 11:42:57 +00:00
|
|
|
//
|
2018-04-09 15:12:07 +00:00
|
|
|
V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Call(
|
2016-09-26 07:40:24 +00:00
|
|
|
Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
|
|
|
|
int argc, Handle<Object> argv[]);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Construct object from function, the caller supplies an array of
|
2015-09-22 04:27:14 +00:00
|
|
|
// arguments.
|
2018-04-09 15:12:07 +00:00
|
|
|
V8_WARN_UNUSED_RESULT static MaybeHandle<Object> New(
|
|
|
|
Isolate* isolate, Handle<Object> constructor, int argc,
|
|
|
|
Handle<Object> argv[]);
|
|
|
|
V8_WARN_UNUSED_RESULT static MaybeHandle<Object> New(
|
|
|
|
Isolate* isolate, Handle<Object> constructor, Handle<Object> new_target,
|
|
|
|
int argc, Handle<Object> argv[]);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2017-01-17 13:01:03 +00:00
|
|
|
// Call a function, just like Call(), but handle don't report exceptions
|
|
|
|
// externally.
|
|
|
|
// The return value is either the result of calling the function (if no
|
|
|
|
// exception occurred), or an empty handle.
|
|
|
|
// If message_handling is MessageHandling::kReport, exceptions (except for
|
|
|
|
// termination exceptions) will be stored in exception_out (if not a
|
|
|
|
// nullptr).
|
2015-10-23 12:26:49 +00:00
|
|
|
static MaybeHandle<Object> TryCall(Isolate* isolate, Handle<Object> callable,
|
2014-09-01 09:11:44 +00:00
|
|
|
Handle<Object> receiver, int argc,
|
|
|
|
Handle<Object> argv[],
|
2017-01-17 13:01:03 +00:00
|
|
|
MessageHandling message_handling,
|
2018-12-18 03:34:15 +00:00
|
|
|
MaybeHandle<Object>* exception_out);
|
2017-11-30 15:27:59 +00:00
|
|
|
// Convenience method for performing RunMicrotasks
|
2018-12-18 03:34:15 +00:00
|
|
|
static MaybeHandle<Object> TryRunMicrotasks(
|
|
|
|
Isolate* isolate, MicrotaskQueue* microtask_queue,
|
|
|
|
MaybeHandle<Object>* exception_out);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExecutionAccess;
|
2018-04-23 14:19:36 +00:00
|
|
|
class InterruptsScope;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-10-01 10:33:05 +00:00
|
|
|
// StackGuard contains the handling of the limits that are used to limit the
|
|
|
|
// number of nested invocations of JavaScript and the stack size used in each
|
|
|
|
// invocation.
|
2017-02-08 09:38:50 +00:00
|
|
|
class V8_EXPORT_PRIVATE StackGuard final {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2018-12-10 13:00:02 +00:00
|
|
|
explicit StackGuard(Isolate* isolate) : isolate_(isolate) {}
|
2018-10-26 16:42:54 +00:00
|
|
|
|
2009-10-01 10:33:05 +00:00
|
|
|
// Pass the address beyond which the stack should not grow. The stack
|
|
|
|
// is assumed to grow downwards.
|
2011-03-18 20:35:07 +00:00
|
|
|
void SetStackLimit(uintptr_t limit);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2015-08-24 15:55:30 +00:00
|
|
|
// The simulator uses a separate JS stack. Limits on the JS stack might have
|
|
|
|
// to be adjusted in order to reflect overflows of the C stack, because we
|
|
|
|
// cannot rely on the interleaving of frames on the simulator.
|
|
|
|
void AdjustStackLimitForSimulator();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Threading support.
|
2011-03-18 20:35:07 +00:00
|
|
|
char* ArchiveStackGuard(char* to);
|
|
|
|
char* RestoreStackGuard(char* from);
|
|
|
|
static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
|
|
|
|
void FreeThreadResources();
|
2009-10-01 10:33:05 +00:00
|
|
|
// Sets up the default stack guard for this thread if it has not
|
|
|
|
// already been set up.
|
2011-03-18 20:35:07 +00:00
|
|
|
void InitThread(const ExecutionAccess& lock);
|
2009-10-01 10:33:05 +00:00
|
|
|
// Clears the stack guard for this thread so it does not look as if
|
|
|
|
// it has been set up.
|
2011-03-18 20:35:07 +00:00
|
|
|
void ClearThread(const ExecutionAccess& lock);
|
|
|
|
|
2016-11-24 12:55:52 +00:00
|
|
|
#define INTERRUPT_LIST(V) \
|
[inspector] use interrupt for pause only as last resort
With this CL we use interrupt for pause in two cases:
- when we process Debugger.pause on interruption,
- when we would like to break as soon as possible after OOM.
In all other cases, e.g. for async step into we use break
on function call by calling StepIn debugger action.
In mentioned cases we should not actually use interrupt as well:
- Debugger.pause in this case scheduled using interrupt and we
may just break right now without requesting another interrupt,
unfortunately blink side is not ready,
- we should use more reliable way to break right after near OOM
callback, otherwise we can get this callback, increase limit,
request break on next interrupt, before interrupt get another
huge memory allocation and crash.
There are couple advantages:
- we get much better break locations for async stepping
(see inspector tests expectations),
- we can remove DEBUG_BREAK interruption
(it should speedup blackboxing with async tasks, see
removed todo in debug.cc for details)
- it is required preparation step for async step out,
(see https://chromium-review.googlesource.com/c/v8/v8/+/1054618)
Bug: v8:7753
Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: Iabd7627dbffa9a0eab1736064caf589d02591926
Reviewed-on: https://chromium-review.googlesource.com/1054155
Commit-Queue: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Dmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53439}
2018-05-29 14:54:08 +00:00
|
|
|
V(TERMINATE_EXECUTION, TerminateExecution, 0) \
|
|
|
|
V(GC_REQUEST, GC, 1) \
|
|
|
|
V(INSTALL_CODE, InstallCode, 2) \
|
|
|
|
V(API_INTERRUPT, ApiInterrupt, 3) \
|
|
|
|
V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 4)
|
2014-07-02 08:05:40 +00:00
|
|
|
|
2017-06-15 14:53:38 +00:00
|
|
|
#define V(NAME, Name, id) \
|
|
|
|
inline bool Check##Name() { return CheckInterrupt(NAME); } \
|
|
|
|
inline bool CheckAndClear##Name() { return CheckAndClearInterrupt(NAME); } \
|
|
|
|
inline void Request##Name() { RequestInterrupt(NAME); } \
|
2014-07-02 08:05:40 +00:00
|
|
|
inline void Clear##Name() { ClearInterrupt(NAME); }
|
2014-05-09 09:13:12 +00:00
|
|
|
INTERRUPT_LIST(V)
|
|
|
|
#undef V
|
2013-12-19 16:45:58 +00:00
|
|
|
|
2014-07-02 08:05:40 +00:00
|
|
|
// Flag used to set the interrupt causes.
|
|
|
|
enum InterruptFlag {
|
|
|
|
#define V(NAME, Name, id) NAME = (1 << id),
|
|
|
|
INTERRUPT_LIST(V)
|
|
|
|
#undef V
|
|
|
|
#define V(NAME, Name, id) NAME |
|
|
|
|
ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
|
|
|
|
#undef V
|
|
|
|
};
|
|
|
|
|
2015-04-15 11:37:07 +00:00
|
|
|
uintptr_t climit() { return thread_local_.climit(); }
|
|
|
|
uintptr_t jslimit() { return thread_local_.jslimit(); }
|
2009-11-05 13:59:40 +00:00
|
|
|
// This provides an asynchronous read of the stack limits for the current
|
2009-10-01 10:33:05 +00:00
|
|
|
// thread. There are no locks protecting this, but it is assumed that you
|
|
|
|
// have the global V8 lock if you are using multiple V8 threads.
|
2011-03-18 20:35:07 +00:00
|
|
|
uintptr_t real_climit() {
|
2010-11-12 08:40:21 +00:00
|
|
|
return thread_local_.real_climit_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
uintptr_t real_jslimit() {
|
2009-11-05 13:59:40 +00:00
|
|
|
return thread_local_.real_jslimit_;
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Address address_of_jslimit() {
|
2009-11-05 13:59:40 +00:00
|
|
|
return reinterpret_cast<Address>(&thread_local_.jslimit_);
|
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
Address address_of_real_jslimit() {
|
2009-11-05 13:59:40 +00:00
|
|
|
return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
|
|
|
|
}
|
2014-05-09 09:13:12 +00:00
|
|
|
|
|
|
|
// If the stack guard is triggered, but it is not an actual
|
|
|
|
// stack overflow, then handle the interruption accordingly.
|
2018-12-25 00:19:47 +00:00
|
|
|
Object HandleInterrupts();
|
2015-03-20 09:52:17 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2014-07-02 08:05:40 +00:00
|
|
|
bool CheckInterrupt(InterruptFlag flag);
|
|
|
|
void RequestInterrupt(InterruptFlag flag);
|
|
|
|
void ClearInterrupt(InterruptFlag flag);
|
2014-06-02 12:07:37 +00:00
|
|
|
bool CheckAndClearInterrupt(InterruptFlag flag);
|
2014-05-09 09:13:12 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// You should hold the ExecutionAccess lock when calling this method.
|
2011-03-18 20:35:07 +00:00
|
|
|
bool has_pending_interrupts(const ExecutionAccess& lock) {
|
2010-04-14 07:36:49 +00:00
|
|
|
return thread_local_.interrupt_flags_ != 0;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// You should hold the ExecutionAccess lock when calling this method.
|
2011-03-18 20:35:07 +00:00
|
|
|
inline void set_interrupt_limits(const ExecutionAccess& lock);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-11-05 13:59:40 +00:00
|
|
|
// Reset limits to actual values. For example after handling interrupt.
|
2008-07-03 15:10:15 +00:00
|
|
|
// You should hold the ExecutionAccess lock when calling this method.
|
2011-03-18 20:35:07 +00:00
|
|
|
inline void reset_limits(const ExecutionAccess& lock);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
// Enable or disable interrupts.
|
2011-03-18 20:35:07 +00:00
|
|
|
void EnableInterrupts();
|
|
|
|
void DisableInterrupts();
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2014-06-24 05:27:44 +00:00
|
|
|
#if V8_TARGET_ARCH_64_BIT
|
2017-12-01 08:58:16 +00:00
|
|
|
static const uintptr_t kInterruptLimit = uintptr_t{0xfffffffffffffffe};
|
|
|
|
static const uintptr_t kIllegalLimit = uintptr_t{0xfffffffffffffff8};
|
2009-08-19 10:18:30 +00:00
|
|
|
#else
|
2008-07-03 15:10:15 +00:00
|
|
|
static const uintptr_t kInterruptLimit = 0xfffffffe;
|
2009-10-01 10:33:05 +00:00
|
|
|
static const uintptr_t kIllegalLimit = 0xfffffff8;
|
2009-08-19 10:18:30 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2018-04-23 14:19:36 +00:00
|
|
|
void PushInterruptsScope(InterruptsScope* scope);
|
|
|
|
void PopInterruptsScope();
|
2014-07-02 08:05:40 +00:00
|
|
|
|
2015-04-20 13:08:11 +00:00
|
|
|
class ThreadLocal final {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2009-10-01 10:33:05 +00:00
|
|
|
ThreadLocal() { Clear(); }
|
|
|
|
// You should hold the ExecutionAccess lock when you call Initialize or
|
|
|
|
// Clear.
|
|
|
|
void Clear();
|
2009-11-05 13:59:40 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Returns true if the heap's stack limits should be set, false if not.
|
2011-05-05 18:55:31 +00:00
|
|
|
bool Initialize(Isolate* isolate);
|
2011-03-18 20:35:07 +00:00
|
|
|
|
2009-11-05 13:59:40 +00:00
|
|
|
// The stack limit is split into a JavaScript and a C++ stack limit. These
|
|
|
|
// two are the same except when running on a simulator where the C++ and
|
|
|
|
// JavaScript stacks are separate. Each of the two stack limits have two
|
|
|
|
// values. The one eith the real_ prefix is the actual stack limit
|
|
|
|
// set for the VM. The one without the real_ prefix has the same value as
|
|
|
|
// the actual stack limit except when there is an interruption (e.g. debug
|
|
|
|
// break or preemption) in which case it is lowered to make stack checks
|
|
|
|
// fail. Both the generated code and the runtime system check against the
|
|
|
|
// one without the real_ prefix.
|
|
|
|
uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
|
|
|
|
uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
|
2015-04-15 11:37:07 +00:00
|
|
|
|
|
|
|
// jslimit_ and climit_ can be read without any lock.
|
|
|
|
// Writing requires the ExecutionAccess lock.
|
|
|
|
base::AtomicWord jslimit_;
|
|
|
|
base::AtomicWord climit_;
|
|
|
|
|
|
|
|
uintptr_t jslimit() {
|
2017-05-30 07:44:37 +00:00
|
|
|
return bit_cast<uintptr_t>(base::Relaxed_Load(&jslimit_));
|
2015-04-15 11:37:07 +00:00
|
|
|
}
|
|
|
|
void set_jslimit(uintptr_t limit) {
|
2017-05-30 07:44:37 +00:00
|
|
|
return base::Relaxed_Store(&jslimit_,
|
|
|
|
static_cast<base::AtomicWord>(limit));
|
2015-04-15 11:37:07 +00:00
|
|
|
}
|
|
|
|
uintptr_t climit() {
|
2017-05-30 07:44:37 +00:00
|
|
|
return bit_cast<uintptr_t>(base::Relaxed_Load(&climit_));
|
2015-04-15 11:37:07 +00:00
|
|
|
}
|
|
|
|
void set_climit(uintptr_t limit) {
|
2017-05-30 07:44:37 +00:00
|
|
|
return base::Relaxed_Store(&climit_,
|
|
|
|
static_cast<base::AtomicWord>(limit));
|
2015-04-15 11:37:07 +00:00
|
|
|
}
|
2009-11-05 13:59:40 +00:00
|
|
|
|
2018-04-23 14:19:36 +00:00
|
|
|
InterruptsScope* interrupt_scopes_;
|
2008-07-03 15:10:15 +00:00
|
|
|
int interrupt_flags_;
|
|
|
|
};
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// TODO(isolates): Technically this could be calculated directly from a
|
|
|
|
// pointer to StackGuard.
|
|
|
|
Isolate* isolate_;
|
|
|
|
ThreadLocal thread_local_;
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
friend class Isolate;
|
2008-07-03 15:10:15 +00:00
|
|
|
friend class StackLimitCheck;
|
2018-04-23 14:19:36 +00:00
|
|
|
friend class InterruptsScope;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(StackGuard);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
2015-09-30 13:46:56 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#endif // V8_EXECUTION_H_
|