execution.h 10.7 KB
Newer Older
1 2 3
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_EXECUTION_H_
#define V8_EXECUTION_H_

8
#include "src/handles.h"
9

10 11
namespace v8 {
namespace internal {
12

13
class Execution final : public AllStatic {
14 15 16 17 18 19 20 21
 public:
  // Call a function, the caller supplies a receiver and an array
  // of arguments. Arguments are Object* type. After function returns,
  // pointers in 'args' might be invalid.
  //
  // *pending_exception tells whether the invoke resulted in
  // a pending exception.
  //
22 23 24 25
  // When convert_receiver is set, and the receiver is not an object,
  // and the function called is not in strict mode, receiver is converted to
  // an object.
  //
26 27 28 29 30 31 32
  MUST_USE_RESULT static MaybeHandle<Object> Call(
      Isolate* isolate,
      Handle<Object> callable,
      Handle<Object> receiver,
      int argc,
      Handle<Object> argv[],
      bool convert_receiver = false);
33 34 35 36 37 38 39 40

  // Construct object from function, the caller supplies an array of
  // arguments. Arguments are Object* type. After function returns,
  // pointers in 'args' might be invalid.
  //
  // *pending_exception tells whether the invoke resulted in
  // a pending exception.
  //
41 42 43
  MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> func,
                                                 int argc,
                                                 Handle<Object> argv[]);
44 45 46 47 48

  // Call a function, just like Call(), but make sure to silently catch
  // any thrown exceptions. The return value is either the result of
  // calling the function (if caught exception is false) or the exception
  // that occurred (if caught exception is true).
49 50 51 52 53 54
  // In the exception case, exception_out holds the caught exceptions, unless
  // it is a termination exception.
  static MaybeHandle<Object> TryCall(Handle<JSFunction> func,
                                     Handle<Object> receiver, int argc,
                                     Handle<Object> argv[],
                                     MaybeHandle<Object>* exception_out = NULL);
55 56

  // ECMA-262 9.3
57 58
  MUST_USE_RESULT static MaybeHandle<Object> ToNumber(
      Isolate* isolate, Handle<Object> obj);
59 60

  // ECMA-262 9.4
61 62
  MUST_USE_RESULT static MaybeHandle<Object> ToInteger(
      Isolate* isolate, Handle<Object> obj);
63 64

  // ECMA-262 9.5
65 66
  MUST_USE_RESULT static MaybeHandle<Object> ToInt32(
      Isolate* isolate, Handle<Object> obj);
67 68

  // ECMA-262 9.6
69 70
  MUST_USE_RESULT static MaybeHandle<Object> ToUint32(
      Isolate* isolate, Handle<Object> obj);
71

72 73 74 75 76

  // ES6, draft 10-14-14, section 7.1.15
  MUST_USE_RESULT static MaybeHandle<Object> ToLength(
      Isolate* isolate, Handle<Object> obj);

77
  // ECMA-262 9.8
78 79
  MUST_USE_RESULT static MaybeHandle<Object> ToString(
      Isolate* isolate, Handle<Object> obj);
80 81

  // ECMA-262 9.8
82 83
  MUST_USE_RESULT static MaybeHandle<Object> ToDetailString(
      Isolate* isolate, Handle<Object> obj);
84 85

  // ECMA-262 9.9
86 87
  MUST_USE_RESULT static MaybeHandle<Object> ToObject(
      Isolate* isolate, Handle<Object> obj);
88 89

  // Create a new date object from 'time'.
90 91
  MUST_USE_RESULT static MaybeHandle<Object> NewDate(
      Isolate* isolate, double time);
92

93
  // Create a new regular expression object from 'pattern' and 'flags'.
94 95
  MUST_USE_RESULT static MaybeHandle<JSRegExp> NewJSRegExp(
      Handle<String> pattern, Handle<String> flags);
96

97 98 99 100 101
  static Handle<Object> GetFunctionFor();
  static Handle<String> GetStackTraceLine(Handle<Object> recv,
                                          Handle<JSFunction> fun,
                                          Handle<Object> pos,
                                          Handle<Object> is_global);
102

103 104
  // Get a function delegate (or undefined) for the given non-function
  // object. Used for support calling objects as functions.
105 106
  static Handle<Object> GetFunctionDelegate(Isolate* isolate,
                                            Handle<Object> object);
107 108 109
  MUST_USE_RESULT static MaybeHandle<Object> TryGetFunctionDelegate(
      Isolate* isolate,
      Handle<Object> object);
110 111 112

