Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | #include "allocation.h" |
| 32 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | |
| 37 | // Flag used to set the interrupt causes. |
| 38 | enum InterruptFlag { |
| 39 | INTERRUPT = 1 << 0, |
| 40 | DEBUGBREAK = 1 << 1, |
| 41 | DEBUGCOMMAND = 1 << 2, |
| 42 | PREEMPT = 1 << 3, |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 43 | TERMINATE = 1 << 4, |
| 44 | RUNTIME_PROFILER_TICK = 1 << 5 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | class Execution : public AllStatic { |
| 48 | public: |
| 49 | // Call a function, the caller supplies a receiver and an array |
| 50 | // of arguments. Arguments are Object* type. After function returns, |
| 51 | // pointers in 'args' might be invalid. |
| 52 | // |
| 53 | // *pending_exception tells whether the invoke resulted in |
| 54 | // a pending exception. |
| 55 | // |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 56 | // When convert_receiver is set, and the receiver is not an object, |
| 57 | // and the function called is not in strict mode, receiver is converted to |
| 58 | // an object. |
| 59 | // |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 60 | static Handle<Object> Call(Handle<Object> callable, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | Handle<Object> receiver, |
| 62 | int argc, |
| 63 | Object*** args, |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 64 | bool* pending_exception, |
| 65 | bool convert_receiver = false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 66 | |
| 67 | // Construct object from function, the caller supplies an array of |
| 68 | // arguments. Arguments are Object* type. After function returns, |
| 69 | // pointers in 'args' might be invalid. |
| 70 | // |
| 71 | // *pending_exception tells whether the invoke resulted in |
| 72 | // a pending exception. |
| 73 | // |
| 74 | static Handle<Object> New(Handle<JSFunction> func, |
| 75 | int argc, |
| 76 | Object*** args, |
| 77 | bool* pending_exception); |
| 78 | |
| 79 | // Call a function, just like Call(), but make sure to silently catch |
| 80 | // any thrown exceptions. The return value is either the result of |
| 81 | // calling the function (if caught exception is false) or the exception |
| 82 | // that occurred (if caught exception is true). |
| 83 | static Handle<Object> TryCall(Handle<JSFunction> func, |
| 84 | Handle<Object> receiver, |
| 85 | int argc, |
| 86 | Object*** args, |
| 87 | bool* caught_exception); |
| 88 | |
| 89 | // ECMA-262 9.2 |
| 90 | static Handle<Object> ToBoolean(Handle<Object> obj); |
| 91 | |
| 92 | // ECMA-262 9.3 |
| 93 | static Handle<Object> ToNumber(Handle<Object> obj, bool* exc); |
| 94 | |
| 95 | // ECMA-262 9.4 |
| 96 | static Handle<Object> ToInteger(Handle<Object> obj, bool* exc); |
| 97 | |
| 98 | // ECMA-262 9.5 |
| 99 | static Handle<Object> ToInt32(Handle<Object> obj, bool* exc); |
| 100 | |
| 101 | // ECMA-262 9.6 |
| 102 | static Handle<Object> ToUint32(Handle<Object> obj, bool* exc); |
| 103 | |
| 104 | // ECMA-262 9.8 |
| 105 | static Handle<Object> ToString(Handle<Object> obj, bool* exc); |
| 106 | |
| 107 | // ECMA-262 9.8 |
| 108 | static Handle<Object> ToDetailString(Handle<Object> obj, bool* exc); |
| 109 | |
| 110 | // ECMA-262 9.9 |
| 111 | static Handle<Object> ToObject(Handle<Object> obj, bool* exc); |
| 112 | |
| 113 | // Create a new date object from 'time'. |
| 114 | static Handle<Object> NewDate(double time, bool* exc); |
| 115 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 116 | // Create a new regular expression object from 'pattern' and 'flags'. |
| 117 | static Handle<JSRegExp> NewJSRegExp(Handle<String> pattern, |
| 118 | Handle<String> flags, |
| 119 | bool* exc); |
| 120 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 121 | // Used to implement [] notation on strings (calls JS code) |
| 122 | static Handle<Object> CharAt(Handle<String> str, uint32_t index); |
| 123 | |
| 124 | static Handle<Object> GetFunctionFor(); |
| 125 | static Handle<JSFunction> InstantiateFunction( |
| 126 | Handle<FunctionTemplateInfo> data, bool* exc); |
| 127 | static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data, |
| 128 | bool* exc); |
| 129 | static void ConfigureInstance(Handle<Object> instance, |
| 130 | Handle<Object> data, |
| 131 | bool* exc); |
| 132 | static Handle<String> GetStackTraceLine(Handle<Object> recv, |
| 133 | Handle<JSFunction> fun, |
| 134 | Handle<Object> pos, |
| 135 | Handle<Object> is_global); |
| 136 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 137 | static Object* DebugBreakHelper(); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 138 | static void ProcessDebugMesssages(bool debug_command_only); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 139 | #endif |
| 140 | |
| 141 | // If the stack guard is triggered, but it is not an actual |
| 142 | // stack overflow, then handle the interruption accordingly. |
John Reck | 5913587 | 2010-11-02 12:39:01 -0700 | [diff] [blame] | 143 | MUST_USE_RESULT static MaybeObject* HandleStackGuardInterrupt(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 144 | |
| 145 | // Get a function delegate (or undefined) for the given non-function |
| 146 | // object. Used for support calling objects as functions. |
| 147 | static Handle<Object> GetFunctionDelegate(Handle<Object> object); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 148 | static Handle<Object> TryGetFunctionDelegate(Handle<Object> object, |
| 149 | bool* has_pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | |
| 151 | // Get a function delegate (or undefined) for the given non-function |
| 152 | // object. Used for support calling objects as constructors. |
| 153 | static Handle<Object> GetConstructorDelegate(Handle<Object> object); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 154 | static Handle<Object> TryGetConstructorDelegate(Handle<Object> object, |
| 155 | bool* has_pending_exception); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | |
| 159 | class ExecutionAccess; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 160 | class Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | |
| 162 | |
| 163 | // StackGuard contains the handling of the limits that are used to limit the |
| 164 | // number of nested invocations of JavaScript and the stack size used in each |
| 165 | // invocation. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 166 | class StackGuard { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | public: |
| 168 | // Pass the address beyond which the stack should not grow. The stack |
| 169 | // is assumed to grow downwards. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 170 | void SetStackLimit(uintptr_t limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | // Threading support. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 173 | char* ArchiveStackGuard(char* to); |
| 174 | char* RestoreStackGuard(char* from); |
| 175 | static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); } |
| 176 | void FreeThreadResources(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | // Sets up the default stack guard for this thread if it has not |
| 178 | // already been set up. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 179 | void InitThread(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | // Clears the stack guard for this thread so it does not look as if |
| 181 | // it has been set up. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 182 | void ClearThread(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 183 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 184 | bool IsStackOverflow(); |
| 185 | bool IsPreempted(); |
| 186 | void Preempt(); |
| 187 | bool IsInterrupted(); |
| 188 | void Interrupt(); |
| 189 | bool IsTerminateExecution(); |
| 190 | void TerminateExecution(); |
| 191 | bool IsRuntimeProfilerTick(); |
| 192 | void RequestRuntimeProfilerTick(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 193 | #ifdef ENABLE_DEBUGGER_SUPPORT |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 194 | bool IsDebugBreak(); |
| 195 | void DebugBreak(); |
| 196 | bool IsDebugCommand(); |
| 197 | void DebugCommand(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 198 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 199 | void Continue(InterruptFlag after_what); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 201 | // This provides an asynchronous read of the stack limits for the current |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 202 | // thread. There are no locks protecting this, but it is assumed that you |
| 203 | // have the global V8 lock if you are using multiple V8 threads. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | uintptr_t climit() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 205 | return thread_local_.climit_; |
| 206 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 207 | uintptr_t real_climit() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 208 | return thread_local_.real_climit_; |
| 209 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 210 | uintptr_t jslimit() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 211 | return thread_local_.jslimit_; |
| 212 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 213 | uintptr_t real_jslimit() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 214 | return thread_local_.real_jslimit_; |
| 215 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 216 | Address address_of_jslimit() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 217 | return reinterpret_cast<Address>(&thread_local_.jslimit_); |
| 218 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 219 | Address address_of_real_jslimit() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 220 | return reinterpret_cast<Address>(&thread_local_.real_jslimit_); |
| 221 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | |
| 223 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 224 | StackGuard(); |
| 225 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | // You should hold the ExecutionAccess lock when calling this method. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 227 | bool has_pending_interrupts(const ExecutionAccess& lock) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 228 | // Sanity check: We shouldn't be asking about pending interrupts |
| 229 | // unless we're not postponing them anymore. |
| 230 | ASSERT(!should_postpone_interrupts(lock)); |
| 231 | return thread_local_.interrupt_flags_ != 0; |
| 232 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | |
| 234 | // You should hold the ExecutionAccess lock when calling this method. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 235 | bool should_postpone_interrupts(const ExecutionAccess& lock) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 236 | return thread_local_.postpone_interrupts_nesting_ > 0; |
| 237 | } |
| 238 | |
| 239 | // You should hold the ExecutionAccess lock when calling this method. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 240 | inline void set_interrupt_limits(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 241 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 242 | // Reset limits to actual values. For example after handling interrupt. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | // You should hold the ExecutionAccess lock when calling this method. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 244 | inline void reset_limits(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 245 | |
| 246 | // Enable or disable interrupts. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 247 | void EnableInterrupts(); |
| 248 | void DisableInterrupts(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 249 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | #ifdef V8_TARGET_ARCH_X64 |
| 251 | static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); |
| 252 | static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); |
| 253 | #else |
| 254 | static const uintptr_t kInterruptLimit = 0xfffffffe; |
| 255 | static const uintptr_t kIllegalLimit = 0xfffffff8; |
| 256 | #endif |
| 257 | |
| 258 | class ThreadLocal { |
| 259 | public: |
| 260 | ThreadLocal() { Clear(); } |
| 261 | // You should hold the ExecutionAccess lock when you call Initialize or |
| 262 | // Clear. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | void Clear(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 264 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 265 | // Returns true if the heap's stack limits should be set, false if not. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 266 | bool Initialize(Isolate* isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 267 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 268 | // The stack limit is split into a JavaScript and a C++ stack limit. These |
| 269 | // two are the same except when running on a simulator where the C++ and |
| 270 | // JavaScript stacks are separate. Each of the two stack limits have two |
| 271 | // values. The one eith the real_ prefix is the actual stack limit |
| 272 | // set for the VM. The one without the real_ prefix has the same value as |
| 273 | // the actual stack limit except when there is an interruption (e.g. debug |
| 274 | // break or preemption) in which case it is lowered to make stack checks |
| 275 | // fail. Both the generated code and the runtime system check against the |
| 276 | // one without the real_ prefix. |
| 277 | uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | uintptr_t jslimit_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 279 | uintptr_t real_climit_; // Actual C++ stack limit set for the VM. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | uintptr_t climit_; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 281 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 282 | int nesting_; |
| 283 | int postpone_interrupts_nesting_; |
| 284 | int interrupt_flags_; |
| 285 | }; |
| 286 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 287 | // TODO(isolates): Technically this could be calculated directly from a |
| 288 | // pointer to StackGuard. |
| 289 | Isolate* isolate_; |
| 290 | ThreadLocal thread_local_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 291 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 292 | friend class Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 293 | friend class StackLimitCheck; |
| 294 | friend class PostponeInterruptsScope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 295 | |
| 296 | DISALLOW_COPY_AND_ASSIGN(StackGuard); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | } } // namespace v8::internal |
| 301 | |
| 302 | #endif // V8_EXECUTION_H_ |