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 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
| 34 | |
| 35 | // Flag used to set the interrupt causes. |
| 36 | enum InterruptFlag { |
| 37 | INTERRUPT = 1 << 0, |
| 38 | DEBUGBREAK = 1 << 1, |
| 39 | DEBUGCOMMAND = 1 << 2, |
| 40 | PREEMPT = 1 << 3, |
| 41 | TERMINATE = 1 << 4 |
| 42 | }; |
| 43 | |
| 44 | class 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 | |
| 108 | // Used to implement [] notation on strings (calls JS code) |
| 109 | static Handle<Object> CharAt(Handle<String> str, uint32_t index); |
| 110 | |
| 111 | static Handle<Object> GetFunctionFor(); |
| 112 | static Handle<JSFunction> InstantiateFunction( |
| 113 | Handle<FunctionTemplateInfo> data, bool* exc); |
| 114 | static Handle<JSObject> InstantiateObject(Handle<ObjectTemplateInfo> data, |
| 115 | bool* exc); |
| 116 | static void ConfigureInstance(Handle<Object> instance, |
| 117 | Handle<Object> data, |
| 118 | bool* exc); |
| 119 | static Handle<String> GetStackTraceLine(Handle<Object> recv, |
| 120 | Handle<JSFunction> fun, |
| 121 | Handle<Object> pos, |
| 122 | Handle<Object> is_global); |
| 123 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 124 | static Object* DebugBreakHelper(); |
| 125 | #endif |
| 126 | |
| 127 | // If the stack guard is triggered, but it is not an actual |
| 128 | // stack overflow, then handle the interruption accordingly. |
| 129 | static Object* HandleStackGuardInterrupt(); |
| 130 | |
| 131 | // Get a function delegate (or undefined) for the given non-function |
| 132 | // object. Used for support calling objects as functions. |
| 133 | static Handle<Object> GetFunctionDelegate(Handle<Object> object); |
| 134 | |
| 135 | // Get a function delegate (or undefined) for the given non-function |
| 136 | // object. Used for support calling objects as constructors. |
| 137 | static Handle<Object> GetConstructorDelegate(Handle<Object> object); |
| 138 | }; |
| 139 | |
| 140 | |
| 141 | class ExecutionAccess; |
| 142 | |
| 143 | |
| 144 | // StackGuard contains the handling of the limits that are used to limit the |
| 145 | // number of nested invocations of JavaScript and the stack size used in each |
| 146 | // invocation. |
| 147 | class StackGuard : public AllStatic { |
| 148 | public: |
| 149 | // Pass the address beyond which the stack should not grow. The stack |
| 150 | // is assumed to grow downwards. |
| 151 | static void SetStackLimit(uintptr_t limit); |
| 152 | |
| 153 | static Address address_of_jslimit() { |
| 154 | return reinterpret_cast<Address>(&thread_local_.jslimit_); |
| 155 | } |
| 156 | |
| 157 | // Threading support. |
| 158 | static char* ArchiveStackGuard(char* to); |
| 159 | static char* RestoreStackGuard(char* from); |
| 160 | static int ArchiveSpacePerThread(); |
| 161 | static void FreeThreadResources(); |
| 162 | // Sets up the default stack guard for this thread if it has not |
| 163 | // already been set up. |
| 164 | static void InitThread(const ExecutionAccess& lock); |
| 165 | // Clears the stack guard for this thread so it does not look as if |
| 166 | // it has been set up. |
| 167 | static void ClearThread(const ExecutionAccess& lock); |
| 168 | |
| 169 | static bool IsStackOverflow(); |
| 170 | static bool IsPreempted(); |
| 171 | static void Preempt(); |
| 172 | static bool IsInterrupted(); |
| 173 | static void Interrupt(); |
| 174 | static bool IsTerminateExecution(); |
| 175 | static void TerminateExecution(); |
| 176 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 177 | static bool IsDebugBreak(); |
| 178 | static void DebugBreak(); |
| 179 | static bool IsDebugCommand(); |
| 180 | static void DebugCommand(); |
| 181 | #endif |
| 182 | static void Continue(InterruptFlag after_what); |
| 183 | |
| 184 | // This provides an asynchronous read of the stack limit for the current |
| 185 | // thread. There are no locks protecting this, but it is assumed that you |
| 186 | // have the global V8 lock if you are using multiple V8 threads. |
| 187 | static uintptr_t climit() { |
| 188 | return thread_local_.climit_; |
| 189 | } |
| 190 | |
| 191 | static uintptr_t jslimit() { |
| 192 | return thread_local_.jslimit_; |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | // You should hold the ExecutionAccess lock when calling this method. |
| 197 | static bool IsSet(const ExecutionAccess& lock); |
| 198 | |
| 199 | // You should hold the ExecutionAccess lock when calling this method. |
| 200 | static void set_limits(uintptr_t value, const ExecutionAccess& lock) { |
| 201 | Heap::SetStackLimit(value); |
| 202 | thread_local_.jslimit_ = value; |
| 203 | thread_local_.climit_ = value; |
| 204 | } |
| 205 | |
| 206 | // Reset limits to initial values. For example after handling interrupt. |
| 207 | // You should hold the ExecutionAccess lock when calling this method. |
| 208 | static void reset_limits(const ExecutionAccess& lock) { |
| 209 | thread_local_.jslimit_ = thread_local_.initial_jslimit_; |
| 210 | Heap::SetStackLimit(thread_local_.jslimit_); |
| 211 | thread_local_.climit_ = thread_local_.initial_climit_; |
| 212 | } |
| 213 | |
| 214 | // Enable or disable interrupts. |
| 215 | static void EnableInterrupts(); |
| 216 | static void DisableInterrupts(); |
| 217 | |
| 218 | static const uintptr_t kLimitSize = kPointerSize * 128 * KB; |
| 219 | #ifdef V8_TARGET_ARCH_X64 |
| 220 | static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); |
| 221 | static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); |
| 222 | #else |
| 223 | static const uintptr_t kInterruptLimit = 0xfffffffe; |
| 224 | static const uintptr_t kIllegalLimit = 0xfffffff8; |
| 225 | #endif |
| 226 | |
| 227 | class ThreadLocal { |
| 228 | public: |
| 229 | ThreadLocal() { Clear(); } |
| 230 | // You should hold the ExecutionAccess lock when you call Initialize or |
| 231 | // Clear. |
| 232 | void Initialize(); |
| 233 | void Clear(); |
| 234 | uintptr_t initial_jslimit_; |
| 235 | uintptr_t jslimit_; |
| 236 | uintptr_t initial_climit_; |
| 237 | uintptr_t climit_; |
| 238 | int nesting_; |
| 239 | int postpone_interrupts_nesting_; |
| 240 | int interrupt_flags_; |
| 241 | }; |
| 242 | |
| 243 | static ThreadLocal thread_local_; |
| 244 | |
| 245 | friend class StackLimitCheck; |
| 246 | friend class PostponeInterruptsScope; |
| 247 | }; |
| 248 | |
| 249 | |
| 250 | // Support for checking for stack-overflows in C++ code. |
| 251 | class StackLimitCheck BASE_EMBEDDED { |
| 252 | public: |
| 253 | bool HasOverflowed() const { |
| 254 | // Stack has overflowed in C++ code only if stack pointer exceeds the C++ |
| 255 | // stack guard and the limits are not set to interrupt values. |
| 256 | // TODO(214): Stack overflows are ignored if a interrupt is pending. This |
| 257 | // code should probably always use the initial C++ limit. |
| 258 | return (reinterpret_cast<uintptr_t>(this) < StackGuard::climit()) && |
| 259 | StackGuard::IsStackOverflow(); |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | |
| 264 | // Support for temporarily postponing interrupts. When the outermost |
| 265 | // postpone scope is left the interrupts will be re-enabled and any |
| 266 | // interrupts that occurred while in the scope will be taken into |
| 267 | // account. |
| 268 | class PostponeInterruptsScope BASE_EMBEDDED { |
| 269 | public: |
| 270 | PostponeInterruptsScope() { |
| 271 | StackGuard::thread_local_.postpone_interrupts_nesting_++; |
| 272 | StackGuard::DisableInterrupts(); |
| 273 | } |
| 274 | |
| 275 | ~PostponeInterruptsScope() { |
| 276 | if (--StackGuard::thread_local_.postpone_interrupts_nesting_ == 0) { |
| 277 | StackGuard::EnableInterrupts(); |
| 278 | } |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | |
| 283 | class GCExtension : public v8::Extension { |
| 284 | public: |
| 285 | GCExtension() : v8::Extension("v8/gc", kSource) {} |
| 286 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 287 | v8::Handle<v8::String> name); |
| 288 | static v8::Handle<v8::Value> GC(const v8::Arguments& args); |
| 289 | private: |
| 290 | static const char* kSource; |
| 291 | }; |
| 292 | |
| 293 | |
| 294 | } } // namespace v8::internal |
| 295 | |
| 296 | #endif // V8_EXECUTION_H_ |