  // Get a function delegate (or undefined) for the given non-function
  // object. Used for support calling objects as constructors.
113 114
  static Handle<Object> GetConstructorDelegate(Isolate* isolate,
                                               Handle<Object> object);
115 116
  static MaybeHandle<Object> TryGetConstructorDelegate(Isolate* isolate,
                                                       Handle<Object> object);
117 118 119 120
};


class ExecutionAccess;
121
class PostponeInterruptsScope;
122 123


124 125 126
// StackGuard contains the handling of the limits that are used to limit the
// number of nested invocations of JavaScript and the stack size used in each
// invocation.
127
class StackGuard final {
128
 public:
129 130
  // Pass the address beyond which the stack should not grow.  The stack
  // is assumed to grow downwards.
131
  void SetStackLimit(uintptr_t limit);
132 133

  // Threading support.
134 135 136 137
  char* ArchiveStackGuard(char* to);
  char* RestoreStackGuard(char* from);
  static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
  void FreeThreadResources();
138 139
  // Sets up the default stack guard for this thread if it has not
  // already been set up.
140
  void InitThread(const ExecutionAccess& lock);
141 142
  // Clears the stack guard for this thread so it does not look as if
  // it has been set up.
143 144
  void ClearThread(const ExecutionAccess& lock);

145 146 147 148 149 150 151 152 153 154 155 156 157
#define INTERRUPT_LIST(V)                                          \
  V(DEBUGBREAK, DebugBreak, 0)                                     \
  V(DEBUGCOMMAND, DebugCommand, 1)                                 \
  V(TERMINATE_EXECUTION, TerminateExecution, 2)                    \
  V(GC_REQUEST, GC, 3)                                             \
  V(INSTALL_CODE, InstallCode, 4)                                  \
  V(API_INTERRUPT, ApiInterrupt, 5)                                \
  V(DEOPT_MARKED_ALLOCATION_SITES, DeoptMarkedAllocationSites, 6)

#define V(NAME, Name, id)                                          \
  inline bool Check##Name() { return CheckInterrupt(NAME); }  \
  inline void Request##Name() { RequestInterrupt(NAME); }     \
  inline void Clear##Name() { ClearInterrupt(NAME); }
158 159
  INTERRUPT_LIST(V)
#undef V
vegorov@chromium.org's avatar
vegorov@chromium.org committed
160

161 162 163 164 165 166 167 168 169 170
  // Flag used to set the interrupt causes.
  enum InterruptFlag {
  #define V(NAME, Name, id) NAME = (1 << id),
    INTERRUPT_LIST(V)
  #undef V
  #define V(NAME, Name, id) NAME |
    ALL_INTERRUPTS = INTERRUPT_LIST(V) 0
  #undef V
  };

171 172
  uintptr_t climit() { return thread_local_.climit(); }
  uintptr_t jslimit() { return thread_local_.jslimit(); }
173
  // This provides an asynchronous read of the stack limits for the current
174 175
  // thread.  There are no locks protecting this, but it is assumed that you
  // have the global V8 lock if you are using multiple V8 threads.
176
  uintptr_t real_climit() {
177 178
    return thread_local_.real_climit_;
  }
179
  uintptr_t real_jslimit() {
180 181
    return thread_local_.real_jslimit_;
  }
182
  Address address_of_jslimit() {
183 184
    return reinterpret_cast<Address>(&thread_local_.jslimit_);
  }
185
  Address address_of_real_jslimit() {
186 187
    return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
  }
188 189 190 191

