blob: af8ad9aff3f1e1dad598ae75d88506ce16f6352b [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_EXECUTION_H_
29#define V8_EXECUTION_H_
30
31namespace v8 {
32namespace internal {
33
34
35// Flag used to set the interrupt causes.
36enum InterruptFlag {
37 INTERRUPT = 1 << 0,
38 DEBUGBREAK = 1 << 1,
39 DEBUGCOMMAND = 1 << 2,
40 PREEMPT = 1 << 3,
Ben Murdochb0fe1622011-05-05 13:52:32 +010041 TERMINATE = 1 << 4,
42 RUNTIME_PROFILER_TICK = 1 << 5
Steve Blocka7e24c12009-10-30 11:49:00 +000043};
44
45class Execution : public AllStatic {
46 public:
47 // Call a function, the caller supplies a receiver and an array
48 // of arguments. Arguments are Object* type. After function returns,
49 // pointers in 'args' might be invalid.
50 //
51 // *pending_exception tells whether the invoke resulted in
52 // a pending exception.
53 //
54 static Handle<Object> Call(Handle<JSFunction> func,
55 Handle<Object> receiver,
56 int argc,
57 Object*** args,
58 bool* pending_exception);
59
60 // Construct object from function, the caller supplies an array of
61 // arguments. Arguments are Object* type. After function returns,
62 // pointers in 'args' might be invalid.
63 //
64 // *pending_exception tells whether the invoke resulted in
65 // a pending exception.
66 //
67 static Handle<Object> New(Handle<JSFunction> func,
68 int argc,
69 Object*** args,
70 bool* pending_exception);
71
72 // Call a function, just like Call(), but make sure to silently catch
73 // any thrown exceptions. The return value is either the result of
74 // calling the function (if caught exception is false) or the exception
75 // that occurred (if caught exception is true).
76 static Handle<Object> TryCall(Handle<JSFunction> func,
77 Handle<Object> receiver,
78 int argc,
79 Object*** args,
80 bool* caught_exception);
81
82 // ECMA-262 9.2
83 static Handle<Object> ToBoolean(Handle<Object> obj);
84
85 // ECMA-262 9.3
86 static Handle<Object> ToNumber(Handle<Object> obj, bool* exc);
87
88 // ECMA-262 9.4
89 static Handle<Object> ToInteger(Handle<Object> obj, bool* exc);
90
91 // ECMA-262 9.5
92 static Handle<Object> ToInt32(Handle<Object> obj, bool* exc);
93
94 // ECMA-262 9.6
95 static Handle<Object> ToUint32(Handle<Object> obj, bool* exc);
96
97 // ECMA-262 9.8
98 static Handle<Object> ToString(Handle<Object> obj, bool* exc);
99
100 // ECMA-262 9.8
101 static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc);
102
103 // ECMA-262 9.9
104 static Handle<Object> ToObject(Handle<Object> obj, bool* exc);
105
106 // Create a new date object from 'time'.
107 static Handle<Object> NewDate(double time, bool* exc);
108
Ben Murdochf87a2032010-10-22 12:50:53 +0100109 // Create a new regular expression object from 'pattern' and 'flags'.
110 static Handle<JSRegExp> NewJSRegExp(Handle<String> pattern,
111 Handle<String> flags,
112 bool* exc);
113
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 // Used to implement [] notation on strings (calls JS code)
115 static Handle<Object> CharAt(Handle<String> str, uint32_t index);
116
117 static Handle<Object> GetFunctionFor();
118 static Handle<JSFunction> InstantiateFunction(
119 Handle<FunctionTemplateInfo> data, bool* exc);
120 static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data,
121 bool* exc);
122 static void ConfigureInstance(Handle<Object> instance,
123 Handle<Object> data,
124 bool* exc);
125 static Handle<String> GetStackTraceLine(Handle<Object> recv,
126 Handle<JSFunction> fun,
127 Handle<Object> pos,
128 Handle<Object> is_global);
129#ifdef ENABLE_DEBUGGER_SUPPORT
130 static Object* DebugBreakHelper();
Leon Clarkee46be812010-01-19 14:06:41 +0000131 static void ProcessDebugMesssages(bool debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000132#endif
133
134 // If the stack guard is triggered, but it is not an actual
135 // stack overflow, then handle the interruption accordingly.
John Reck59135872010-11-02 12:39:01 -0700136 MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt();
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
138 // Get a function delegate (or undefined) for the given non-function
139 // object. Used for support calling objects as functions.
140 static Handle<Object> GetFunctionDelegate(Handle<Object> object);
141
142 // Get a function delegate (or undefined) for the given non-function
143 // object. Used for support calling objects as constructors.
144 static Handle<Object> GetConstructorDelegate(Handle<Object> object);
145};
146
147
148class ExecutionAccess;
149
150
151// StackGuard contains the handling of the limits that are used to limit the
152// number of nested invocations of JavaScript and the stack size used in each
153// invocation.
154class StackGuard : public AllStatic {
155 public:
156 // Pass the address beyond which the stack should not grow. The stack
157 // is assumed to grow downwards.
158 static void SetStackLimit(uintptr_t limit);
159
Steve Blocka7e24c12009-10-30 11:49:00 +0000160 // Threading support.
161 static char* ArchiveStackGuard(char* to);
162 static char* RestoreStackGuard(char* from);
163 static int ArchiveSpacePerThread();
164 static void FreeThreadResources();
165 // Sets up the default stack guard for this thread if it has not
166 // already been set up.
167 static void InitThread(const ExecutionAccess& lock);
168 // Clears the stack guard for this thread so it does not look as if
169 // it has been set up.
170 static void ClearThread(const ExecutionAccess& lock);
171
172 static bool IsStackOverflow();
173 static bool IsPreempted();
174 static void Preempt();
175 static bool IsInterrupted();
176 static void Interrupt();
177 static bool IsTerminateExecution();
178 static void TerminateExecution();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100179 static bool IsRuntimeProfilerTick();
180 static void RequestRuntimeProfilerTick();
Steve Blocka7e24c12009-10-30 11:49:00 +0000181#ifdef ENABLE_DEBUGGER_SUPPORT
182 static bool IsDebugBreak();
183 static void DebugBreak();
184 static bool IsDebugCommand();
185 static void DebugCommand();
186#endif
187 static void Continue(InterruptFlag after_what);
188
Steve Blockd0582a62009-12-15 09:54:21 +0000189 // This provides an asynchronous read of the stack limits for the current
Steve Blocka7e24c12009-10-30 11:49:00 +0000190 // thread. There are no locks protecting this, but it is assumed that you
191 // have the global V8 lock if you are using multiple V8 threads.
192 static uintptr_t climit() {
193 return thread_local_.climit_;
194 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800195 static uintptr_t real_climit() {
196 return thread_local_.real_climit_;
197 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 static uintptr_t jslimit() {
199 return thread_local_.jslimit_;
200 }
Steve Blockd0582a62009-12-15 09:54:21 +0000201 static uintptr_t real_jslimit() {
202 return thread_local_.real_jslimit_;
203 }
204 static Address address_of_jslimit() {
205 return reinterpret_cast<Address>(&thread_local_.jslimit_);
206 }
207 static Address address_of_real_jslimit() {
208 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
209 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000210
211 private:
212 // You should hold the ExecutionAccess lock when calling this method.
Steve Block6ded16b2010-05-10 14:33:55 +0100213 static bool has_pending_interrupts(const ExecutionAccess& lock) {
214 // Sanity check: We shouldn't be asking about pending interrupts
215 // unless we're not postponing them anymore.
216 ASSERT(!should_postpone_interrupts(lock));
217 return thread_local_.interrupt_flags_ != 0;
218 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000219
220 // You should hold the ExecutionAccess lock when calling this method.
Steve Block6ded16b2010-05-10 14:33:55 +0100221 static bool should_postpone_interrupts(const ExecutionAccess& lock) {
222 return thread_local_.postpone_interrupts_nesting_ > 0;
223 }
224
225 // You should hold the ExecutionAccess lock when calling this method.
226 static void set_interrupt_limits(const ExecutionAccess& lock) {
227 // Ignore attempts to interrupt when interrupts are postponed.
228 if (should_postpone_interrupts(lock)) return;
229 thread_local_.jslimit_ = kInterruptLimit;
230 thread_local_.climit_ = kInterruptLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000231 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 }
233
Steve Blockd0582a62009-12-15 09:54:21 +0000234 // Reset limits to actual values. For example after handling interrupt.
Steve Blocka7e24c12009-10-30 11:49:00 +0000235 // You should hold the ExecutionAccess lock when calling this method.
236 static void reset_limits(const ExecutionAccess& lock) {
Steve Blockd0582a62009-12-15 09:54:21 +0000237 thread_local_.jslimit_ = thread_local_.real_jslimit_;
238 thread_local_.climit_ = thread_local_.real_climit_;
239 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000240 }
241
242 // Enable or disable interrupts.
243 static void EnableInterrupts();
244 static void DisableInterrupts();
245
246 static const uintptr_t kLimitSize = kPointerSize * 128 * KB;
Steve Block3ce2e202009-11-05 08:53:23 +0000247
Steve Blocka7e24c12009-10-30 11:49:00 +0000248#ifdef V8_TARGET_ARCH_X64
249 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
250 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
251#else
252 static const uintptr_t kInterruptLimit = 0xfffffffe;
253 static const uintptr_t kIllegalLimit = 0xfffffff8;
254#endif
255
256 class ThreadLocal {
257 public:
258 ThreadLocal() { Clear(); }
259 // You should hold the ExecutionAccess lock when you call Initialize or
260 // Clear.
261 void Initialize();
262 void Clear();
Steve Blockd0582a62009-12-15 09:54:21 +0000263
264 // The stack limit is split into a JavaScript and a C++ stack limit. These
265 // two are the same except when running on a simulator where the C++ and
266 // JavaScript stacks are separate. Each of the two stack limits have two
267 // values. The one eith the real_ prefix is the actual stack limit
268 // set for the VM. The one without the real_ prefix has the same value as
269 // the actual stack limit except when there is an interruption (e.g. debug
270 // break or preemption) in which case it is lowered to make stack checks
271 // fail. Both the generated code and the runtime system check against the
272 // one without the real_ prefix.
273 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 uintptr_t jslimit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000275 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 uintptr_t climit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000277
Steve Blocka7e24c12009-10-30 11:49:00 +0000278 int nesting_;
279 int postpone_interrupts_nesting_;
280 int interrupt_flags_;
281 };
282
283 static ThreadLocal thread_local_;
284
285 friend class StackLimitCheck;
286 friend class PostponeInterruptsScope;
287};
288
289
290// Support for checking for stack-overflows in C++ code.
291class StackLimitCheck BASE_EMBEDDED {
292 public:
293 bool HasOverflowed() const {
294 // Stack has overflowed in C++ code only if stack pointer exceeds the C++
295 // stack guard and the limits are not set to interrupt values.
296 // TODO(214): Stack overflows are ignored if a interrupt is pending. This
297 // code should probably always use the initial C++ limit.
298 return (reinterpret_cast<uintptr_t>(this) < StackGuard::climit()) &&
299 StackGuard::IsStackOverflow();
300 }
301};
302
303
304// Support for temporarily postponing interrupts. When the outermost
305// postpone scope is left the interrupts will be re-enabled and any
306// interrupts that occurred while in the scope will be taken into
307// account.
308class PostponeInterruptsScope BASE_EMBEDDED {
309 public:
310 PostponeInterruptsScope() {
311 StackGuard::thread_local_.postpone_interrupts_nesting_++;
312 StackGuard::DisableInterrupts();
313 }
314
315 ~PostponeInterruptsScope() {
316 if (--StackGuard::thread_local_.postpone_interrupts_nesting_ == 0) {
317 StackGuard::EnableInterrupts();
318 }
319 }
320};
321
Steve Blocka7e24c12009-10-30 11:49:00 +0000322} } // namespace v8::internal
323
324#endif // V8_EXECUTION_H_