blob: a2ddc41a15f506fef49c41295c7a899ed0517195 [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,
41 TERMINATE = 1 << 4
42};
43
44class Execution : public AllStatic {
45 public:
46 // Call a function, the caller supplies a receiver and an array
47 // of arguments. Arguments are Object* type. After function returns,
48 // pointers in 'args' might be invalid.
49 //
50 // *pending_exception tells whether the invoke resulted in
51 // a pending exception.
52 //
53 static Handle<Object> Call(Handle<JSFunction> func,
54 Handle<Object> receiver,
55 int argc,
56 Object*** args,
57 bool* pending_exception);
58
59 // Construct object from function, the caller supplies an array of
60 // arguments. Arguments are Object* type. After function returns,
61 // pointers in 'args' might be invalid.
62 //
63 // *pending_exception tells whether the invoke resulted in
64 // a pending exception.
65 //
66 static Handle<Object> New(Handle<JSFunction> func,
67 int argc,
68 Object*** args,
69 bool* pending_exception);
70
71 // Call a function, just like Call(), but make sure to silently catch
72 // any thrown exceptions. The return value is either the result of
73 // calling the function (if caught exception is false) or the exception
74 // that occurred (if caught exception is true).
75 static Handle<Object> TryCall(Handle<JSFunction> func,
76 Handle<Object> receiver,
77 int argc,
78 Object*** args,
79 bool* caught_exception);
80
81 // ECMA-262 9.2
82 static Handle<Object> ToBoolean(Handle<Object> obj);
83
84 // ECMA-262 9.3
85 static Handle<Object> ToNumber(Handle<Object> obj, bool* exc);
86
87 // ECMA-262 9.4
88 static Handle<Object> ToInteger(Handle<Object> obj, bool* exc);
89
90 // ECMA-262 9.5
91 static Handle<Object> ToInt32(Handle<Object> obj, bool* exc);
92
93 // ECMA-262 9.6
94 static Handle<Object> ToUint32(Handle<Object> obj, bool* exc);
95
96 // ECMA-262 9.8
97 static Handle<Object> ToString(Handle<Object> obj, bool* exc);
98
99 // ECMA-262 9.8
100 static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc);
101
102 // ECMA-262 9.9
103 static Handle<Object> ToObject(Handle<Object> obj, bool* exc);
104
105 // Create a new date object from 'time'.
106 static Handle<Object> NewDate(double time, bool* exc);
107
Ben Murdochf87a2032010-10-22 12:50:53 +0100108 // Create a new regular expression object from 'pattern' and 'flags'.
109 static Handle<JSRegExp> NewJSRegExp(Handle<String> pattern,
110 Handle<String> flags,
111 bool* exc);
112
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 // Used to implement [] notation on strings (calls JS code)
114 static Handle<Object> CharAt(Handle<String> str, uint32_t index);
115
116 static Handle<Object> GetFunctionFor();
117 static Handle<JSFunction> InstantiateFunction(
118 Handle<FunctionTemplateInfo> data, bool* exc);
119 static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data,
120 bool* exc);
121 static void ConfigureInstance(Handle<Object> instance,
122 Handle<Object> data,
123 bool* exc);
124 static Handle<String> GetStackTraceLine(Handle<Object> recv,
125 Handle<JSFunction> fun,
126 Handle<Object> pos,
127 Handle<Object> is_global);
128#ifdef ENABLE_DEBUGGER_SUPPORT
129 static Object* DebugBreakHelper();
Leon Clarkee46be812010-01-19 14:06:41 +0000130 static void ProcessDebugMesssages(bool debug_command_only);
Steve Blocka7e24c12009-10-30 11:49:00 +0000131#endif
132
133 // If the stack guard is triggered, but it is not an actual
134 // stack overflow, then handle the interruption accordingly.
John Reck59135872010-11-02 12:39:01 -0700135 MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt();
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
137 // Get a function delegate (or undefined) for the given non-function
138 // object. Used for support calling objects as functions.
139 static Handle<Object> GetFunctionDelegate(Handle<Object> object);
140
141 // Get a function delegate (or undefined) for the given non-function
142 // object. Used for support calling objects as constructors.
143 static Handle<Object> GetConstructorDelegate(Handle<Object> object);
144};
145
146
147class ExecutionAccess;
148
149
150// StackGuard contains the handling of the limits that are used to limit the
151// number of nested invocations of JavaScript and the stack size used in each
152// invocation.
153class StackGuard : public AllStatic {
154 public:
155 // Pass the address beyond which the stack should not grow. The stack
156 // is assumed to grow downwards.
157 static void SetStackLimit(uintptr_t limit);
158
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 // Threading support.
160 static char* ArchiveStackGuard(char* to);
161 static char* RestoreStackGuard(char* from);
162 static int ArchiveSpacePerThread();
163 static void FreeThreadResources();
164 // Sets up the default stack guard for this thread if it has not
165 // already been set up.
166 static void InitThread(const ExecutionAccess& lock);
167 // Clears the stack guard for this thread so it does not look as if
168 // it has been set up.
169 static void ClearThread(const ExecutionAccess& lock);
170
171 static bool IsStackOverflow();
172 static bool IsPreempted();
173 static void Preempt();
174 static bool IsInterrupted();
175 static void Interrupt();
176 static bool IsTerminateExecution();
177 static void TerminateExecution();
178#ifdef ENABLE_DEBUGGER_SUPPORT
179 static bool IsDebugBreak();
180 static void DebugBreak();
181 static bool IsDebugCommand();
182 static void DebugCommand();
183#endif
184 static void Continue(InterruptFlag after_what);
185
Steve Blockd0582a62009-12-15 09:54:21 +0000186 // This provides an asynchronous read of the stack limits for the current
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 // thread. There are no locks protecting this, but it is assumed that you
188 // have the global V8 lock if you are using multiple V8 threads.
189 static uintptr_t climit() {
190 return thread_local_.climit_;
191 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800192 static uintptr_t real_climit() {
193 return thread_local_.real_climit_;
194 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000195 static uintptr_t jslimit() {
196 return thread_local_.jslimit_;
197 }
Steve Blockd0582a62009-12-15 09:54:21 +0000198 static uintptr_t real_jslimit() {
199 return thread_local_.real_jslimit_;
200 }
201 static Address address_of_jslimit() {
202 return reinterpret_cast<Address>(&thread_local_.jslimit_);
203 }
204 static Address address_of_real_jslimit() {
205 return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
206 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000207
208 private:
209 // You should hold the ExecutionAccess lock when calling this method.
Steve Block6ded16b2010-05-10 14:33:55 +0100210 static bool has_pending_interrupts(const ExecutionAccess& lock) {
211 // Sanity check: We shouldn't be asking about pending interrupts
212 // unless we're not postponing them anymore.
213 ASSERT(!should_postpone_interrupts(lock));
214 return thread_local_.interrupt_flags_ != 0;
215 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000216
217 // You should hold the ExecutionAccess lock when calling this method.
Steve Block6ded16b2010-05-10 14:33:55 +0100218 static bool should_postpone_interrupts(const ExecutionAccess& lock) {
219 return thread_local_.postpone_interrupts_nesting_ > 0;
220 }
221
222 // You should hold the ExecutionAccess lock when calling this method.
223 static void set_interrupt_limits(const ExecutionAccess& lock) {
224 // Ignore attempts to interrupt when interrupts are postponed.
225 if (should_postpone_interrupts(lock)) return;
226 thread_local_.jslimit_ = kInterruptLimit;
227 thread_local_.climit_ = kInterruptLimit;
Steve Blockd0582a62009-12-15 09:54:21 +0000228 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 }
230
Steve Blockd0582a62009-12-15 09:54:21 +0000231 // Reset limits to actual values. For example after handling interrupt.
Steve Blocka7e24c12009-10-30 11:49:00 +0000232 // You should hold the ExecutionAccess lock when calling this method.
233 static void reset_limits(const ExecutionAccess& lock) {
Steve Blockd0582a62009-12-15 09:54:21 +0000234 thread_local_.jslimit_ = thread_local_.real_jslimit_;
235 thread_local_.climit_ = thread_local_.real_climit_;
236 Heap::SetStackLimits();
Steve Blocka7e24c12009-10-30 11:49:00 +0000237 }
238
239 // Enable or disable interrupts.
240 static void EnableInterrupts();
241 static void DisableInterrupts();
242
243 static const uintptr_t kLimitSize = kPointerSize * 128 * KB;
Steve Block3ce2e202009-11-05 08:53:23 +0000244
Steve Blocka7e24c12009-10-30 11:49:00 +0000245#ifdef V8_TARGET_ARCH_X64
246 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
247 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
248#else
249 static const uintptr_t kInterruptLimit = 0xfffffffe;
250 static const uintptr_t kIllegalLimit = 0xfffffff8;
251#endif
252
253 class ThreadLocal {
254 public:
255 ThreadLocal() { Clear(); }
256 // You should hold the ExecutionAccess lock when you call Initialize or
257 // Clear.
258 void Initialize();
259 void Clear();
Steve Blockd0582a62009-12-15 09:54:21 +0000260
261 // The stack limit is split into a JavaScript and a C++ stack limit. These
262 // two are the same except when running on a simulator where the C++ and
263 // JavaScript stacks are separate. Each of the two stack limits have two
264 // values. The one eith the real_ prefix is the actual stack limit
265 // set for the VM. The one without the real_ prefix has the same value as
266 // the actual stack limit except when there is an interruption (e.g. debug
267 // break or preemption) in which case it is lowered to make stack checks
268 // fail. Both the generated code and the runtime system check against the
269 // one without the real_ prefix.
270 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 uintptr_t jslimit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000272 uintptr_t real_climit_; // Actual C++ stack limit set for the VM.
Steve Blocka7e24c12009-10-30 11:49:00 +0000273 uintptr_t climit_;
Steve Blockd0582a62009-12-15 09:54:21 +0000274
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 int nesting_;
276 int postpone_interrupts_nesting_;
277 int interrupt_flags_;
278 };
279
280 static ThreadLocal thread_local_;
281
282 friend class StackLimitCheck;
283 friend class PostponeInterruptsScope;
284};
285
286
287// Support for checking for stack-overflows in C++ code.
288class StackLimitCheck BASE_EMBEDDED {
289 public:
290 bool HasOverflowed() const {
291 // Stack has overflowed in C++ code only if stack pointer exceeds the C++
292 // stack guard and the limits are not set to interrupt values.
293 // TODO(214): Stack overflows are ignored if a interrupt is pending. This
294 // code should probably always use the initial C++ limit.
295 return (reinterpret_cast<uintptr_t>(this) < StackGuard::climit()) &&
296 StackGuard::IsStackOverflow();
297 }
298};
299
300
301// Support for temporarily postponing interrupts. When the outermost
302// postpone scope is left the interrupts will be re-enabled and any
303// interrupts that occurred while in the scope will be taken into
304// account.
305class PostponeInterruptsScope BASE_EMBEDDED {
306 public:
307 PostponeInterruptsScope() {
308 StackGuard::thread_local_.postpone_interrupts_nesting_++;
309 StackGuard::DisableInterrupts();
310 }
311
312 ~PostponeInterruptsScope() {
313 if (--StackGuard::thread_local_.postpone_interrupts_nesting_ == 0) {
314 StackGuard::EnableInterrupts();
315 }
316 }
317};
318
Steve Blocka7e24c12009-10-30 11:49:00 +0000319} } // namespace v8::internal
320
321#endif // V8_EXECUTION_H_