| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // object. Used for support calling objects as functions. | 132 // object. Used for support calling objects as functions. |
| 133 static Handle<Object> GetFunctionDelegate(Handle<Object> object); | 133 static Handle<Object> GetFunctionDelegate(Handle<Object> object); |
| 134 | 134 |
| 135 // Get a function delegate (or undefined) for the given non-function | 135 // Get a function delegate (or undefined) for the given non-function |
| 136 // object. Used for support calling objects as constructors. | 136 // object. Used for support calling objects as constructors. |
| 137 static Handle<Object> GetConstructorDelegate(Handle<Object> object); | 137 static Handle<Object> GetConstructorDelegate(Handle<Object> object); |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 | 140 |
| 141 class ExecutionAccess; | 141 class ExecutionAccess; |
| 142 class StackGuardPrivateData; |
| 142 | 143 |
| 144 class StackGuardData { |
| 145 class ThreadLocal { |
| 146 public: |
| 147 explicit ThreadLocal(int dummy) { } // static data initialization only |
| 148 ThreadLocal() { Clear(); } |
| 149 // You should hold the ExecutionAccess lock when you call Initialize or |
| 150 // Clear. |
| 151 void Initialize(); |
| 152 void Clear(); |
| 153 |
| 154 // The stack limit is split into a JavaScript and a C++ stack limit. These |
| 155 // two are the same except when running on a simulator where the C++ and |
| 156 // JavaScript stacks are separate. Each of the two stack limits have two |
| 157 // values. The one eith the real_ prefix is the actual stack limit |
| 158 // set for the VM. The one without the real_ prefix has the same value as |
| 159 // the actual stack limit except when there is an interruption (e.g. debug |
| 160 // break or preemption) in which case it is lowered to make stack checks |
| 161 // fail. Both the generated code and the runtime system check against the |
| 162 // one without the real_ prefix. |
| 163 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. |
| 164 uintptr_t jslimit_; |
| 165 uintptr_t real_climit_; // Actual C++ stack limit set for the VM. |
| 166 uintptr_t climit_; |
| 167 |
| 168 int nesting_; |
| 169 int postpone_interrupts_nesting_; |
| 170 int interrupt_flags_; |
| 171 private: |
| 172 ThreadLocal(const ThreadLocal& another); |
| 173 }; |
| 174 |
| 175 // Static state for stack guards. |
| 176 ThreadLocal thread_local_; |
| 177 |
| 178 StackGuardPrivateData* stack_guard_private_data_; |
| 179 |
| 180 friend class V8Context; |
| 181 friend class StackGuard; |
| 182 friend class PostponeInterruptsScope; |
| 183 |
| 184 StackGuardData(); |
| 185 DISALLOW_COPY_AND_ASSIGN(StackGuardData); |
| 186 }; |
| 143 | 187 |
| 144 // StackGuard contains the handling of the limits that are used to limit the | 188 // 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 | 189 // number of nested invocations of JavaScript and the stack size used in each |
| 146 // invocation. | 190 // invocation. |
| 147 class StackGuard : public AllStatic { | 191 class StackGuard : public AllStatic { |
| 148 public: | 192 public: |
| 149 // Pass the address beyond which the stack should not grow. The stack | 193 // Pass the address beyond which the stack should not grow. The stack |
| 150 // is assumed to grow downwards. | 194 // is assumed to grow downwards. |
| 151 static void SetStackLimit(uintptr_t limit); | 195 static void SetStackLimit(uintptr_t limit); |
| 152 | 196 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 174 static void DebugBreak(); | 218 static void DebugBreak(); |
| 175 static bool IsDebugCommand(); | 219 static bool IsDebugCommand(); |
| 176 static void DebugCommand(); | 220 static void DebugCommand(); |
| 177 #endif | 221 #endif |
| 178 static void Continue(InterruptFlag after_what); | 222 static void Continue(InterruptFlag after_what); |
| 179 | 223 |
| 180 // This provides an asynchronous read of the stack limits for the current | 224 // This provides an asynchronous read of the stack limits for the current |
| 181 // thread. There are no locks protecting this, but it is assumed that you | 225 // thread. There are no locks protecting this, but it is assumed that you |
| 182 // have the global V8 lock if you are using multiple V8 threads. | 226 // have the global V8 lock if you are using multiple V8 threads. |
| 183 static uintptr_t climit() { | 227 static uintptr_t climit() { |
| 184 return thread_local_.climit_; | 228 return v8_context()->stack_guard_data_.thread_local_.climit_; |
| 185 } | 229 } |
| 186 static uintptr_t jslimit() { | 230 static uintptr_t jslimit() { |
| 187 return thread_local_.jslimit_; | 231 return v8_context()->stack_guard_data_.thread_local_.jslimit_; |
| 188 } | 232 } |
| 189 static uintptr_t real_jslimit() { | 233 static uintptr_t real_jslimit() { |
| 190 return thread_local_.real_jslimit_; | 234 return v8_context()->stack_guard_data_.thread_local_.real_jslimit_; |
| 191 } | 235 } |
| 192 static Address address_of_jslimit() { | 236 static Address address_of_jslimit() { |
| 193 return reinterpret_cast<Address>(&thread_local_.jslimit_); | 237 return reinterpret_cast<Address>( |
| 238 &v8_context()->stack_guard_data_.thread_local_.jslimit_); |
| 194 } | 239 } |
| 195 static Address address_of_real_jslimit() { | 240 static Address address_of_real_jslimit() { |
| 196 return reinterpret_cast<Address>(&thread_local_.real_jslimit_); | 241 return reinterpret_cast<Address>( |
| 242 &v8_context()->stack_guard_data_.thread_local_.real_jslimit_); |
| 197 } | 243 } |
| 198 | 244 |
| 199 private: | 245 private: |
| 200 // You should hold the ExecutionAccess lock when calling this method. | 246 // You should hold the ExecutionAccess lock when calling this method. |
| 201 static bool IsSet(const ExecutionAccess& lock); | 247 static bool IsSet(const ExecutionAccess& lock); |
| 202 | 248 |
| 203 // You should hold the ExecutionAccess lock when calling this method. | 249 // You should hold the ExecutionAccess lock when calling this method. |
| 204 static void set_limits(uintptr_t value, const ExecutionAccess& lock) { | 250 static void set_limits(uintptr_t value, const ExecutionAccess& lock) { |
| 205 thread_local_.jslimit_ = value; | 251 StackGuardData::ThreadLocal& thread_local = |
| 206 thread_local_.climit_ = value; | 252 v8_context()->stack_guard_data_.thread_local_; |
| 253 thread_local.jslimit_ = value; |
| 254 thread_local.climit_ = value; |
| 207 Heap::SetStackLimits(); | 255 Heap::SetStackLimits(); |
| 208 } | 256 } |
| 209 | 257 |
| 210 // Reset limits to actual values. For example after handling interrupt. | 258 // Reset limits to actual values. For example after handling interrupt. |
| 211 // You should hold the ExecutionAccess lock when calling this method. | 259 // You should hold the ExecutionAccess lock when calling this method. |
| 212 static void reset_limits(const ExecutionAccess& lock) { | 260 static void reset_limits(const ExecutionAccess& lock) { |
| 213 thread_local_.jslimit_ = thread_local_.real_jslimit_; | 261 StackGuardData::ThreadLocal& thread_local = v8_context()-> |
| 214 thread_local_.climit_ = thread_local_.real_climit_; | 262 stack_guard_data_.thread_local_; |
| 263 thread_local.jslimit_ = thread_local.real_jslimit_; |
| 264 thread_local.climit_ = thread_local.real_climit_; |
| 215 Heap::SetStackLimits(); | 265 Heap::SetStackLimits(); |
| 216 } | 266 } |
| 217 | 267 |
| 218 // Enable or disable interrupts. | 268 // Enable or disable interrupts. |
| 219 static void EnableInterrupts(); | 269 static void EnableInterrupts(); |
| 220 static void DisableInterrupts(); | 270 static void DisableInterrupts(); |
| 221 | 271 |
| 222 static const uintptr_t kLimitSize = kPointerSize * 128 * KB; | 272 static const uintptr_t kLimitSize = kPointerSize * 128 * KB; |
| 223 | 273 |
| 224 #ifdef V8_TARGET_ARCH_X64 | 274 #ifdef V8_TARGET_ARCH_X64 |
| 225 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); | 275 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); |
| 226 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); | 276 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); |
| 227 #else | 277 #else |
| 228 static const uintptr_t kInterruptLimit = 0xfffffffe; | 278 static const uintptr_t kInterruptLimit = 0xfffffffe; |
| 229 static const uintptr_t kIllegalLimit = 0xfffffff8; | 279 static const uintptr_t kIllegalLimit = 0xfffffff8; |
| 230 #endif | 280 #endif |
| 231 | 281 |
| 232 class ThreadLocal { | 282 static void PostConstruct(); |
| 233 public: | 283 static void PreDestroy(); |
| 234 ThreadLocal() { Clear(); } | 284 friend class V8Context; |
| 235 // You should hold the ExecutionAccess lock when you call Initialize or | |
| 236 // Clear. | |
| 237 void Initialize(); | |
| 238 void Clear(); | |
| 239 | |
| 240 // The stack limit is split into a JavaScript and a C++ stack limit. These | |
| 241 // two are the same except when running on a simulator where the C++ and | |
| 242 // JavaScript stacks are separate. Each of the two stack limits have two | |
| 243 // values. The one eith the real_ prefix is the actual stack limit | |
| 244 // set for the VM. The one without the real_ prefix has the same value as | |
| 245 // the actual stack limit except when there is an interruption (e.g. debug | |
| 246 // break or preemption) in which case it is lowered to make stack checks | |
| 247 // fail. Both the generated code and the runtime system check against the | |
| 248 // one without the real_ prefix. | |
| 249 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. | |
| 250 uintptr_t jslimit_; | |
| 251 uintptr_t real_climit_; // Actual C++ stack limit set for the VM. | |
| 252 uintptr_t climit_; | |
| 253 | |
| 254 int nesting_; | |
| 255 int postpone_interrupts_nesting_; | |
| 256 int interrupt_flags_; | |
| 257 }; | |
| 258 | |
| 259 static ThreadLocal thread_local_; | |
| 260 | 285 |
| 261 friend class StackLimitCheck; | 286 friend class StackLimitCheck; |
| 262 friend class PostponeInterruptsScope; | 287 friend class PostponeInterruptsScope; |
| 288 friend class StackGuardData::ThreadLocal; |
| 263 }; | 289 }; |
| 264 | 290 |
| 265 | 291 |
| 266 // Support for checking for stack-overflows in C++ code. | 292 // Support for checking for stack-overflows in C++ code. |
| 267 class StackLimitCheck BASE_EMBEDDED { | 293 class StackLimitCheck BASE_EMBEDDED { |
| 268 public: | 294 public: |
| 269 bool HasOverflowed() const { | 295 bool HasOverflowed() const { |
| 270 // Stack has overflowed in C++ code only if stack pointer exceeds the C++ | 296 // Stack has overflowed in C++ code only if stack pointer exceeds the C++ |
| 271 // stack guard and the limits are not set to interrupt values. | 297 // stack guard and the limits are not set to interrupt values. |
| 272 // TODO(214): Stack overflows are ignored if a interrupt is pending. This | 298 // TODO(214): Stack overflows are ignored if a interrupt is pending. This |
| 273 // code should probably always use the initial C++ limit. | 299 // code should probably always use the initial C++ limit. |
| 274 return (reinterpret_cast<uintptr_t>(this) < StackGuard::climit()) && | 300 return (reinterpret_cast<uintptr_t>(this) < StackGuard::climit()) && |
| 275 StackGuard::IsStackOverflow(); | 301 StackGuard::IsStackOverflow(); |
| 276 } | 302 } |
| 277 }; | 303 }; |
| 278 | 304 |
| 279 | 305 |
| 280 // Support for temporarily postponing interrupts. When the outermost | 306 // Support for temporarily postponing interrupts. When the outermost |
| 281 // postpone scope is left the interrupts will be re-enabled and any | 307 // postpone scope is left the interrupts will be re-enabled and any |
| 282 // interrupts that occurred while in the scope will be taken into | 308 // interrupts that occurred while in the scope will be taken into |
| 283 // account. | 309 // account. |
| 284 class PostponeInterruptsScope BASE_EMBEDDED { | 310 class PostponeInterruptsScope BASE_EMBEDDED { |
| 285 public: | 311 public: |
| 286 PostponeInterruptsScope() { | 312 PostponeInterruptsScope() { |
| 287 StackGuard::thread_local_.postpone_interrupts_nesting_++; | 313 v8_context()-> |
| 314 stack_guard_data_.thread_local_.postpone_interrupts_nesting_++; |
| 288 StackGuard::DisableInterrupts(); | 315 StackGuard::DisableInterrupts(); |
| 289 } | 316 } |
| 290 | 317 |
| 291 ~PostponeInterruptsScope() { | 318 ~PostponeInterruptsScope() { |
| 292 if (--StackGuard::thread_local_.postpone_interrupts_nesting_ == 0) { | 319 if (--v8_context()-> |
| 320 stack_guard_data_.thread_local_.postpone_interrupts_nesting_ == 0) { |
| 293 StackGuard::EnableInterrupts(); | 321 StackGuard::EnableInterrupts(); |
| 294 } | 322 } |
| 295 } | 323 } |
| 296 }; | 324 }; |
| 297 | 325 |
| 298 | 326 |
| 299 class GCExtension : public v8::Extension { | 327 class GCExtension : public v8::Extension { |
| 300 public: | 328 public: |
| 301 GCExtension() : v8::Extension("v8/gc", kSource) {} | 329 GCExtension() : v8::Extension("v8/gc", kSource) {} |
| 302 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | 330 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 303 v8::Handle<v8::String> name); | 331 v8::Handle<v8::String> name); |
| 304 static v8::Handle<v8::Value> GC(const v8::Arguments& args); | 332 static v8::Handle<v8::Value> GC(const v8::Arguments& args); |
| 305 private: | 333 private: |
| 306 static const char* kSource; | 334 static const char* kSource; |
| 307 }; | 335 }; |
| 308 | 336 |
| 309 | 337 |
| 310 } } // namespace v8::internal | 338 } } // namespace v8::internal |
| 311 | 339 |
| 312 #endif // V8_EXECUTION_H_ | 340 #endif // V8_EXECUTION_H_ |
| OLD | NEW |