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