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/allocation.h"
|
|
|
|
#include "src/base/atomicops.h"
|
2017-02-08 09:38:50 +00:00
|
|
|
#include "src/globals.h"
|
2015-08-11 12:00:01 +00:00
|
|
|
#include "src/utils.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
|
|
|
|
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 };
|
|
|
|
|
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
|
|
|
//
|
2016-09-26 07:40:24 +00:00
|
|
|
V8_EXPORT_PRIVATE MUST_USE_RESULT static MaybeHandle<Object> Call(
|
|
|
|
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.
|
2017-05-26 14:48:34 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> New(Isolate* isolate,
|
|
|
|
Handle<Object> constructor,
|
2015-09-22 04:27:14 +00:00
|
|
|
int argc,
|
|
|
|
Handle<Object> argv[]);
|
2015-09-23 05:43:57 +00:00
|
|
|
MUST_USE_RESULT static MaybeHandle<Object> New(Isolate* isolate,
|
|
|
|
Handle<Object> constructor,
|
|
|
|
Handle<Object> new_target,
|
2014-04-11 10:41:09 +00:00
|
|
|
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,
|
|
|
|
MaybeHandle<Object>* exception_out);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ExecutionAccess;
|
2014-07-02 08:05:40 +00:00
|
|
|
class PostponeInterruptsScope;
|
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:
|
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) \
|
|
|
|
V(DEBUGBREAK, DebugBreak, 0) \
|
2017-01-23 10:44:20 +00:00
|
|
|
V(TERMINATE_EXECUTION, TerminateExecution, 1) \
|
|
|
|
V(GC_REQUEST, GC, 2) \
|
|
|
|
V(INSTALL_CODE, InstallCode, 3) \
|
|
|
|
V(API_INTERRUPT, ApiInterrupt, 4) \
|
|
|
|
V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 5)
|
2014-07-02 08:05:40 +00:00
|
|
|
|
2017-06-13 13:24:05 +00:00
|
|
|
#define V(NAME, Name, id) \
|
|
|
|
inline bool Check##Name() { return CheckInterrupt(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.
|
|
|
|
Object* HandleInterrupts();
|
2015-08-24 15:24:41 +00:00
|
|
|
void HandleGCInterrupt();
|
2015-03-20 09:52:17 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2011-03-18 20:35:07 +00:00
|
|
|
StackGuard();
|
|
|
|
|
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
|
2009-08-19 10:18:30 +00:00
|
|
|
static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
|
2009-10-01 10:33:05 +00:00
|
|
|
static const uintptr_t kIllegalLimit = V8_UINT64_C(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
|
|
|
|
2014-07-02 08:05:40 +00:00
|
|
|
void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
|
|
|
|
void PopPostponeInterruptsScope();
|
|
|
|
|
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
|
|
|
|
2014-07-02 08:05:40 +00:00
|
|
|
PostponeInterruptsScope* postpone_interrupts_;
|
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;
|
2008-07-25 07:37:58 +00:00
|
|
|
friend class PostponeInterruptsScope;
|
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_
|