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