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. 4 5 #ifndef V8_EXECUTION_H_ 6 #define V8_EXECUTION_H_ 7 8 #include "src/base/atomicops.h" 9 #include "src/globals.h" 10 11 namespace v8 { 12 namespace internal { 13 14 template <typename T> 15 class Handle; 16 17 class Execution final : public AllStatic { 18 public: 19 // Whether to report pending messages, or keep them pending on the isolate. 20 enum class MessageHandling { kReport, kKeepPending }; 21 enum class Target { kCallable, kRunMicrotasks }; 22 23 // Call a function, the caller supplies a receiver and an array 24 // of arguments. 25 // 26 // When the function called is not in strict mode, receiver is 27 // converted to an object. 28 // 29 V8_EXPORT_PRIVATE V8_WARN_UNUSED_RESULT static MaybeHandle<Object> Call( 30 Isolate* isolate, Handle<Object> callable, Handle<Object> receiver, 31 int argc, Handle<Object> argv[]); 32 33 // Construct object from function, the caller supplies an array of 34 // arguments. 35 V8_WARN_UNUSED_RESULT static MaybeHandle<Object> New( 36 Isolate* isolate, Handle<Object> constructor, int argc, 37 Handle<Object> argv[]); 38 V8_WARN_UNUSED_RESULT static MaybeHandle<Object> New( 39 Isolate* isolate, Handle<Object> constructor, Handle<Object> new_target, 40 int argc, Handle<Object> argv[]); 41 42 // Call a function, just like Call(), but handle don't report exceptions 43 // externally. 44 // The return value is either the result of calling the function (if no 45 // exception occurred), or an empty handle. 46 // If message_handling is MessageHandling::kReport, exceptions (except for 47 // termination exceptions) will be stored in exception_out (if not a 48 // nullptr). 49 static MaybeHandle<Object> TryCall(Isolate* isolate, Handle<Object> callable, 50 Handle<Object> receiver, int argc, 51 Handle<Object> argv[], 52 MessageHandling message_handling, 53 MaybeHandle<Object>* exception_out, 54 Target target = Target::kCallable); 55 // Convenience method for performing RunMicrotasks 56 static MaybeHandle<Object> RunMicrotasks(Isolate* isolate, 57 MessageHandling message_handling, 58 MaybeHandle<Object>* exception_out); 59 }; 60 61 62 class ExecutionAccess; 63 class InterruptsScope; 64 65 // StackGuard contains the handling of the limits that are used to limit the 66 // number of nested invocations of JavaScript and the stack size used in each 67 // invocation. 68 class V8_EXPORT_PRIVATE StackGuard final { 69 public: 70 // Pass the address beyond which the stack should not grow. The stack 71 // is assumed to grow downwards. 72 void SetStackLimit(uintptr_t limit); 73 74 // The simulator uses a separate JS stack. Limits on the JS stack might have 75 // to be adjusted in order to reflect overflows of the C stack, because we 76 // cannot rely on the interleaving of frames on the simulator. 77 void AdjustStackLimitForSimulator(); 78 79 // Threading support. 80 char* ArchiveStackGuard(char* to); 81 char* RestoreStackGuard(char* from); ArchiveSpacePerThread()82 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); } 83 void FreeThreadResources(); 84 // Sets up the default stack guard for this thread if it has not 85 // already been set up. 86 void InitThread(const ExecutionAccess& lock); 87 // Clears the stack guard for this thread so it does not look as if 88 // it has been set up. 89 void ClearThread(const ExecutionAccess& lock); 90 91 #define INTERRUPT_LIST(V) \ 92 V(TERMINATE_EXECUTION, TerminateExecution, 0) \ 93 V(GC_REQUEST, GC, 1) \ 94 V(INSTALL_CODE, InstallCode, 2) \ 95 V(API_INTERRUPT, ApiInterrupt, 3) \ 96 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 4) 97 98 #define V(NAME, Name, id) \ 99 inline bool Check##Name() { return CheckInterrupt(NAME); } \ 100 inline bool CheckAndClear##Name() { return CheckAndClearInterrupt(NAME); } \ 101 inline void Request##Name() { RequestInterrupt(NAME); } \ 102 inline void Clear##Name() { ClearInterrupt(NAME); } 103 INTERRUPT_LIST(V) 104 #undef V 105 106 // Flag used to set the interrupt causes. 107 enum InterruptFlag { 108 #define V(NAME, Name, id) NAME = (1 << id), 109 INTERRUPT_LIST(V) 110 #undef V 111 #define V(NAME, Name, id) NAME | 112 ALL_INTERRUPTS = INTERRUPT_LIST(V) 0 113 #undef V 114 }; 115 climit()116 uintptr_t climit() { return thread_local_.climit(); } jslimit()117 uintptr_t jslimit() { return thread_local_.jslimit(); } 118 // This provides an asynchronous read of the stack limits for the current 119 // thread. There are no locks protecting this, but it is assumed that you 120 // have the global V8 lock if you are using multiple V8 threads. real_climit()121 uintptr_t real_climit() { 122 return thread_local_.real_climit_; 123 } real_jslimit()124 uintptr_t real_jslimit() { 125 return thread_local_.real_jslimit_; 126 } address_of_jslimit()127 Address address_of_jslimit() { 128 return reinterpret_cast<Address>(&thread_local_.jslimit_); 129 } address_of_real_jslimit()130 Address address_of_real_jslimit() { 131 return reinterpret_cast<Address>(&thread_local_.real_jslimit_); 132 } 133 134 // If the stack guard is triggered, but it is not an actual 135 // stack overflow, then handle the interruption accordingly. 136 Object* HandleInterrupts(); 137 138 private: 139 StackGuard(); 140 141 bool CheckInterrupt(InterruptFlag flag); 142 void RequestInterrupt(InterruptFlag flag); 143 void ClearInterrupt(InterruptFlag flag); 144 bool CheckAndClearInterrupt(InterruptFlag flag); 145 146 // You should hold the ExecutionAccess lock when calling this method. has_pending_interrupts(const ExecutionAccess & lock)147 bool has_pending_interrupts(const ExecutionAccess& lock) { 148 return thread_local_.interrupt_flags_ != 0; 149 } 150 151 // You should hold the ExecutionAccess lock when calling this method. 152 inline void set_interrupt_limits(const ExecutionAccess& lock); 153 154 // Reset limits to actual values. For example after handling interrupt. 155 // You should hold the ExecutionAccess lock when calling this method. 156 inline void reset_limits(const ExecutionAccess& lock); 157 158 // Enable or disable interrupts. 159 void EnableInterrupts(); 160 void DisableInterrupts(); 161 162 #if V8_TARGET_ARCH_64_BIT 163 static const uintptr_t kInterruptLimit = uintptr_t{0xfffffffffffffffe}; 164 static const uintptr_t kIllegalLimit = uintptr_t{0xfffffffffffffff8}; 165 #else 166 static const uintptr_t kInterruptLimit = 0xfffffffe; 167 static const uintptr_t kIllegalLimit = 0xfffffff8; 168 #endif 169 170 void PushInterruptsScope(InterruptsScope* scope); 171 void PopInterruptsScope(); 172 173 class ThreadLocal final { 174 public: ThreadLocal()175 ThreadLocal() { Clear(); } 176 // You should hold the ExecutionAccess lock when you call Initialize or 177 // Clear. 178 void Clear(); 179 180 // Returns true if the heap's stack limits should be set, false if not. 181 bool Initialize(Isolate* isolate); 182 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. 193 uintptr_t real_climit_; // Actual C++ stack limit set for the VM. 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 jslimit()200 uintptr_t jslimit() { 201 return bit_cast<uintptr_t>(base::Relaxed_Load(&jslimit_)); 202 } set_jslimit(uintptr_t limit)203 void set_jslimit(uintptr_t limit) { 204 return base::Relaxed_Store(&jslimit_, 205 static_cast<base::AtomicWord>(limit)); 206 } climit()207 uintptr_t climit() { 208 return bit_cast<uintptr_t>(base::Relaxed_Load(&climit_)); 209 } set_climit(uintptr_t limit)210 void set_climit(uintptr_t limit) { 211 return base::Relaxed_Store(&climit_, 212 static_cast<base::AtomicWord>(limit)); 213 } 214 215 InterruptsScope* interrupt_scopes_; 216 int interrupt_flags_; 217 }; 218 219 // TODO(isolates): Technically this could be calculated directly from a 220 // pointer to StackGuard. 221 Isolate* isolate_; 222 ThreadLocal thread_local_; 223 224 friend class Isolate; 225 friend class StackLimitCheck; 226 friend class InterruptsScope; 227 228 DISALLOW_COPY_AND_ASSIGN(StackGuard); 229 }; 230 231 } // namespace internal 232 } // namespace v8 233 234 #endif // V8_EXECUTION_H_ 235