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/handles.h" 9 10 namespace v8 { 11 namespace internal { 12 13 class Execution V8_FINAL : public AllStatic { 14 public: 15 // Call a function, the caller supplies a receiver and an array 16 // of arguments. Arguments are Object* type. After function returns, 17 // pointers in 'args' might be invalid. 18 // 19 // *pending_exception tells whether the invoke resulted in 20 // a pending exception. 21 // 22 // When convert_receiver is set, and the receiver is not an object, 23 // and the function called is not in strict mode, receiver is converted to 24 // an object. 25 // 26 MUST_USE_RESULT static MaybeHandle<Object> Call( 27 Isolate* isolate, 28 Handle<Object> callable, 29 Handle<Object> receiver, 30 int argc, 31 Handle<Object> argv[], 32 bool convert_receiver = false); 33 34 // Construct object from function, the caller supplies an array of 35 // arguments. Arguments are Object* type. After function returns, 36 // pointers in 'args' might be invalid. 37 // 38 // *pending_exception tells whether the invoke resulted in 39 // a pending exception. 40 // 41 MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func, 42 int argc, 43 Handle<Object> argv[]); 44 45 // Call a function, just like Call(), but make sure to silently catch 46 // any thrown exceptions. The return value is either the result of 47 // calling the function (if caught exception is false) or the exception 48 // that occurred (if caught exception is true). 49 static MaybeHandle<Object> TryCall( 50 Handle<JSFunction> func, 51 Handle<Object> receiver, 52 int argc, 53 Handle<Object> argv[], 54 Handle<Object>* exception_out = NULL); 55 56 // ECMA-262 9.3 57 MUST_USE_RESULT static MaybeHandle<Object> ToNumber( 58 Isolate* isolate, Handle<Object> obj); 59 60 // ECMA-262 9.4 61 MUST_USE_RESULT static MaybeHandle<Object> ToInteger( 62 Isolate* isolate, Handle<Object> obj); 63 64 // ECMA-262 9.5 65 MUST_USE_RESULT static MaybeHandle<Object> ToInt32( 66 Isolate* isolate, Handle<Object> obj); 67 68 // ECMA-262 9.6 69 MUST_USE_RESULT static MaybeHandle<Object> ToUint32( 70 Isolate* isolate, Handle<Object> obj); 71 72 // ECMA-262 9.8 73 MUST_USE_RESULT static MaybeHandle<Object> ToString( 74 Isolate* isolate, Handle<Object> obj); 75 76 // ECMA-262 9.8 77 MUST_USE_RESULT static MaybeHandle<Object> ToDetailString( 78 Isolate* isolate, Handle<Object> obj); 79 80 // ECMA-262 9.9 81 MUST_USE_RESULT static MaybeHandle<Object> ToObject( 82 Isolate* isolate, Handle<Object> obj); 83 84 // Create a new date object from 'time'. 85 MUST_USE_RESULT static MaybeHandle<Object> NewDate( 86 Isolate* isolate, double time); 87 88 // Create a new regular expression object from 'pattern' and 'flags'. 89 MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp( 90 Handle<String> pattern, Handle<String> flags); 91 92 // Used to implement [] notation on strings (calls JS code) 93 static Handle<Object> CharAt(Handle<String> str, uint32_t index); 94 95 static Handle<Object> GetFunctionFor(); 96 MUST_USE_RESULT static MaybeHandle<JSFunction> InstantiateFunction( 97 Handle<FunctionTemplateInfo> data); 98 MUST_USE_RESULT static MaybeHandle<JSObject> InstantiateObject( 99 Handle<ObjectTemplateInfo> data); 100 MUST_USE_RESULT static MaybeHandle<Object> ConfigureInstance( 101 Isolate* isolate, Handle<Object> instance, Handle<Object> data); 102 static Handle<String> GetStackTraceLine(Handle<Object> recv, 103 Handle<JSFunction> fun, 104 Handle<Object> pos, 105 Handle<Object> is_global); 106 107 // Get a function delegate (or undefined) for the given non-function 108 // object. Used for support calling objects as functions. 109 static Handle<Object> GetFunctionDelegate(Isolate* isolate, 110 Handle<Object> object); 111 MUST_USE_RESULT static MaybeHandle<Object> TryGetFunctionDelegate( 112 Isolate* isolate, 113 Handle<Object> object); 114 115 // Get a function delegate (or undefined) for the given non-function 116 // object. Used for support calling objects as constructors. 117 static Handle<Object> GetConstructorDelegate(Isolate* isolate, 118 Handle<Object> object); 119 static MaybeHandle<Object> TryGetConstructorDelegate(Isolate* isolate, 120 Handle<Object> object); 121 }; 122 123 124 class ExecutionAccess; 125 126 127 // StackGuard contains the handling of the limits that are used to limit the 128 // number of nested invocations of JavaScript and the stack size used in each 129 // invocation. 130 class StackGuard V8_FINAL { 131 public: 132 // Pass the address beyond which the stack should not grow. The stack 133 // is assumed to grow downwards. 134 void SetStackLimit(uintptr_t limit); 135 136 // Threading support. 137 char* ArchiveStackGuard(char* to); 138 char* RestoreStackGuard(char* from); ArchiveSpacePerThread()139 static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); } 140 void FreeThreadResources(); 141 // Sets up the default stack guard for this thread if it has not 142 // already been set up. 143 void InitThread(const ExecutionAccess& lock); 144 // Clears the stack guard for this thread so it does not look as if 145 // it has been set up. 146 void ClearThread(const ExecutionAccess& lock); 147 148 #define INTERRUPT_LIST(V) \ 149 V(DEBUGBREAK, DebugBreak) \ 150 V(DEBUGCOMMAND, DebugCommand) \ 151 V(TERMINATE_EXECUTION, TerminateExecution) \ 152 V(GC_REQUEST, GC) \ 153 V(INSTALL_CODE, InstallCode) \ 154 V(API_INTERRUPT, ApiInterrupt) \ 155 V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites) 156 157 #define V(NAME, Name) \ 158 inline bool Check##Name() { return CheckInterrupt(1 << NAME); } \ 159 inline void Request##Name() { RequestInterrupt(1 << NAME); } \ 160 inline void Clear##Name() { ClearInterrupt(1 << NAME); } INTERRUPT_LIST(V)161 INTERRUPT_LIST(V) 162 #undef V 163 164 // This provides an asynchronous read of the stack limits for the current 165 // thread. There are no locks protecting this, but it is assumed that you 166 // have the global V8 lock if you are using multiple V8 threads. 167 uintptr_t climit() { 168 return thread_local_.climit_; 169 } real_climit()170 uintptr_t real_climit() { 171 return thread_local_.real_climit_; 172 } jslimit()173 uintptr_t jslimit() { 174 return thread_local_.jslimit_; 175 } real_jslimit()176 uintptr_t real_jslimit() { 177 return thread_local_.real_jslimit_; 178 } address_of_jslimit()179 Address address_of_jslimit() { 180 return reinterpret_cast<Address>(&thread_local_.jslimit_); 181 } address_of_real_jslimit()182 Address address_of_real_jslimit() { 183 return reinterpret_cast<Address>(&thread_local_.real_jslimit_); 184 } 185 186 // If the stack guard is triggered, but it is not an actual 187 // stack overflow, then handle the interruption accordingly. 188 Object* HandleInterrupts(); 189 190 private: 191 StackGuard(); 192 193 // Flag used to set the interrupt causes. 194 enum InterruptFlag { 195 #define V(NAME, Name) NAME, 196 INTERRUPT_LIST(V) 197 #undef V 198 NUMBER_OF_INTERRUPTS 199 }; 200 201 bool CheckInterrupt(int flagbit); 202 void RequestInterrupt(int flagbit); 203 void ClearInterrupt(int flagbit); 204 bool CheckAndClearInterrupt(InterruptFlag flag); 205 206 // You should hold the ExecutionAccess lock when calling this method. has_pending_interrupts(const ExecutionAccess & lock)207 bool has_pending_interrupts(const ExecutionAccess& lock) { 208 // Sanity check: We shouldn't be asking about pending interrupts 209 // unless we're not postponing them anymore. 210 ASSERT(!should_postpone_interrupts(lock)); 211 return thread_local_.interrupt_flags_ != 0; 212 } 213 214 // You should hold the ExecutionAccess lock when calling this method. should_postpone_interrupts(const ExecutionAccess & lock)215 bool should_postpone_interrupts(const ExecutionAccess& lock) { 216 return thread_local_.postpone_interrupts_nesting_ > 0; 217 } 218 219 // You should hold the ExecutionAccess lock when calling this method. 220 inline void set_interrupt_limits(const ExecutionAccess& lock); 221 222 // Reset limits to actual values. For example after handling interrupt. 223 // You should hold the ExecutionAccess lock when calling this method. 224 inline void reset_limits(const ExecutionAccess& lock); 225 226 // Enable or disable interrupts. 227 void EnableInterrupts(); 228 void DisableInterrupts(); 229 230 #if V8_TARGET_ARCH_X64 || V8_TARGET_ARCH_ARM64 231 static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe); 232 static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8); 233 #else 234 static const uintptr_t kInterruptLimit = 0xfffffffe; 235 static const uintptr_t kIllegalLimit = 0xfffffff8; 236 #endif 237 238 class ThreadLocal V8_FINAL { 239 public: ThreadLocal()240 ThreadLocal() { Clear(); } 241 // You should hold the ExecutionAccess lock when you call Initialize or 242 // Clear. 243 void Clear(); 244 245 // Returns true if the heap's stack limits should be set, false if not. 246 bool Initialize(Isolate* isolate); 247 248 // The stack limit is split into a JavaScript and a C++ stack limit. These 249 // two are the same except when running on a simulator where the C++ and 250 // JavaScript stacks are separate. Each of the two stack limits have two 251 // values. The one eith the real_ prefix is the actual stack limit 252 // set for the VM. The one without the real_ prefix has the same value as 253 // the actual stack limit except when there is an interruption (e.g. debug 254 // break or preemption) in which case it is lowered to make stack checks 255 // fail. Both the generated code and the runtime system check against the 256 // one without the real_ prefix. 257 uintptr_t real_jslimit_; // Actual JavaScript stack limit set for the VM. 258 uintptr_t jslimit_; 259 uintptr_t real_climit_; // Actual C++ stack limit set for the VM. 260 uintptr_t climit_; 261 262 int nesting_; 263 int postpone_interrupts_nesting_; 264 int interrupt_flags_; 265 }; 266 267 class StackPointer { 268 public: address()269 inline uintptr_t address() { return reinterpret_cast<uintptr_t>(this); } 270 }; 271 272 // TODO(isolates): Technically this could be calculated directly from a 273 // pointer to StackGuard. 274 Isolate* isolate_; 275 ThreadLocal thread_local_; 276 277 friend class Isolate; 278 friend class StackLimitCheck; 279 friend class PostponeInterruptsScope; 280 281 DISALLOW_COPY_AND_ASSIGN(StackGuard); 282 }; 283 284 } } // namespace v8::internal 285 286 #endif // V8_EXECUTION_H_ 287