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.
5 #ifndef V8_EXECUTION_H_
6 #define V8_EXECUTION_H_
8 #include "src/allocation.h"
9 #include "src/base/atomicops.h"
10 #include "src/handles.h"
11 #include "src/utils.h"
16 // Forward declarations.
19 class Execution final : public AllStatic {
21 // Call a function, the caller supplies a receiver and an array
24 // When the function called is not in strict mode, receiver is
25 // converted to an object.
27 MUST_USE_RESULT static MaybeHandle<Object> Call(Isolate* isolate,
28 Handle<Object> callable,
29 Handle<Object> receiver,
31 Handle<Object> argv[]);
33 // Construct object from function, the caller supplies an array of
34 // arguments. Arguments are Object* type. After function returns,
35 // pointers in 'args' might be invalid.
37 // *pending_exception tells whether the invoke resulted in
38 // a pending exception.
40 MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func,
42 Handle<Object> argv[]);
44 // Call a function, just like Call(), but make sure to silently catch
45 // any thrown exceptions. The return value is either the result of
46 // calling the function (if caught exception is false) or the exception
47 // that occurred (if caught exception is true).
48 // In the exception case, exception_out holds the caught exceptions, unless
49 // it is a termination exception.
50 static MaybeHandle<Object> TryCall(Handle<JSFunction> func,
51 Handle<Object> receiver, int argc,
52 Handle<Object> argv[],
53 MaybeHandle<Object>* exception_out = NULL);
56 MUST_USE_RESULT static MaybeHandle<Object> ToInteger(
57 Isolate* isolate, Handle<Object> obj);
60 MUST_USE_RESULT static MaybeHandle<Object> ToInt32(
61 Isolate* isolate, Handle<Object> obj);
64 MUST_USE_RESULT static MaybeHandle<Object> ToUint32(
65 Isolate* isolate, Handle<Object> obj);
68 // ES6, draft 10-14-14, section 7.1.15
69 MUST_USE_RESULT static MaybeHandle<Object> ToLength(
70 Isolate* isolate, Handle<Object> obj);
73 MUST_USE_RESULT static MaybeHandle<Object> ToDetailString(
74 Isolate* isolate, Handle<Object> obj);
77 MUST_USE_RESULT static MaybeHandle<Object> ToObject(
78 Isolate* isolate, Handle<Object> obj);
80 // Create a new date object from 'time'.
81 MUST_USE_RESULT static MaybeHandle<Object> NewDate(
82 Isolate* isolate, double time);
84 // Create a new regular expression object from 'pattern' and 'flags'.
85 MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp(
86 Handle<String> pattern, Handle<String> flags);
88 static Handle<String> GetStackTraceLine(Handle<Object> recv,
89 Handle<JSFunction> fun,
91 Handle<Object> is_global);
93 // Get a function delegate for the given non-function object.
94 // Used for support calling objects as functions.
95 MUST_USE_RESULT static MaybeHandle<JSFunction> GetFunctionDelegate(
96 Isolate* isolate, Handle<Object> object);
98 // Get a function delegate (or undefined) for the given non-function
99 // object. Used for support calling objects as constructors.
100 MUST_USE_RESULT static MaybeHandle<JSFunction> GetConstructorDelegate(
101 Isolate* isolate, Handle<Object> object);
104 MUST_USE_RESULT static Handle<String> RenderCallSite(Isolate* isolate,
105 Handle<Object> object);
109 class ExecutionAccess;
110 class PostponeInterruptsScope;
113 // StackGuard contains the handling of the limits that are used to limit the
114 // number of nested invocations of JavaScript and the stack size used in each
116 class StackGuard final {
118 // Pass the address beyond which the stack should not grow. The stack
119 // is assumed to grow downwards.
120 void SetStackLimit(uintptr_t limit);
122 // The simulator uses a separate JS stack. Limits on the JS stack might have
123 // to be adjusted in order to reflect overflows of the C stack, because we
124 // cannot rely on the interleaving of frames on the simulator.
125 void AdjustStackLimitForSimulator();
127 // Threading support.
128 char* ArchiveStackGuard(char* to);
129 char* RestoreStackGuard(char* from);
130 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
131 void FreeThreadResources();
132 // Sets up the default stack guard for this thread if it has not
133 // already been set up.
134 void InitThread(const ExecutionAccess& lock);
135 // Clears the stack guard for this thread so it does not look as if
136 // it has been set up.
137 void ClearThread(const ExecutionAccess& lock);
139 #define INTERRUPT_LIST(V) \
140 V(DEBUGBREAK, DebugBreak, 0) \
141 V(DEBUGCOMMAND, DebugCommand, 1) \
142 V(TERMINATE_EXECUTION, TerminateExecution, 2) \
143 V(GC_REQUEST, GC, 3) \
144 V(INSTALL_CODE, InstallCode, 4) \
145 V(API_INTERRUPT, ApiInterrupt, 5) \
146 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)
148 #define V(NAME, Name, id) \
149 inline bool Check##Name() { return CheckInterrupt(NAME); } \
150 inline void Request##Name() { RequestInterrupt(NAME); } \
151 inline void Clear##Name() { ClearInterrupt(NAME); }
155 // Flag used to set the interrupt causes.
157 #define V(NAME, Name, id) NAME = (1 << id),
160 #define V(NAME, Name, id) NAME |
161 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
165 uintptr_t climit() { return thread_local_.climit(); }
166 uintptr_t jslimit() { return thread_local_.jslimit(); }
167 // This provides an asynchronous read of the stack limits for the current
168 // thread. There are no locks protecting this, but it is assumed that you
169 // have the global V8 lock if you are using multiple V8 threads.
170 uintptr_t real_climit() {
171 return thread_local_.real_climit_;
173 uintptr_t real_jslimit() {
174 return thread_local_.real_jslimit_;
176 Address address_of_jslimit() {
177 return reinterpret_cast<Address>(&thread_local_.jslimit_);
179 Address address_of_real_jslimit() {
180 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
183 // If the stack guard is triggered, but it is not an actual
184 // stack overflow, then handle the interruption accordingly.
185 Object* HandleInterrupts();
186 void HandleGCInterrupt();
191 bool CheckInterrupt(InterruptFlag flag);
192 void RequestInterrupt(InterruptFlag flag);
193 void ClearInterrupt(InterruptFlag flag);
194 bool CheckAndClearInterrupt(InterruptFlag flag);
196 // You should hold the ExecutionAccess lock when calling this method.
197 bool has_pending_interrupts(const ExecutionAccess& lock) {
198 return thread_local_.interrupt_flags_ != 0;
201 // You should hold the ExecutionAccess lock when calling this method.
202 inline void set_interrupt_limits(const ExecutionAccess& lock);
204 // Reset limits to actual values. For example after handling interrupt.
205 // You should hold the ExecutionAccess lock when calling this method.
206 inline void reset_limits(const ExecutionAccess& lock);
208 // Enable or disable interrupts.
209 void EnableInterrupts();
210 void DisableInterrupts();
212 #if V8_TARGET_ARCH_64_BIT
213 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
214 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
216 static const uintptr_t kInterruptLimit = 0xfffffffe;
217 static const uintptr_t kIllegalLimit = 0xfffffff8;
220 void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
221 void PopPostponeInterruptsScope();
223 class ThreadLocal final {
225 ThreadLocal() { Clear(); }
226 // You should hold the ExecutionAccess lock when you call Initialize or
230 // Returns true if the heap's stack limits should be set, false if not.
231 bool Initialize(Isolate* isolate);
233 // The stack limit is split into a JavaScript and a C++ stack limit. These
234 // two are the same except when running on a simulator where the C++ and
235 // JavaScript stacks are separate. Each of the two stack limits have two
236 // values. The one eith the real_ prefix is the actual stack limit
237 // set for the VM. The one without the real_ prefix has the same value as
238 // the actual stack limit except when there is an interruption (e.g. debug
239 // break or preemption) in which case it is lowered to make stack checks
240 // fail. Both the generated code and the runtime system check against the
241 // one without the real_ prefix.
242 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
243 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
245 // jslimit_ and climit_ can be read without any lock.
246 // Writing requires the ExecutionAccess lock.
247 base::AtomicWord jslimit_;
248 base::AtomicWord climit_;
250 uintptr_t jslimit() {
251 return bit_cast<uintptr_t>(base::NoBarrier_Load(&jslimit_));
253 void set_jslimit(uintptr_t limit) {
254 return base::NoBarrier_Store(&jslimit_,
255 static_cast<base::AtomicWord>(limit));
258 return bit_cast<uintptr_t>(base::NoBarrier_Load(&climit_));
260 void set_climit(uintptr_t limit) {
261 return base::NoBarrier_Store(&climit_,
262 static_cast<base::AtomicWord>(limit));
265 PostponeInterruptsScope* postpone_interrupts_;
266 int interrupt_flags_;
269 // TODO(isolates): Technically this could be calculated directly from a
270 // pointer to StackGuard.
272 ThreadLocal thread_local_;
274 friend class Isolate;
275 friend class StackLimitCheck;
276 friend class PostponeInterruptsScope;
278 DISALLOW_COPY_AND_ASSIGN(StackGuard);
281 } } // namespace v8::internal
283 #endif // V8_EXECUTION_H_