blob: 81b71b631efe3dfddd74437585d9cbec5b022c22 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_EXECUTION_H_
6#define V8_EXECUTION_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/allocation.h"
9#include "src/base/atomicops.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/handles.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/utils.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000012
Steve Blocka7e24c12009-10-30 11:49:00 +000013namespace v8 {
14namespace internal {
15
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016class Execution final : public AllStatic {
Steve Blocka7e24c12009-10-30 11:49:00 +000017 public:
18 // Call a function, the caller supplies a receiver and an array
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019 // of arguments.
Steve Blocka7e24c12009-10-30 11:49:00 +000020 //
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021 // When the function called is not in strict mode, receiver is
22 // converted to an object.
Steve Blocka7e24c12009-10-30 11:49:00 +000023 //
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024 MUST_USE_RESULT static MaybeHandle<Object> Call(Isolate* isolate,
25 Handle<Object> callable,
26 Handle<Object> receiver,
27 int argc,
28 Handle<Object> argv[]);
Steve Blocka7e24c12009-10-30 11:49:00 +000029
30 // Construct object from function, the caller supplies an array of
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 // arguments.
32 MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> constructor,
33 int argc,
34 Handle<Object> argv[]);
35 MUST_USE_RESULT static MaybeHandle<Object> New(Isolate* isolate,
36 Handle<Object> constructor,
37 Handle<Object> new_target,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 int argc,
39 Handle<Object> argv[]);
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41 // Call a function, just like Call(), but make sure to silently catch
42 // any thrown exceptions. The return value is either the result of
43 // calling the function (if caught exception is false) or the exception
44 // that occurred (if caught exception is true).
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 // In the exception case, exception_out holds the caught exceptions, unless
46 // it is a termination exception.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047 static MaybeHandle<Object> TryCall(Isolate* isolate, Handle<Object> callable,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 Handle<Object> receiver, int argc,
49 Handle<Object> argv[],
50 MaybeHandle<Object>* exception_out = NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +000051
Steve Blocka7e24c12009-10-30 11:49:00 +000052 // ECMA-262 9.9
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 MUST_USE_RESULT static MaybeHandle<JSReceiver> ToObject(Isolate* isolate,
54 Handle<Object> obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Steve Blocka7e24c12009-10-30 11:49:00 +000056 static Handle<String> GetStackTraceLine(Handle<Object> recv,
57 Handle<JSFunction> fun,
58 Handle<Object> pos,
59 Handle<Object> is_global);
Steve Blocka7e24c12009-10-30 11:49:00 +000060};
61
62
63class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064class PostponeInterruptsScope;
Steve Blocka7e24c12009-10-30 11:49:00 +000065
66
67// StackGuard contains the handling of the limits that are used to limit the
68// number of nested invocations of JavaScript and the stack size used in each
69// invocation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070class StackGuard final {
Steve Blocka7e24c12009-10-30 11:49:00 +000071 public:
72 // Pass the address beyond which the stack should not grow. The stack
73 // is assumed to grow downwards.
Steve Block44f0eee2011-05-26 01:26:41 +010074 void SetStackLimit(uintptr_t limit);
Steve Blocka7e24c12009-10-30 11:49:00 +000075
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000076 // The simulator uses a separate JS stack. Limits on the JS stack might have
77 // to be adjusted in order to reflect overflows of the C stack, because we
78 // cannot rely on the interleaving of frames on the simulator.
79 void AdjustStackLimitForSimulator();
80
Steve Blocka7e24c12009-10-30 11:49:00 +000081 // Threading support.
Steve Block44f0eee2011-05-26 01:26:41 +010082 char* ArchiveStackGuard(char* to);
83 char* RestoreStackGuard(char* from);
84 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
85 void FreeThreadResources();
Steve Blocka7e24c12009-10-30 11:49:00 +000086 // Sets up the default stack guard for this thread if it has not
87 // already been set up.
Steve Block44f0eee2011-05-26 01:26:41 +010088 void InitThread(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +000089 // Clears the stack guard for this thread so it does not look as if
90 // it has been set up.
Steve Block44f0eee2011-05-26 01:26:41 +010091 void ClearThread(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +000092
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093#define INTERRUPT_LIST(V) \
94 V(DEBUGBREAK, DebugBreak, 0) \
95 V(DEBUGCOMMAND, DebugCommand, 1) \
96 V(TERMINATE_EXECUTION, TerminateExecution, 2) \
97 V(GC_REQUEST, GC, 3) \
98 V(INSTALL_CODE, InstallCode, 4) \
99 V(API_INTERRUPT, ApiInterrupt, 5) \
100 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)
101
102#define V(NAME, Name, id) \
103 inline bool Check##Name() { return CheckInterrupt(NAME); } \
104 inline void Request##Name() { RequestInterrupt(NAME); } \
105 inline void Clear##Name() { ClearInterrupt(NAME); }
106 INTERRUPT_LIST(V)
107#undef V
108
109 // Flag used to set the interrupt causes.
110 enum InterruptFlag {
111 #define V(NAME, Name, id) NAME = (1 << id),
112 INTERRUPT_LIST(V)
113 #undef V
114 #define V(NAME, Name, id) NAME |
115 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
116 #undef V
117 };
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 uintptr_t climit() { return thread_local_.climit(); }
120 uintptr_t jslimit() { return thread_local_.jslimit(); }
Steve Blockd0582a62009-12-15 09:54:21 +0000121 // This provides an asynchronous read of the stack limits for the current
Steve Blocka7e24c12009-10-30 11:49:00 +0000122 // thread. There are no locks protecting this, but it is assumed that you
123 // have the global V8 lock if you are using multiple V8 threads.
Steve Block44f0eee2011-05-26 01:26:41 +0100124 uintptr_t real_climit() {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800125 return thread_local_.real_climit_;
126 }
Steve Block44f0eee2011-05-26 01:26:41 +0100127 uintptr_t real_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000128 return thread_local_.real_jslimit_;
129 }
Steve Block44f0eee2011-05-26 01:26:41 +0100130 Address address_of_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000131 return reinterpret_cast<Address>(&thread_local_.jslimit_);
132 }
Steve Block44f0eee2011-05-26 01:26:41 +0100133 Address address_of_real_jslimit() {
Steve Blockd0582a62009-12-15 09:54:21 +0000134 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
135 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136
137 // If the stack guard is triggered, but it is not an actual
138 // stack overflow, then handle the interruption accordingly.
139 Object* HandleInterrupts();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000140 void HandleGCInterrupt();
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
142 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100143 StackGuard();
144
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 bool CheckInterrupt(InterruptFlag flag);
146 void RequestInterrupt(InterruptFlag flag);
147 void ClearInterrupt(InterruptFlag flag);
148 bool CheckAndClearInterrupt(InterruptFlag flag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
150 // You should hold the ExecutionAccess lock when calling this method.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 bool has_pending_interrupts(const ExecutionAccess& lock) {
152 return thread_local_.interrupt_flags_ != 0;
Steve Block6ded16b2010-05-10 14:33:55 +0100153 }
154
155 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100156 inline void set_interrupt_limits(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000157
Steve Blockd0582a62009-12-15 09:54:21 +0000158 // Reset limits to actual values. For example after handling interrupt.
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 // You should hold the ExecutionAccess lock when calling this method.
Steve Block44f0eee2011-05-26 01:26:41 +0100160 inline void reset_limits(const ExecutionAccess& lock);
Steve Blocka7e24c12009-10-30 11:49:00 +0000161
162 // Enable or disable interrupts.
Steve Block44f0eee2011-05-26 01:26:41 +0100163 void EnableInterrupts();
164 void DisableInterrupts();
Steve Blocka7e24c12009-10-30 11:49:00 +0000165
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166#if V8_TARGET_ARCH_64_BIT
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
168 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
169#else
170 static const uintptr_t kInterruptLimit = 0xfffffffe;
171 static const uintptr_t kIllegalLimit = 0xfffffff8;
172#endif
173
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
175 void PopPostponeInterruptsScope();
176
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177 class ThreadLocal final {
Steve Blocka7e24c12009-10-30 11:49:00 +0000178 public:
179 ThreadLocal() { Clear(); }
180 // You should hold the ExecutionAccess lock when you call Initialize or
181 // Clear.
Steve Blocka7e24c12009-10-30 11:49:00 +0000182 void Clear();
Steve Blockd0582a62009-12-15 09:54:21 +0000183
Steve Block44f0eee2011-05-26 01:26:41 +0100184 // Returns true if the heap's stack limits should be set, false if not.
Ben Murdoch257744e2011-11-30 15:57:28 +0000185 bool Initialize(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +0100186
Steve Blockd0582a62009-12-15 09:54:21 +0000187 // The stack limit is split into a JavaScript and a C++ stack limit. These
188 // two are the same except when running on a simulator where the C++ and
189 // JavaScript stacks are separate. Each of the two stack limits have two
190 // values. The one eith the real_ prefix is the actual stack limit
191 // set for the VM. The one without the real_ prefix has the same value as
192 // the actual stack limit except when there is an interruption (e.g. debug
193 // break or preemption) in which case it is lowered to make stack checks
194 // fail. Both the generated code and the runtime system check against the
195 // one without the real_ prefix.
196 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
Steve Blockd0582a62009-12-15 09:54:21 +0000197 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000198
199 // jslimit_ and climit_ can be read without any lock.
200 // Writing requires the ExecutionAccess lock.
201 base::AtomicWord jslimit_;
202 base::AtomicWord climit_;
203
204 uintptr_t jslimit() {
205 return bit_cast<uintptr_t>(base::NoBarrier_Load(&jslimit_));
206 }
207 void set_jslimit(uintptr_t limit) {
208 return base::NoBarrier_Store(&jslimit_,
209 static_cast<base::AtomicWord>(limit));
210 }
211 uintptr_t climit() {
212 return bit_cast<uintptr_t>(base::NoBarrier_Load(&climit_));
213 }
214 void set_climit(uintptr_t limit) {
215 return base::NoBarrier_Store(&climit_,
216 static_cast<base::AtomicWord>(limit));
217 }
Steve Blockd0582a62009-12-15 09:54:21 +0000218
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 PostponeInterruptsScope* postpone_interrupts_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000220 int interrupt_flags_;
221 };
222
Steve Block44f0eee2011-05-26 01:26:41 +0100223 // TODO(isolates): Technically this could be calculated directly from a
224 // pointer to StackGuard.
225 Isolate* isolate_;
226 ThreadLocal thread_local_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000227
Steve Block44f0eee2011-05-26 01:26:41 +0100228 friend class Isolate;
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 friend class StackLimitCheck;
230 friend class PostponeInterruptsScope;
Steve Block44f0eee2011-05-26 01:26:41 +0100231
232 DISALLOW_COPY_AND_ASSIGN(StackGuard);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233};
234
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000235} // namespace internal
236} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000237
238#endif // V8_EXECUTION_H_