| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // object. Used for support calling objects as functions. | 139 // object. Used for support calling objects as functions. |
| 140 static Handle<Object> GetFunctionDelegate(Handle<Object> object); | 140 static Handle<Object> GetFunctionDelegate(Handle<Object> object); |
| 141 | 141 |
| 142 // Get a function delegate (or undefined) for the given non-function | 142 // Get a function delegate (or undefined) for the given non-function |
| 143 // object. Used for support calling objects as constructors. | 143 // object. Used for support calling objects as constructors. |
| 144 static Handle<Object> GetConstructorDelegate(Handle<Object> object); | 144 static Handle<Object> GetConstructorDelegate(Handle<Object> object); |
| 145 }; | 145 }; |
| 146 | 146 |
| 147 | 147 |
| 148 class ExecutionAccess; | 148 class ExecutionAccess; |
| 149 class Isolate; |
| 149 | 150 |
| 150 | 151 |
| 151 // StackGuard contains the handling of the limits that are used to limit the | 152 // 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 // number of nested invocations of JavaScript and the stack size used in each |
| 153 // invocation. | 154 // invocation. |
| 154 class StackGuard : public AllStatic { | 155 class StackGuard { |
| 155 public: | 156 public: |
| 156 // Pass the address beyond which the stack should not grow. The stack | 157 // Pass the address beyond which the stack should not grow. The stack |
| 157 // is assumed to grow downwards. | 158 // is assumed to grow downwards. |
| 158 static void SetStackLimit(uintptr_t limit); | 159 void SetStackLimit(uintptr_t limit); |
| 159 | 160 |
| 160 // Threading support. | 161 // Threading support. |
| 161 static char* ArchiveStackGuard(char* to); | 162 char* ArchiveStackGuard(char* to); |
| 162 static char* RestoreStackGuard(char* from); | 163 char* RestoreStackGuard(char* from); |
| 163 static int ArchiveSpacePerThread(); | 164 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); } |
| 164 static void FreeThreadResources(); | 165 void FreeThreadResources(); |
| 165 // Sets up the default stack guard for this thread if it has not | 166 // Sets up the default stack guard for this thread if it has not |
| 166 // already been set up. | 167 // already been set up. |
| 167 static void InitThread(const ExecutionAccess& lock); | 168 void InitThread(const ExecutionAccess& lock); |
| 168 // Clears the stack guard for this thread so it does not look as if | 169 // Clears the stack guard for this thread so it does not look as if |
| 169 // it has been set up. | 170 // it has been set up. |
| 170 static void ClearThread(const ExecutionAccess& lock); | 171 void ClearThread(const ExecutionAccess& lock); |
| 171 | 172 |
| 172 static bool IsStackOverflow(); | 173 bool IsStackOverflow(); |
| 173 static bool IsPreempted(); | 174 bool IsPreempted(); |
| 174 static void Preempt(); | 175 void Preempt(); |
| 175 static bool IsInterrupted(); | 176 bool IsInterrupted(); |
| 176 static void Interrupt(); | 177 void Interrupt(); |
| 177 static bool IsTerminateExecution(); | 178 bool IsTerminateExecution(); |
| 178 static void TerminateExecution(); | 179 void TerminateExecution(); |
| 179 static bool IsRuntimeProfilerTick(); | 180 bool IsRuntimeProfilerTick(); |
| 180 static void RequestRuntimeProfilerTick(); | 181 void RequestRuntimeProfilerTick(); |
| 181 #ifdef ENABLE_DEBUGGER_SUPPORT | 182 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 182 static bool IsDebugBreak(); | 183 bool IsDebugBreak(); |
| 183 static void DebugBreak(); | 184 void DebugBreak(); |
| 184 static bool IsDebugCommand(); | 185 bool IsDebugCommand(); |
| 185 static void DebugCommand(); | 186 void DebugCommand(); |
| 186 #endif | 187 #endif |
| 187 static void Continue(InterruptFlag after_what); | 188 void Continue(InterruptFlag after_what); |
| 188 | 189 |
| 189 // This provides an asynchronous read of the stack limits for the current | 190 // This provides an asynchronous read of the stack limits for the current |
| 190 // thread. There are no locks protecting this, but it is assumed that you | 191 // 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 // have the global V8 lock if you are using multiple V8 threads. |
| 192 static uintptr_t climit() { | 193 uintptr_t climit() { |
| 193 return thread_local_.climit_; | 194 return thread_local_.climit_; |
| 194 } | 195 } |
| 195 static uintptr_t real_climit() { | 196 uintptr_t real_climit() { |
| 196 return thread_local_.real_climit_; | 197 return thread_local_.real_climit_; |
| 197 } | 198 } |
| 198 static uintptr_t jslimit() { | 199 uintptr_t jslimit() { |
| 199 return thread_local_.jslimit_; | 200 return thread_local_.jslimit_; |
| 200 } | 201 } |
| 201 static uintptr_t real_jslimit() { | 202 uintptr_t real_jslimit() { |
| 202 return thread_local_.real_jslimit_; | 203 return thread_local_.real_jslimit_; |
| 203 } | 204 } |
| 204 static Address address_of_jslimit() { | 205 Address address_of_jslimit() { |
| 205 return reinterpret_cast<Address>(&thread_local_.jslimit_); | 206 return reinterpret_cast<Address>(&thread_local_.jslimit_); |
| 206 } | 207 } |
| 207 static Address address_of_real_jslimit() { | 208 Address address_of_real_jslimit() { |
| 208 return reinterpret_cast<Address>(&thread_local_.real_jslimit_); | 209 return reinterpret_cast<Address>(&thread_local_.real_jslimit_); |
| 209 } | 210 } |
| 210 | 211 |
| 211 private: | 212 private: |
| 213 StackGuard(); |
| 214 |
| 212 // You should hold the ExecutionAccess lock when calling this method. | 215 // You should hold the ExecutionAccess lock when calling this method. |
| 213 static bool has_pending_interrupts(const ExecutionAccess& lock) { | 216 bool has_pending_interrupts(const ExecutionAccess& lock) { |
| 214 // Sanity check: We shouldn't be asking about pending interrupts | 217 // Sanity check: We shouldn't be asking about pending interrupts |
| 215 // unless we're not postponing them anymore. | 218 // unless we're not postponing them anymore. |
| 216 ASSERT(!should_postpone_interrupts(lock)); | 219 ASSERT(!should_postpone_interrupts(lock)); |
| 217 return thread_local_.interrupt_flags_ != 0; | 220 return thread_local_.interrupt_flags_ != 0; |
| 218 } | 221 } |
| 219 | 222 |
| 220 // You should hold the ExecutionAccess lock when calling this method. | 223 // You should hold the ExecutionAccess lock when calling this method. |
| 221 static bool should_postpone_interrupts(const ExecutionAccess& lock) { | 224 bool should_postpone_interrupts(const ExecutionAccess& lock) { |
| 222 return thread_local_.postpone_interrupts_nesting_ > 0; | 225 return thread_local_.postpone_interrupts_nesting_ > 0; |
| 223 } | 226 } |
| 224 | 227 |
| 225 // You should hold the ExecutionAccess lock when calling this method. | 228 // You should hold the ExecutionAccess lock when calling this method. |
| 226 static void set_interrupt_limits(const ExecutionAccess& lock) { | 229 inline 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; | |
| 231 Heap::SetStackLimits(); | |
| 232 } | |
| 233 | 230 |
| 234 // Reset limits to actual values. For example after handling interrupt. | 231 // Reset limits to actual values. For example after handling interrupt. |
| 235 // You should hold the ExecutionAccess lock when calling this method. | 232 // You should hold the ExecutionAccess lock when calling this method. |
| 236 static void reset_limits(const ExecutionAccess& lock) { | 233 inline void reset_limits(const ExecutionAccess& lock); |
| 237 thread_local_.jslimit_ = thread_local_.real_jslimit_; | |
| 238 thread_local_.climit_ = thread_local_.real_climit_; | |
| 239 Heap::SetStackLimits(); | |
| 240 } | |
| 241 | 234 |
| 242 // Enable or disable interrupts. | 235 // Enable or disable interrupts. |
| 243 static void EnableInterrupts(); | 236 void EnableInterrupts(); |
| 244 static void DisableInterrupts(); | 237 void DisableInterrupts(); |
| 245 | 238 |
| 246 #ifdef V8_TARGET_ARCH_X64 | 239 #ifdef V8_TARGET_ARCH_X64 |
| 247 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); | 240 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); |
| 248 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); | 241 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); |
| 249 #else | 242 #else |
| 250 static const uintptr_t kInterruptLimit = 0xfffffffe; | 243 static const uintptr_t kInterruptLimit = 0xfffffffe; |
| 251 static const uintptr_t kIllegalLimit = 0xfffffff8; | 244 static const uintptr_t kIllegalLimit = 0xfffffff8; |
| 252 #endif | 245 #endif |
| 253 | 246 |
| 254 class ThreadLocal { | 247 class ThreadLocal { |
| 255 public: | 248 public: |
| 256 ThreadLocal() { Clear(); } | 249 ThreadLocal() { Clear(); } |
| 257 // You should hold the ExecutionAccess lock when you call Initialize or | 250 // You should hold the ExecutionAccess lock when you call Initialize or |
| 258 // Clear. | 251 // Clear. |
| 259 void Initialize(); | |
| 260 void Clear(); | 252 void Clear(); |
| 261 | 253 |
| 254 // Returns true if the heap's stack limits should be set, false if not. |
| 255 bool Initialize(); |
| 256 |
| 262 // The stack limit is split into a JavaScript and a C++ stack limit. These | 257 // The stack limit is split into a JavaScript and a C++ stack limit. These |
| 263 // two are the same except when running on a simulator where the C++ and | 258 // two are the same except when running on a simulator where the C++ and |
| 264 // JavaScript stacks are separate. Each of the two stack limits have two | 259 // JavaScript stacks are separate. Each of the two stack limits have two |
| 265 // values. The one eith the real_ prefix is the actual stack limit | 260 // values. The one eith the real_ prefix is the actual stack limit |
| 266 // set for the VM. The one without the real_ prefix has the same value as | 261 // set for the VM. The one without the real_ prefix has the same value as |
| 267 // the actual stack limit except when there is an interruption (e.g. debug | 262 // the actual stack limit except when there is an interruption (e.g. debug |
| 268 // break or preemption) in which case it is lowered to make stack checks | 263 // break or preemption) in which case it is lowered to make stack checks |
| 269 // fail. Both the generated code and the runtime system check against the | 264 // fail. Both the generated code and the runtime system check against the |
| 270 // one without the real_ prefix. | 265 // one without the real_ prefix. |
| 271 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. | 266 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. |
| 272 uintptr_t jslimit_; | 267 uintptr_t jslimit_; |
| 273 uintptr_t real_climit_; // Actual C++ stack limit set for the VM. | 268 uintptr_t real_climit_; // Actual C++ stack limit set for the VM. |
| 274 uintptr_t climit_; | 269 uintptr_t climit_; |
| 275 | 270 |
| 276 int nesting_; | 271 int nesting_; |
| 277 int postpone_interrupts_nesting_; | 272 int postpone_interrupts_nesting_; |
| 278 int interrupt_flags_; | 273 int interrupt_flags_; |
| 279 }; | 274 }; |
| 280 | 275 |
| 281 static ThreadLocal thread_local_; | 276 // TODO(isolates): Technically this could be calculated directly from a |
| 277 // pointer to StackGuard. |
| 278 Isolate* isolate_; |
| 279 ThreadLocal thread_local_; |
| 282 | 280 |
| 281 friend class Isolate; |
| 283 friend class StackLimitCheck; | 282 friend class StackLimitCheck; |
| 284 friend class PostponeInterruptsScope; | 283 friend class PostponeInterruptsScope; |
| 284 |
| 285 DISALLOW_COPY_AND_ASSIGN(StackGuard); |
| 285 }; | 286 }; |
| 286 | 287 |
| 287 | 288 |
| 288 // Support for checking for stack-overflows in C++ code. | |
| 289 class StackLimitCheck BASE_EMBEDDED { | |
| 290 public: | |
| 291 bool HasOverflowed() const { | |
| 292 // Stack has overflowed in C++ code only if stack pointer exceeds the C++ | |
| 293 // stack guard and the limits are not set to interrupt values. | |
| 294 // TODO(214): Stack overflows are ignored if a interrupt is pending. This | |
| 295 // code should probably always use the initial C++ limit. | |
| 296 return (reinterpret_cast<uintptr_t>(this) < StackGuard::climit()) && | |
| 297 StackGuard::IsStackOverflow(); | |
| 298 } | |
| 299 }; | |
| 300 | |
| 301 | |
| 302 // Support for temporarily postponing interrupts. When the outermost | |
| 303 // postpone scope is left the interrupts will be re-enabled and any | |
| 304 // interrupts that occurred while in the scope will be taken into | |
| 305 // account. | |
| 306 class PostponeInterruptsScope BASE_EMBEDDED { | |
| 307 public: | |
| 308 PostponeInterruptsScope() { | |
| 309 StackGuard::thread_local_.postpone_interrupts_nesting_++; | |
| 310 StackGuard::DisableInterrupts(); | |
| 311 } | |
| 312 | |
| 313 ~PostponeInterruptsScope() { | |
| 314 if (--StackGuard::thread_local_.postpone_interrupts_nesting_ == 0) { | |
| 315 StackGuard::EnableInterrupts(); | |
| 316 } | |
| 317 } | |
| 318 }; | |
| 319 | |
| 320 } } // namespace v8::internal | 289 } } // namespace v8::internal |
| 321 | 290 |
| 322 #endif // V8_EXECUTION_H_ | 291 #endif // V8_EXECUTION_H_ |
| OLD | NEW |