Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_EXECUTION_H_ |
| 6 | #define V8_EXECUTION_H_ |
| 7 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include "src/allocation.h" |
| 9 | #include "src/base/atomicops.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 10 | #include "src/handles.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 11 | #include "src/utils.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 12 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 16 | class Execution final : public AllStatic { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | public: |
| 18 | // Call a function, the caller supplies a receiver and an array |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | // of arguments. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 20 | // |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 21 | // When the function called is not in strict mode, receiver is |
| 22 | // converted to an object. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 23 | // |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 24 | MUST_USE_RESULT static MaybeHandle<Object> Call(Isolate* isolate, |
| 25 | Handle<Object> callable, |
| 26 | Handle<Object> receiver, |
| 27 | int argc, |
| 28 | Handle<Object> argv[]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | |
| 30 | // Construct object from function, the caller supplies an array of |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 31 | // arguments. |
| 32 | MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> constructor, |
| 33 | int argc, |
| 34 | Handle<Object> argv[]); |
| 35 | MUST_USE_RESULT static MaybeHandle<Object> New(Isolate* isolate, |
| 36 | Handle<Object> constructor, |
| 37 | Handle<Object> new_target, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | int argc, |
| 39 | Handle<Object> argv[]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | |
| 41 | // Call a function, just like Call(), but make sure to silently catch |
| 42 | // any thrown exceptions. The return value is either the result of |
| 43 | // calling the function (if caught exception is false) or the exception |
| 44 | // that occurred (if caught exception is true). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | // In the exception case, exception_out holds the caught exceptions, unless |
| 46 | // it is a termination exception. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | static MaybeHandle<Object> TryCall(Isolate* isolate, Handle<Object> callable, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | Handle<Object> receiver, int argc, |
| 49 | Handle<Object> argv[], |
| 50 | MaybeHandle<Object>* exception_out = NULL); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | static Handle<String> GetStackTraceLine(Handle<Object> recv, |
| 53 | Handle<JSFunction> fun, |
| 54 | Handle<Object> pos, |
| 55 | Handle<Object> is_global); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | |
| 59 | class ExecutionAccess; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 60 | class PostponeInterruptsScope; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | // StackGuard contains the handling of the limits that are used to limit the |
| 64 | // number of nested invocations of JavaScript and the stack size used in each |
| 65 | // invocation. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | class StackGuard final { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | public: |
| 68 | // Pass the address beyond which the stack should not grow. The stack |
| 69 | // is assumed to grow downwards. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 70 | void SetStackLimit(uintptr_t limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | // The simulator uses a separate JS stack. Limits on the JS stack might have |
| 73 | // to be adjusted in order to reflect overflows of the C stack, because we |
| 74 | // cannot rely on the interleaving of frames on the simulator. |
| 75 | void AdjustStackLimitForSimulator(); |
| 76 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | // Threading support. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 78 | char* ArchiveStackGuard(char* to); |
| 79 | char* RestoreStackGuard(char* from); |
| 80 | static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); } |
| 81 | void FreeThreadResources(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | // Sets up the default stack guard for this thread if it has not |
| 83 | // already been set up. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 84 | void InitThread(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 85 | // Clears the stack guard for this thread so it does not look as if |
| 86 | // it has been set up. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 87 | void ClearThread(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 88 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 89 | #define INTERRUPT_LIST(V) \ |
| 90 | V(DEBUGBREAK, DebugBreak, 0) \ |
| 91 | V(DEBUGCOMMAND, DebugCommand, 1) \ |
| 92 | V(TERMINATE_EXECUTION, TerminateExecution, 2) \ |
| 93 | V(GC_REQUEST, GC, 3) \ |
| 94 | V(INSTALL_CODE, InstallCode, 4) \ |
| 95 | V(API_INTERRUPT, ApiInterrupt, 5) \ |
| 96 | V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6) |
| 97 | |
| 98 | #define V(NAME, Name, id) \ |
| 99 | inline bool Check##Name() { return CheckInterrupt(NAME); } \ |
| 100 | inline void Request##Name() { RequestInterrupt(NAME); } \ |
| 101 | inline void Clear##Name() { ClearInterrupt(NAME); } |
| 102 | INTERRUPT_LIST(V) |
| 103 | #undef V |
| 104 | |
| 105 | // Flag used to set the interrupt causes. |
| 106 | enum InterruptFlag { |
| 107 | #define V(NAME, Name, id) NAME = (1 << id), |
| 108 | INTERRUPT_LIST(V) |
| 109 | #undef V |
| 110 | #define V(NAME, Name, id) NAME | |
| 111 | ALL_INTERRUPTS = INTERRUPT_LIST(V) 0 |
| 112 | #undef V |
| 113 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 115 | uintptr_t climit() { return thread_local_.climit(); } |
| 116 | uintptr_t jslimit() { return thread_local_.jslimit(); } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 117 | // This provides an asynchronous read of the stack limits for the current |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | // thread. There are no locks protecting this, but it is assumed that you |
| 119 | // have the global V8 lock if you are using multiple V8 threads. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 120 | uintptr_t real_climit() { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 121 | return thread_local_.real_climit_; |
| 122 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 123 | uintptr_t real_jslimit() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 124 | return thread_local_.real_jslimit_; |
| 125 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 126 | Address address_of_jslimit() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 127 | return reinterpret_cast<Address>(&thread_local_.jslimit_); |
| 128 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 129 | Address address_of_real_jslimit() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 130 | return reinterpret_cast<Address>(&thread_local_.real_jslimit_); |
| 131 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | |
| 133 | // If the stack guard is triggered, but it is not an actual |
| 134 | // stack overflow, then handle the interruption accordingly. |
| 135 | Object* HandleInterrupts(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 136 | void HandleGCInterrupt(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | |
| 138 | private: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 139 | StackGuard(); |
| 140 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | bool CheckInterrupt(InterruptFlag flag); |
| 142 | void RequestInterrupt(InterruptFlag flag); |
| 143 | void ClearInterrupt(InterruptFlag flag); |
| 144 | bool CheckAndClearInterrupt(InterruptFlag flag); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | |
| 146 | // You should hold the ExecutionAccess lock when calling this method. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | bool has_pending_interrupts(const ExecutionAccess& lock) { |
| 148 | return thread_local_.interrupt_flags_ != 0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // You should hold the ExecutionAccess lock when calling this method. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 152 | inline void set_interrupt_limits(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 153 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 154 | // Reset limits to actual values. For example after handling interrupt. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 155 | // You should hold the ExecutionAccess lock when calling this method. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 156 | inline void reset_limits(const ExecutionAccess& lock); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | |
| 158 | // Enable or disable interrupts. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 159 | void EnableInterrupts(); |
| 160 | void DisableInterrupts(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 162 | #if V8_TARGET_ARCH_64_BIT |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 163 | static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); |
| 164 | static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); |
| 165 | #else |
| 166 | static const uintptr_t kInterruptLimit = 0xfffffffe; |
| 167 | static const uintptr_t kIllegalLimit = 0xfffffff8; |
| 168 | #endif |
| 169 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | void PushPostponeInterruptsScope(PostponeInterruptsScope* scope); |
| 171 | void PopPostponeInterruptsScope(); |
| 172 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 173 | class ThreadLocal final { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | public: |
| 175 | ThreadLocal() { Clear(); } |
| 176 | // You should hold the ExecutionAccess lock when you call Initialize or |
| 177 | // Clear. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | void Clear(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 179 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 180 | // 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] | 181 | bool Initialize(Isolate* isolate); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 182 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 183 | // The stack limit is split into a JavaScript and a C++ stack limit. These |
| 184 | // two are the same except when running on a simulator where the C++ and |
| 185 | // JavaScript stacks are separate. Each of the two stack limits have two |
| 186 | // values. The one eith the real_ prefix is the actual stack limit |
| 187 | // set for the VM. The one without the real_ prefix has the same value as |
| 188 | // the actual stack limit except when there is an interruption (e.g. debug |
| 189 | // break or preemption) in which case it is lowered to make stack checks |
| 190 | // fail. Both the generated code and the runtime system check against the |
| 191 | // one without the real_ prefix. |
| 192 | uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 193 | uintptr_t real_climit_; // Actual C++ stack limit set for the VM. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 194 | |
| 195 | // jslimit_ and climit_ can be read without any lock. |
| 196 | // Writing requires the ExecutionAccess lock. |
| 197 | base::AtomicWord jslimit_; |
| 198 | base::AtomicWord climit_; |
| 199 | |
| 200 | uintptr_t jslimit() { |
| 201 | return bit_cast<uintptr_t>(base::NoBarrier_Load(&jslimit_)); |
| 202 | } |
| 203 | void set_jslimit(uintptr_t limit) { |
| 204 | return base::NoBarrier_Store(&jslimit_, |
| 205 | static_cast<base::AtomicWord>(limit)); |
| 206 | } |
| 207 | uintptr_t climit() { |
| 208 | return bit_cast<uintptr_t>(base::NoBarrier_Load(&climit_)); |
| 209 | } |
| 210 | void set_climit(uintptr_t limit) { |
| 211 | return base::NoBarrier_Store(&climit_, |
| 212 | static_cast<base::AtomicWord>(limit)); |
| 213 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 214 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | PostponeInterruptsScope* postpone_interrupts_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 216 | int interrupt_flags_; |
| 217 | }; |
| 218 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 219 | // TODO(isolates): Technically this could be calculated directly from a |
| 220 | // pointer to StackGuard. |
| 221 | Isolate* isolate_; |
| 222 | ThreadLocal thread_local_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 224 | friend class Isolate; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | friend class StackLimitCheck; |
| 226 | friend class PostponeInterruptsScope; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 227 | |
| 228 | DISALLOW_COPY_AND_ASSIGN(StackGuard); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 231 | } // namespace internal |
| 232 | } // namespace v8 |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 233 | |
| 234 | #endif // V8_EXECUTION_H_ |