execution.h 8.5 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 9
#include "src/allocation.h"
#include "src/base/atomicops.h"
10
#include "src/handles.h"
11
#include "src/utils.h"
12

13 14
namespace v8 {
namespace internal {
15

16
class Execution final : public AllStatic {
17 18
 public:
  // Call a function, the caller supplies a receiver and an array
19
  // of arguments.
20
  //
21 22
  // When the function called is not in strict mode, receiver is
  // converted to an object.
23
  //
24 25 26
  V8_EXPORT_PRIVATE MUST_USE_RESULT static MaybeHandle<Object> Call(
      Isolate* isolate, Handle<Object> callable, Handle<Object> receiver,
      int argc, Handle<Object> argv[]);
27 28

  // Construct object from function, the caller supplies an array of
29 30 31 32
  // arguments.
  MUST_USE_RESULT static MaybeHandle<Object> New(Handle<JSFunction> constructor,
                                                 int argc,
                                                 Handle<Object> argv[]);
33 34 35
  MUST_USE_RESULT static MaybeHandle<Object> New(Isolate* isolate,
                                                 Handle<Object> constructor,
                                                 Handle<Object> new_target,
36 37
                                                 int argc,
                                                 Handle<Object> argv[]);
38 39 40 41 42

  // 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).
43 44
  // In the exception case, exception_out holds the caught exceptions, unless
  // it is a termination exception.
45
  static MaybeHandle<Object> TryCall(Isolate* isolate, Handle<Object> callable,
46 47 48
                                     Handle<Object> receiver, int argc,
                                     Handle<Object> argv[],
                                     MaybeHandle<Object>* exception_out = NULL);
49 50 51 52
};


class ExecutionAccess;
53
class PostponeInterruptsScope;
54 55


56 57 58
// 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.
59
class StackGuard final {
60
 public:
61 62
  // Pass the address beyond which the stack should not grow.  The stack
  // is assumed to grow downwards.
63
  void SetStackLimit(uintptr_t limit);
64

65 66 67 68 69
  // The simulator uses a separate JS stack. Limits on the JS stack might have
  // to be adjusted in order to reflect overflows of the C stack, because we
  // cannot rely on the interleaving of frames on the simulator.
  void AdjustStackLimitForSimulator();

70
  // Threading support.
71 72 73 74
  char* ArchiveStackGuard(char* to);
  char* RestoreStackGuard(char* from);
  static int ArchiveSpacePerThread() { return sizeof(ThreadLocal); }
  void FreeThreadResources();
75 76
  // Sets up the default stack guard for this thread if it has not
  // already been set up.
77
  void InitThread(const ExecutionAccess& lock);
78 79
  // Clears the stack guard for this thread so it does not look as if
  // it has been set up.
80 81
  void ClearThread(const ExecutionAccess& lock);

82 83 84 85 86 87 88 89 90 91 92 93 94
#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); }
95 96
  INTERRUPT_LIST(V)
#undef V
vegorov@chromium.org's avatar
vegorov@chromium.org committed
97

98 99 100 101 102 103 104 105 106 107
  // 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
  };

108 109
  uintptr_t climit() { return thread_local_.climit(); }
  uintptr_t jslimit() { return thread_local_.jslimit(); }
110
  // This provides an asynchronous read of the stack limits for the current
111 112
  // 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.
113
  uintptr_t real_climit() {
114 115
    return thread_local_.real_climit_;
  }
116
  uintptr_t real_jslimit() {
117 118
    return thread_local_.real_jslimit_;
  }
119
  Address address_of_jslimit() {
120 121
    return reinterpret_cast<Address>(&thread_local_.jslimit_);
  }
122
  Address address_of_real_jslimit() {
123 124
    return reinterpret_cast<Address>(&thread_local_.real_jslimit_);
  }
125 126 127 128

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

131
 private:
132 133
  StackGuard();

134 135 136
  bool CheckInterrupt(InterruptFlag flag);
  void RequestInterrupt(InterruptFlag flag);
  void ClearInterrupt(InterruptFlag flag);
137
  bool CheckAndClearInterrupt(InterruptFlag flag);
138

139
  // You should hold the ExecutionAccess lock when calling this method.
140
  bool has_pending_interrupts(const ExecutionAccess& lock) {
141 142 143
    return thread_local_.interrupt_flags_ != 0;
  }

144
  // You should hold the ExecutionAccess lock when calling this method.
145
  inline void set_interrupt_limits(const ExecutionAccess& lock);
146

147
  // Reset limits to actual values. For example after handling interrupt.
148
  // You should hold the ExecutionAccess lock when calling this method.
149
  inline void reset_limits(const ExecutionAccess& lock);
150

151
  // Enable or disable interrupts.
152 153
  void EnableInterrupts();
  void DisableInterrupts();
154

155
#if V8_TARGET_ARCH_64_BIT
156
  static const uintptr_t kInterruptLimit = V8_UINT64_C(0xfffffffffffffffe);
157
  static const uintptr_t kIllegalLimit = V8_UINT64_C(0xfffffffffffffff8);
158
#else
159
  static const uintptr_t kInterruptLimit = 0xfffffffe;
160
  static const uintptr_t kIllegalLimit = 0xfffffff8;
161
#endif
162

163 164 165
  void PushPostponeInterruptsScope(PostponeInterruptsScope* scope);
  void PopPostponeInterruptsScope();

166
  class ThreadLocal final {
167
   public:
168 169 170 171
    ThreadLocal() { Clear(); }
    // You should hold the ExecutionAccess lock when you call Initialize or
    // Clear.
    void Clear();
172

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

176 177 178 179 180 181 182 183 184 185 186
    // 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.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    // 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));
    }
207

208
    PostponeInterruptsScope* postpone_interrupts_;
209 210
    int interrupt_flags_;
  };
211

212 213 214 215
  // TODO(isolates): Technically this could be calculated directly from a
  //                 pointer to StackGuard.
  Isolate* isolate_;
  ThreadLocal thread_local_;
216

217
  friend class Isolate;
218
  friend class StackLimitCheck;
219
  friend class PostponeInterruptsScope;
220

221
  DISALLOW_COPY_AND_ASSIGN(StackGuard);
222 223
};

224 225
}  // namespace internal
}  // namespace v8
226 227

#endif  // V8_EXECUTION_H_