  // If the stack guard is triggered, but it is not an actual
  // stack overflow, then handle the interruption accordingly.
  Object* HandleInterrupts();
192

193 194 195 196
  bool InterruptRequested() { return GetCurrentStackPosition() < climit(); }

  void CheckAndHandleGCInterrupt();

197
 private:
198 199
  StackGuard();

200 201 202
  bool CheckInterrupt(InterruptFlag flag);
  void RequestInterrupt(InterruptFlag flag);
  void ClearInterrupt(InterruptFlag flag);
203
  bool CheckAndClearInterrupt(InterruptFlag flag);
204

205
  // You should hold the ExecutionAccess lock when calling this method.
206
  bool has_pending_interrupts(const ExecutionAccess& lock) {
207 208 209
    return thread_local_.interrupt_flags_ != 0;
  }

210
  // You should hold the ExecutionAccess lock when calling this method.
211
  inline void set_interrupt_limits(const ExecutionAccess& lock);
212

213
  // Reset limits to actual values. For example after handling interrupt.
214
  // You should hold the ExecutionAccess lock when calling this method.
215
  inline void reset_limits(const ExecutionAccess& lock);
216

217
  // Enable or disable interrupts.
218 219
  void EnableInterrupts();
  void DisableInterrupts();
220

221
#if V8_TARGET_ARCH_64_BIT
222
  static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
223
  static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
224
#else
225
  static const uintptr_t kInterruptLimit = 0xfffffffe;
226
  static const uintptr_t kIllegalLimit = 0xfffffff8;
227
#endif
228

229 230 231
  void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
  void PopPostponeInterruptsScope();

232
  class ThreadLocal final {
233
   public:
234 235 236 237
    ThreadLocal() { Clear(); }
    // You should hold the ExecutionAccess lock when you call Initialize or
    // Clear.
    void Clear();
238

239
    // Returns true if the heap's stack limits should be set, false if not.
240
    bool Initialize(Isolate* isolate);
241

242 243 244 245 246 247 248 249 250 251 252
    // The stack limit is split into a JavaScript and a C++ stack limit. These
    // two are the same except when running on a simulator where the C++ and
    // JavaScript stacks are separate. Each of the two stack limits have two
    // values. The one eith the real_ prefix is the actual stack limit
    // set for the VM. The one without the real_ prefix has the same value as
    // the actual stack limit except when there is an interruption (e.g. debug
    // break or preemption) in which case it is lowered to make stack checks
    // fail. Both the generated code and the runtime system check against the
    // one without the real_ prefix.
    uintptr_t real_jslimit_;  // Actual JavaScript stack limit set for the VM.
    uintptr_t real_climit_;  // Actual C++ stack limit set for the VM.
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

    // jslimit_ and climit_ can be read without any lock.
    // Writing requires the ExecutionAccess lock.
    base::AtomicWord jslimit_;
    base::AtomicWord climit_;

    uintptr_t jslimit() {
      return bit_cast<uintptr_t>(base::NoBarrier_Load(&jslimit_));
    }
    void set_jslimit(uintptr_t limit) {
      return base::NoBarrier_Store(&jslimit_,
                                   static_cast<base::AtomicWord>(limit));
    }
    uintptr_t climit() {
      return bit_cast<uintptr_t>(base::NoBarrier_Load(&climit_));
    }
    void set_climit(uintptr_t limit) {
      return base::NoBarrier_Store(&climit_,
                                   static_cast<base::AtomicWord>(limit));
    }
273

274
    PostponeInterruptsScope* postpone_interrupts_;
275 276
    int interrupt_flags_;
  };
277

278 279 280 281
  // TODO(isolates): Technically this could be calculated directly from a
  //                 pointer to StackGuard.
  Isolate* isolate_;
  ThreadLocal thread_local_;
282

283
  friend class Isolate;
284
  friend class StackLimitCheck;
285
  friend class PostponeInterruptsScope;
286

287
  DISALLOW_COPY_AND_ASSIGN(StackGuard);
288 289 290 291 292
};

} }  // namespace v8::internal

#endif  // V8_EXECUTION_H_